Interface Objectives Discuss interfaces concept definition - - PDF document
Interface Objectives Discuss interfaces concept definition - - PDF document
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
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
4
interface IMyInterface { ... }
interface definition
Interface contents
- Interface can contain method, indexer, property, and event
– no implementations allowed – no other types of members allowed
interface IMyInterface { void Process(int arg1, double arg2); float this [int index] { get; set; } string Name { get; set; } event MouseEventHandler Mouse; }
method indexer property event
5
Access level
- Interface contents implicitly public
– error to use modifier explicitly
interface IMyInterface { public void Process(int arg1, double arg2); ... }
error, can not explicitly label as public
6
Framework interfaces
- .NET Framework defines many interfaces
public interface ICloneable {
- bject Clone();
} 7 public interface IDisposable { void Dispose(); } public interface IList ... { int Add (object value); void Remove (object value); bool Contains(object value); ... }
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
class Soldier : IFighter { public void Punch(int side) { Move(arms[side], Forward); } public void Kick (int side) { Move(legs[side], Forward); } public void Block() { Move(arms[Left ], Up); Move(arms[Right], Up); } ... }
implement methods declare
9
Interface reference
- Interface reference can refer to implementing object
– access limited – can only access interface members
IFighter f = new Soldier(); f.Punch(Left); f.Kick(Right); f.Block(); ...
can only call IFighter methods when using IFighter reference
10
Implementation by multiple classes
- Many classes can support the same interface
– implement methods in their own way
11
class Soldier : IFighter { public void Punch(int side) { ... } public void Kick (int side) { ... } public void Block() { ... } ... } Soldier implements IFighter class Robot : IFighter { public void Punch(int side) { ... } public void Kick (int side) { ... } public void Block() { ... } ... } Robot implement IFighter
Generic code
- Methods coded against interface
– work with any implementing object
12 void WarmUp(IFighter f) { f.Punch(Left ); f.Punch(Right); f.Kick (Left ); f.Kick (Right); } void Fight(IFighter a, IFighter b) { a.Punch(Left); b.Block(); b.Punch(Right); a.Block(); b.Kick(Left); } void Match() { Soldier s = new Soldier(); Robot r = new Robot (); WarmUp(s); WarmUp(r); Fight(s, r); }
generic
- k, both
implement IFighter
Type testing
- Can test if object implements particular interface
– using operator is
void March(Soldier s) { if (s is IFighter) ... }
test if Soldier implements IFighter
13
Implementing multiple interfaces
- Class can support several interfaces
– comma separated list in class definition – must define methods of all interfaces
14
class Soldier : IFighter, IWrestler { public void Punch(int side) { ... } public void Kick (int side) { ... } public void Block() { ... } public void Takedown(int legs) { ... } public void Escape() { ... } ... } IFighter methods IWrestler methods
Compatibility
- Object compatible with reference of any supported interface
void WarmUp(IFighter f) { ... } void Stretch(IWrestler w) { ... } void Prepare(Soldier s) { WarmUp (s); Stretch(s); }
- k since Soldier
implements 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
class Soldier : Person, IFighter, IWrestler { ... }
base class interface interface
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(); void Bite(); } class Soldier : IFighter { ... public void Punch(int side) { ... } public void Kick (int side) { ... } public void Block() { ... } } new method code breaks
17
Interface inheritance
- Interface can inherit from other interface
– syntax analogous to class inheritance – creates new interface that is a specialization of base interface
18
interface IFighter { void Punch(int side); void Kick (int side); void Block(); }
base
interface IStreetFighter : IFighter { void Bite(); }
derived
Multiple interface inheritance
- Interface can inherit from multiple interfaces
– new interface includes members of all base interfaces – can also add its own
interface IFighter { void Punch(int side); void Kick (int side); void Block(); } interface IStreetFighter : IFighter, IWrestler { void Bite(); } interface IWrestler { void Takedown(int legs); void Escape (); } multiple inheritance allowed
19
Implementing derived interface
- Class implementing derived interface must code all methods
– of all interfaces in hierarchy
class Soldier : IStreetFighter { public void Punch(int side) { ... } public void Kick (int side) { ... } public void Block() { ... } public void Takedown(int legs) { ... } public void Escape() { ... } public void Bite() { ... } ... } from IFighter from IWrestler from IStreetFighter
20
Ambiguity
- Different interfaces may define method with same signature
– ambiguous when class tries to implement both interfaces
interface IFighter { void Block(); ... } interface IWrestler { void Block(); ... }
same signature different interfaces 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
class Soldier : IFighter, IWrestler { public void Block() { ... } ... }
interfaces contain method with same signature class codes one 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
class Soldier : IFighter, IWrestler { void IFighter.Block() { ... } void IWrestler.Block() { ... } ... }
interfaces contain 2 methods with same signature IFighter version IWrestler version
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
25
Soldier s = new Soldier(); s.Block(); ...
Soldier reference error, cannot call Block
Calling explicit method implementation
- Call explicit method implementation with interface reference
– type of reference determines method called
Soldier s = new Soldier(); IFighter f = s; f.Block(); IWrestler w = s; w.Block(); ...
IFighter reference calls IFighter.Block IWrestler reference calls IWrestler.Block
26
Summary
27
- Interface
– named collection of declarations – useful to write generic code
- Classes implement interfaces
– must implement all methods – gain type compatibility with interface