Presentation is loading. Please wait.

Presentation is loading. Please wait.

C# Interfaces C# Class Version 1.0. Copyright © 2012 by Dennis A. Fairclough all rights reserved. 2 Interface  “A surface forming a common boundary between.

Similar presentations


Presentation on theme: "C# Interfaces C# Class Version 1.0. Copyright © 2012 by Dennis A. Fairclough all rights reserved. 2 Interface  “A surface forming a common boundary between."— Presentation transcript:

1 C# Interfaces C# Class Version 1.0

2 Copyright © 2012 by Dennis A. Fairclough all rights reserved. 2 Interface  “A surface forming a common boundary between adjacent regions, bodies,…” – Dictionary.com

3 C# Standard  An interface type defines a contract as a named set of public method prototypes. A class or struct that implements an interface must provide implementations of the interface’s method prototypes. An interface may inherit from multiple base interfaces, and a class or struct may implement multiple interfaces. Copyright © 2012 by Dennis A. Fairclough all rights reserved. 3

4 4 Interfaces in Programming  The member(s) that a class or an object expose(s) is its interface  Classes can implicitly or explicitly share an interface Classes which share common member(s) but are not related implicitly share an interface  STL containers Classes with a common type explicitly share an interface  An explicit interface defines a contract  C# has such an interface construct!

5 Interface (Contract)  Like a class except for: Provides only a specification (prototype) for one or more member methods, properties, indexers, events.  implicity abstract and public, implied & required, i.e. virtual and non-static. Classes and struct’s can implement (inherit) one or more interfaces  “…we defined polymorphism as the ability to perform the same operations on many types, as long as each type shares a common subset of characteristics. The purpose of an interface is to precisely define such a set of characteristics.” (C# Essentials 2 nd Edition) Copyright © 2012 by Dennis A. Fairclough all rights reserved. 5

6 6 C# Interfaces  Define object signatures, names and return data types for: Methods Properties Indexers Events Not Variables!  classes and structs may implement zero or more interfaces Accomplished through no inheritance, single, multiple inheritance A class or struct may implement zero or more interfaces Each Interface member must be implemented in the class that implements that interface

7 C# Standard  Interfaces Overview An interface has the following characteristics: Interfaces Overview Similar to an abstract base class: any non-abstract type inheriting the interface must implement all its members. Adapters are used to modify this constraint. Cannot be instantiated directly. Can contain events, indexers, methods and properties. Contain no implementation of methods. Classes and structs can inherit from zero or more interfaces. An interface can itself inherit from zero or more interfaces Copyright © 2012 by Dennis A. Fairclough all rights reserved. 7

8 8 Defining an Interface public interface IPlayer { void Play();//method prototype }  Notice: interface keyword Only method prototypes are allowed No access specifier allowed on members  all members of an interface are inherently public No function bodies allowed  An interface has no method implementations Common practice is to start the interface name with an “I”  i.e. IPlayer

9 Copyright © 2012 by Dennis A. Fairclough all rights reserved. 9 Interface Inheritance  Interfaces may inherit from other interfaces Any class implementing the interface must implement all the methods of all the interfaces in the inheritance chain  Multiple interface inheritance and hierarchies are allowed public interface IRadioPlayer : IPlayer { double Volume//property prototype { get; set; } }

10 Copyright © 2012 by Dennis A. Fairclough all rights reserved. 10 Implementing Interfaces  Interface members always implemented publicly  Implementing an interface adds the interface type to the implementing class MyRadio is-a IRadioPlayer MyRadio is-a IPlayer public class MyRadio : IRadioPlayer { private double volume = 50f; public double Volume { get { return volume; } set { volume=value; } } public void Play() { // TODO: Implement Play }

11 Copyright © 2012 by Dennis A. Fairclough all rights reserved. 11 With class Inheritance...  Classes may inherit from a class and still implement interfaces Class inheritance MUST come first followed by the interface(s) public class MyEntertainmentCenter : MyRadio, IComparable, IServiceProvider { IComparable Members IServiceProvider Members }

12 Copyright © 2012 by Dennis A. Fairclough all rights reserved. 12 Name Conflicts  What if two Interfaces use the same member name? Not good practice! public interface ICar { void Go(); } public interface IBoat { void Go(); }

13 Copyright © 2012 by Dennis A. Fairclough all rights reserved. 13 The Infamous Car-Boat public class TheInfamousCarBoat : ICar, IBoat { public void Go() { Console.WriteLine("Going... (Wet or dry? I don't know.)"); }  The Implementation of Go() satisfies both interfaces Reference for either ICar or IBoat will call Go()

14 Copyright © 2012 by Dennis A. Fairclough all rights reserved. 14 The Amphibious Vehicle  Implements each explicitly private to the class public to the appropriate Interface An interface reference will call the appropriate method public class AmphibiousVehicle : ICar, IBoat { void IBoat.Go() { Console.WriteLine("IBoat.Go()... Floating"); } void ICar.Go() { Console.WriteLine("ICar.Go()... Driving"); } (See Interfaces Demo)

15 Copyright © 2012 by Dennis A. Fairclough all rights reserved. 15 virtual and override  Interface methods are not virtual It wouldn’t make any sense You can however make them virtual in the implementing class  A override or new modified method may satisfy the interface  A public function in a base class before the interface will satisfy the interface in a derived class  Normal overriding/new hiding rules apply to the derived class

16 Copyright © 2012 by Dennis A. Fairclough all rights reserved. 16 The Proper use of Interfaces  Allow you to separate object definition from implementation  Once an interface is set, changing it can be a big deal Especially if there is code programmed to the interface already Adapters can be used to solve this problem Interface IBetween Programmer A creates Module A implements IBetween Programmer B creates Module B uses an IBetween

17 Interface ValueType or Reference Type Variables  ValueType or Reference Type Variables my be cast to an interface type.  IEnumerable inumb = new MyObject();  What is the implication? Copyright © 2012 by Dennis A. Fairclough all rights reserved. 17

18 Copyright © 2012 by Dennis A. Fairclough all rights reserved. 18 Interfaces in the C# Libraries  IComparable Provides for items to be compared (and thus, sorted)  IComparer Provides for objects to be compared and to override IComparable for standard objects  ICloneable Provides for objects to create a copy of themselves  IDisposable Forces the class to implement the Dispose method  ISerializable Provides for an object to write itself out to disk (serialize itself)  IEnumerable Provides for a class to be traversed like an array (using foreach)  And a whole bunch more!

19 IEnumerable & IEnumerator  See Example Code & Lab Copyright © 2012 by Dennis A. Fairclough all rights reserved. 19

20 Copyright © 2012 by Dennis A. Fairclough all rights reserved. 20 What did you learn?  ??


Download ppt "C# Interfaces C# Class Version 1.0. Copyright © 2012 by Dennis A. Fairclough all rights reserved. 2 Interface  “A surface forming a common boundary between."

Similar presentations


Ads by Google