Download presentation
Presentation is loading. Please wait.
Published byNaomi Tate Modified over 5 years ago
1
Interface 11: Interface Programming C# © 2003 DevelopMentor, Inc.
12/1/2003
2
Objectives Discuss interfaces concept definition implementation
support for generic code Programming C# © 2003 DevelopMentor, Inc. 12/1/2003
3
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 Programming C# © 2003 DevelopMentor, Inc. 12/1/2003
4
11: Interface 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 definition interface IMyInterface { ... } Programming C# © 2003 DevelopMentor, Inc. 12/1/2003
5
11: Interface 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 Programming C# © 2003 DevelopMentor, Inc. 12/1/2003
6
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 Programming C# © 2003 DevelopMentor, Inc. 12/1/2003
7
Framework interfaces .NET Framework defines many interfaces
public interface ICloneable { object Clone(); } public interface IDisposable { void Dispose(); } public interface IList ... { int Add (object value); void Remove (object value); bool Contains(object value); ... } Programming C# © 2003 DevelopMentor, Inc. 12/1/2003
8
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(); } Programming C# © 2003 DevelopMentor, Inc. 12/1/2003
9
Implementing interface
Class can support interface declare using class : interface syntax implement contents declare 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 Programming C# © 2003 DevelopMentor, Inc. 12/1/2003
10
11: Interface 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 Programming C# © 2003 DevelopMentor, Inc. 12/1/2003
11
Implementation by multiple classes
11: Interface 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() { ... } ... } Programming C# © 2003 DevelopMentor, Inc. 12/1/2003
12
Generic code Methods coded against interface
work with any implementing object generic 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); } ok, both implement IFighter Programming C# © 2003 DevelopMentor, Inc. 12/1/2003
13
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 Programming C# © 2003 DevelopMentor, Inc. 12/1/2003
14
Implementing multiple interfaces
Class can support several interfaces comma separated list in class definition must define methods of all interfaces 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 Programming C# © 2003 DevelopMentor, Inc. 12/1/2003
15
11: Interface 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); } ok since Soldier implements both interfaces Programming C# © 2003 DevelopMentor, Inc. 12/1/2003
16
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 { ... } Programming C# © 2003 DevelopMentor, Inc. 12/1/2003
17
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(); } new method class Soldier : IFighter { ... public void Punch(int side) { ... } public void Kick (int side) { ... } public void Block() { ... } } code breaks Programming C# © 2003 DevelopMentor, Inc. 12/1/2003
18
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(); } Programming C# © 2003 DevelopMentor, Inc. 12/1/2003
19
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 IWrestler { void Takedown(int legs); void Escape (); } multiple inheritance allowed interface IStreetFighter : IFighter, IWrestler { void Bite(); } Programming C# © 2003 DevelopMentor, Inc. 12/1/2003
20
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 Programming C# © 2003 DevelopMentor, Inc. 12/1/2003
21
Ambiguity Different interfaces may define method with same signature
ambiguous when class tries to implement both interfaces different interfaces interface IFighter { void Block(); ... } interface IWrestler { void Block(); ... } same signature Programming C# © 2003 DevelopMentor, Inc. 12/1/2003
22
11: Interface 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 Programming C# © 2003 DevelopMentor, Inc. 12/1/2003
23
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 with same signature class Soldier : IFighter, IWrestler { public void Block() ... } class codes one method only Programming C# © 2003 DevelopMentor, Inc. 12/1/2003
24
Explicit method implementation
11: Interface 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 with same signature class Soldier : IFighter, IWrestler { void IFighter.Block() { ... } void IWrestler.Block() { ... } ... } IFighter version IWrestler version Programming C# © 2003 DevelopMentor, Inc. 12/1/2003
25
Limitation on explicit method call
11: Interface 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(); s.Block(); ... error, cannot call Block Programming C# © 2003 DevelopMentor, Inc. 12/1/2003
26
Calling explicit method implementation
11: Interface 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 Programming C# © 2003 DevelopMentor, Inc. 12/1/2003
27
Summary Interface named collection of declarations
useful to write generic code Classes implement interfaces must implement all methods gain type compatibility with interface Programming C# © 2003 DevelopMentor, Inc. 12/1/2003
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.