Presentation is loading. Please wait.

Presentation is loading. Please wait.

Polymorphism Lecture - 9.

Similar presentations


Presentation on theme: "Polymorphism Lecture - 9."— Presentation transcript:

1 Polymorphism Lecture - 9

2 References Polymorphism from Lecture Notes on Object Oriented Programming with C++. Available at Run-time Polymorphism from Lecture Notes on Object Oriented Programming with C++. Available at

3 Introduction Word polymorphism is derived from a Greek word polumorphos. Polumorphos = Polus (many) + Morphe (shaped) When applied to object technology, polymorphism means different forms of data being handled by the same type of operation. Polymorphism is achieved by various forms of overloading and overriding. Overloading and overriding allows functions and operators to behave differently in different contexts.

4 Method Overriding Also known as pure polymorphism.
Refers to the ability of an operation to have the same name, same signature, different implementations and possibly different semantics within different classes. Overriding also has the ability of a derived class to specialise an inherited operation by redefining the implementation of the operation but not its specification.

5 Method Overriding It enables the same message to invoke different operations within different classes based on the class of the object on which the operation is invoked. The invoked operation is determined by dynamically searching the inheritance hierarchy. The actual implementation of an operation is found by searching the class of the object on which the operation is applied. If no implementation is found, the class’s superclasses are searched. The process is repeated until an implementation is found.

6 Example Overridden Method Overriding Method

7 Example: tick Method of Clock Class
void Clock::tick(void) { if (bIncrementSecond(&(tTime.wSecond))) if (bIncrementMinute(&(tTime.wMinute))) if (bIncrementHour(&(tTime.wHour))) if (bIncrementDay(&(tTime.wDay),tTime.wMonth,tTime.wYear)) if (bIncrementMonth(&(tTime.wMonth))) IncrementYear(&(tTime.wYear)); }

8 Example: tick Method of AlarmClock Class
void AlarmClock::tick(void) { TimeStructure tCurrentTime; Clock::tick(); if (bAlarmSet) Clock::GetTime(&tCurrentTime); if (memcmp(&tCurrentTime,&tAlarmTime,sizeof(TimeStructure)) == 0) bAlarmRinging = 1; } if (bAlarmRinging) uwAlarmRingCount++; if (uwAlarmRingCount > 5) bAlarmRinging = 0; uwAlarmRingCount = 0;

9 Overloading The ability of an operation to have the same name, different signatures, different implementations and possibly different semantics within the same class or different classes. It enables messages with same names but different argument lists to invoke different operations within the same class or different classes. The invoked operation is determined by statically considering the signature of the message. Also known as ad hoc polymorphism.

10 Overloading (Contd.) Function (arguments) int float char,float
char, int Impl1 Impl2 Impl3 Impl4

11 Example Overloaded Constructors

12 Example AlarmClock(TimeStructure* ptSetTimeData):Clock(ptSetTimeData)
{ memset(&tAlarmTime,0,sizeof(TimeStructure)); bAlarmSet = 0; bAlarmRinging = 0; uwAlarmRingCount = 0; } AlarmClock(TimeStructure* ptSetTimeData, TimeStructure* ptSetAlarmTime, int bInAlarmSet):Clock(ptSetTimeData) tAlarmTime = *ptSetAlarmTime; bAlarmSet = bInAlarmSet;

13 Static Binding Method overriding also known as inheritance polymorphism, Identically named methods defined in different classes in an inheritance hierarchy. Resolution of the implementation of the method called performed at compile time. By identifying the class to which the object of the class belongs. The identified method is bound to the object. Process is referred to as static binding or early binding.

14 ConnectionlessInterface
Example CommsInterface transmit(Data: BYTE*):int ConnectedInterface A; ConnectionlessInterface B; A.transmit(Buffer); B.transmit(Buffer); ConnectedInterface ConnectionlessInterface transmit(Data: BYTE*):int transmit(Data: BYTE*):int

15 Example (Contd.) For object A the transmit method must belong to the class ConnectedInterface. For object B the transmit method must belong to the class ConnectionlessInterface. The compiler is able to identify the classes to which the two objects belong and bind their respective methods to them. Objects of this hierarchy even instantiated dynamically are bound statically.

16 Dynamic Binding In certain situations, it may not be feasible or possible to identify the class of an object at compile time. i.e., the class of an object that has been instantiated may need to be identified at runtime. When the class of an object cannot be identified at compile time, static (or early) binding of methods cannot take place. The identification of which polymorphic methods are being called by the object must be deferred until runtime. Runtime binding of methods is also known as dynamic or late binding.

17 Dynamic Binding(Contd.)
Dynamic binding achieved by declaring abstract base classes. Abstract base classes contain one or more abstract methods. Abstract methods specify their interface but are not implemented in the abstract base classes. Derived classes contain the implementation of the abstract methods of abstract base classes. Derived classes thus override the abstract methods of their abstract superclasses. This ensures the consistency in method names and signatures required essentially for dynamic binding.

18 Dynamic Binding(Contd.)
Dynamic binding allows derived class objects to be dynamically created and type-casted as base class objects. References of these objects can be passed to functions as references of base class objects. Method calls to abstract methods are resolved at runtime.

19 Dynamic Binding(Contd.)
To achieve the desired behaviour at runtime, the compiler needs to be instructed to defer binding till the abstract method is called. At runtime, the constructor would have been executed and the class of the object would be known. Dynamic binding allows for runtime polymorphism.

20 Example Consider CommsInterface to be an abstract class and transmit method to be an abstract method. ConnectedInterface and Connectionless Interface classes override the abstract transmit method and provide implementations specific to their functionality.

21 Example (Contd.) MessageHandler M; CommsInterface* C; …
if (LinkType == CONNECTED) { C = new ConnectedInterface(); } else C = new ConnectionlessInterface(); M.operate(C ); delete C; MessageHandler ::operate(CommsInterface* C) { C->transmit(Buffer); }


Download ppt "Polymorphism Lecture - 9."

Similar presentations


Ads by Google