Interface
Objectives Discuss interfaces • – concept – definition – implementation – support for generic code 2
Interface An interface defines a specification • – also called contract • A class can agree to abide by the contract – by implementing the interface – enforced by compiler 3
Interface definition • An interface is created using the keyword interface – body contained in { and } – names follow class naming convention with initial upper case I interface interface IMyInterface definition { ... } 4
Interface contents Interface can contain method, indexer, property, and event • – no implementations allowed – no other types of members allowed interface IMyInterface { method void Process(int arg1, double arg2); indexer float this [int index] { get; set; } property string Name { get; set; } event event MouseEventHandler Mouse; } 5
Access level • Interface contents implicitly public – error to use modifier explicitly interface IMyInterface { error, can not explicitly label public void Process(int arg1, double arg2); as public ... } 6
Framework interfaces .NET Framework defines many interfaces • public interface ICloneable { object Clone(); } public interface IDisposable { public interface IList ... void Dispose(); { } int Add (object value); void Remove (object value); bool Contains(object value); ... } 7
Custom interface Programmer can define interface • interface IFighter { void Punch(int side); void Kick (int side); void Block(); } interface IWrestler { void Takedown(int legs); void Escape(); } 8
Implementing interface Class can support interface • – declare using class : interface syntax – implement contents declare class Soldier : IFighter { public void Punch(int side) implement methods { Move(arms[side], Forward); } public void Kick (int side) { Move(legs[side], Forward); } public void Block() { Move(arms[Left ], Up); Move(arms[Right], Up); } ... } 9
Interface reference Interface reference can refer to implementing object • – access limited – can only access interface members IFighter f = new Soldier(); can only call IFighter f.Punch(Left); methods when using f.Kick(Right); IFighter reference f.Block(); ... 10
Implementation by multiple classes Many classes can support the same interface • – implement methods in their own way Soldier implements IFighter class Soldier : IFighter { public void Punch(int side) { ... } public void Kick (int side) { ... } public void Block() { ... } ... } Robot implement IFighter class Robot : IFighter { public void Punch(int side) { ... } public void Kick (int side) { ... } public void Block() { ... } ... } 11
Generic code Methods coded against interface • – work with any implementing object generic void WarmUp(IFighter f) void Match() { { f.Punch(Left ); Soldier s = new Soldier(); f.Punch(Right); Robot r = new Robot (); f.Kick (Left ); f.Kick (Right); WarmUp(s); } WarmUp(r); void Fight(IFighter a, IFighter b) Fight(s, r); { } a.Punch(Left); b.Block(); b.Punch(Right); ok, both a.Block(); b.Kick(Left); implement } IFighter 12
Type testing Can test if object implements particular interface • – using operator is void March(Soldier s) { test if Soldier if (s is IFighter) implements IFighter ... } 13
Implementing multiple interfaces Class can support several interfaces • – comma separated list in class definition – must define methods of all interfaces class Soldier : IFighter, IWrestler { IFighter public void Punch(int side) { ... } methods public void Kick (int side) { ... } public void Block() { ... } IWrestler public void Takedown(int legs) { ... } methods public void Escape() { ... } ... } 14
Compatibility Object compatible with reference of any supported interface • void WarmUp(IFighter f) { ... } void Stretch(IWrestler w) { ... } void Prepare(Soldier s) { ok since Soldier WarmUp (s); implements Stretch(s); both interfaces } 15
Inheritance and interface Class may inherit from base class and implement interfaces • – same syntax in declaration – base class must be listed first or compile time error base class interface interface class Soldier : Person, IFighter, IWrestler { ... } 16
Interfaces should not grow Adding methods to interface will break client code • – classes no longer implement all methods interface IFighter { void Punch(int side); void Kick (int side); void Block(); new method void Bite(); } class Soldier : IFighter { ... public void Punch(int side) { ... } code breaks public void Kick (int side) { ... } public void Block() { ... } } 17
Interface inheritance Interface can inherit from other interface • – syntax analogous to class inheritance – creates new interface that is a specialization of base interface base interface IFighter { void Punch(int side); void Kick (int side); void Block(); } derived interface IStreetFighter : IFighter { void Bite(); } 18
Multiple interface inheritance Interface can inherit from multiple interfaces • – new interface includes members of all base interfaces – can also add its own interface IFighter interface IWrestler { { void Punch(int side); void Takedown(int legs); void Kick (int side); void Escape (); void Block(); } } multiple inheritance interface IStreetFighter : IFighter, IWrestler allowed { void Bite(); } 19
Implementing derived interface Class implementing derived interface must code all methods • – of all interfaces in hierarchy class Soldier : IStreetFighter { from IFighter public void Punch(int side) { ... } public void Kick (int side) { ... } public void Block() { ... } from IWrestler public void Takedown(int legs) { ... } public void Escape() { ... } from IStreetFighter public void Bite() { ... } ... } 20
Ambiguity Different interfaces may define method with same signature • – ambiguous when class tries to implement both interfaces different interface IFighter interface IWrestler interfaces { { same void Block(); void Block(); signature ... ... } } 21
Resolving ambiguity Class may need to implement interfaces having ambiguity • • Two options to handle the situation – can implement one method that fills both roles – can implement both methods explicitly 22
Implement one method When implementing two interfaces with same method • – can implement one method – the method must logically work in both roles – simplest solution when applicable interfaces contain method class Soldier : IFighter, IWrestler with same signature { class codes one public void Block() method only { ... } ... } 23
Explicit method implementation When implementing two interfaces with same method • – can explicitly implement both methods – must qualify each implementation with interface name – no access modifier allowed interfaces contain 2 methods class Soldier : IFighter, IWrestler with same signature { IFighter version void IFighter.Block() { ... } IWrestler version void IWrestler.Block() { ... } ... } 24
Limitation on explicit method call Call to explicit method implementation limited • • Cannot call through reference of class type – call would be ambiguous since more than one exists – methods are not public Soldier reference Soldier s = new Soldier(); error, cannot call Block s.Block(); ... 25
Calling explicit method implementation Call explicit method implementation with interface reference • – type of reference determines method called Soldier s = new Soldier(); IFighter reference IFighter f = s; calls IFighter.Block f.Block(); IWrestler reference IWrestler w = s; calls IWrestler.Block w.Block(); ... 26
Summary Interface • – named collection of declarations – useful to write generic code • Classes implement interfaces – must implement all methods – gain type compatibility with interface 27
Recommend
More recommend