Interface Objectives Discuss interfaces concept definition - - PDF document

interface objectives
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

Interface

slide-2
SLIDE 2

Objectives

  • Discuss interfaces

– concept – definition – implementation – support for generic code

2

slide-3
SLIDE 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

3

slide-4
SLIDE 4

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

slide-5
SLIDE 5

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

slide-6
SLIDE 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

6

slide-7
SLIDE 7

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); ... }

slide-8
SLIDE 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(); }

8

slide-9
SLIDE 9

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

slide-10
SLIDE 10

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

slide-11
SLIDE 11

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

slide-12
SLIDE 12

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

slide-13
SLIDE 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

13

slide-14
SLIDE 14

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

slide-15
SLIDE 15

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

slide-16
SLIDE 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

class Soldier : Person, IFighter, IWrestler { ... }

base class interface interface

16

slide-17
SLIDE 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(); } class Soldier : IFighter { ... public void Punch(int side) { ... } public void Kick (int side) { ... } public void Block() { ... } } new method code breaks

17

slide-18
SLIDE 18

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

slide-19
SLIDE 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 IStreetFighter : IFighter, IWrestler { void Bite(); } interface IWrestler { void Takedown(int legs); void Escape (); } multiple inheritance allowed

19

slide-20
SLIDE 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

20

slide-21
SLIDE 21

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

slide-22
SLIDE 22

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

slide-23
SLIDE 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

class Soldier : IFighter, IWrestler { public void Block() { ... } ... }

interfaces contain method with same signature class codes one method only

23

slide-24
SLIDE 24

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

slide-25
SLIDE 25

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

slide-26
SLIDE 26

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

slide-27
SLIDE 27

Summary

27

  • Interface

– named collection of declarations – useful to write generic code

  • Classes implement interfaces

– must implement all methods – gain type compatibility with interface