Polymorphism Lecture - 9.

Slides:



Advertisements
Similar presentations
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
Advertisements

V IRTUAL F UNCTIONS Chapter 10 Department of CSE, BUET 1.
Inheritance and Polymorphism Recitation – 10/(16,17)/2008 CS 180 Department of Computer Science, Purdue University.
OOP Spring 2007 – Recitation 71 Object Oriented Programming Spring 2006 Recitation 8.
OOP Etgar 2008 – Recitation 71 Object Oriented Programming Etgar 2008 Recitation 7.
Polymorphism. Lecture Objectives To understand the concept of polymorphism To understand the concept of static or early binding To understand the concept.
C++ Polymorphism Systems Programming. Systems Programming: Polymorphism 2   Polymorphism Examples   Relationships Among Objects in an Inheritance.
Shallow Versus Deep Copy and Pointers Shallow copy: when two or more pointers of the same types point to the same memory – They point to the same data.
LECTURE 8. Polymorphism One interface, multiple methods C++ supports both compile time and runtime polymorphism.
LECTURE 07 Programming using C# Inheritance
Learners Support Publications Pointers, Virtual Functions and Polymorphism.
1 Classes- Inheritance Multiple Inheritance It is possible to derive a new class from more than one base class. This is called Multiple Inheritance. Under.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Taken from slides of Starting Out with C++ Early Objects Seventh Edition.
Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page 1 Virtual Functions Polymorphism Abstract base classes.
Polymorphism. Introduction ‘one name multiple forms’ Implemented using overloaded functions and operators Early binding or static binding or static linking.
Polymorphism &Virtual Functions
Polymorphism &Virtual Functions 1. Polymorphism in C++ 2 types ▫Compile time polymorphism  Uses static or early binding  Example: Function and operator.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
Object Oriented Programming with C++/ Session 6 / 1 of 44 Multiple Inheritance and Polymorphism Session 6.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Chapter 10 Inheritance and Polymorphism
Copyright 2006 Oxford Consulting, Ltd1 February Polymorphism Polymorphism Polymorphism is a major strength of an object centered paradigm Same.
Review Class Inheritance, Abstract, Interfaces, Polymorphism, GUI (MVC)
10 Polymorphism. 2 Contents Defining Polymorphism Method Overloading Method Overriding Early Binding and Late Binding Implementing Polymorphism.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Chapter -6 Polymorphism
AD Lecture #1 Object Oriented Programming Three Main Principles 1 Inheritance Encapsulation Polymorphism.
Overview of C++ Polymorphism
Recap Introduction to Inheritance Inheritance in C++ IS-A Relationship Polymorphism in Inheritance Classes in Inheritance Visibility Rules Constructor.
Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
Polymorphism & Virtual Functions 1. Objectives 2  Polymorphism in C++  Pointers to derived classes  Important point on inheritance  Introduction to.
C++ How to Program, 7/e. © by Pearson Education, Inc. All Rights Reserved.2.
CSCI-383 Object-Oriented Programming & Design Lecture 17.
Lecture 10 – Polymorphism Nancy Harris with additional slides Professor Adams from Lewis & Bernstein.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Polymorphism in Methods
Modern Programming Tools And Techniques-I
Class A { public : Int x; A()
Object-Oriented Programming
Polymorphism.
CS212: Object Oriented Analysis and Design
Polymorphism & Virtual Functions
Types of Programming Languages
ATS Application Programming: Java Programming
Object Oriented Programming
Polymorphism Lec
Object Oriented Analysis and Design
Inheritance Basics Programming with Inheritance
Java Programming Language
Virtual Functions Department of CSE, BUET Chapter 10.
Week 6 Object-Oriented Programming (2): Polymorphism
Polymorphism Polymorphism
Computer Programming with JAVA
Java – Inheritance.
9: POLYMORPHISM Programming Technique II (SCSJ1023) Jumail Bin Taliba
Pointers Dr. Bhargavi Goswami Department of Computer Science
Polymorphism CT1513.
Polymorphism Polymorphism - Greek for “many forms”
Polymorphism Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition, by Kernighan.
Virtual Functions Polymorphism is supported by C++ both at compile time and at run time. Compile-time polymorphism is achieved by overloading functions.
VIRTUAL FUNCTIONS RITIKA SHARMA.
Lecture 10 Concepts of Programming Languages
Lecture 6: Polymorphism
Static Binding Static binding chooses the function in the class of the base class pointer, ignoring any versions in the class of the object actually.
Computer Science II for Majors
Presentation transcript:

Polymorphism Lecture - 9

References Polymorphism from Lecture Notes on Object Oriented Programming with C++. Available at http://www.geocities.com/obashir/interests/cpp/LECT10.DOC. Run-time Polymorphism from Lecture Notes on Object Oriented Programming with C++. Available at http://www.geocities.com/obashir/interests/cpp/LECT11.DOC.

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.

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.

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.

Example Overridden Method Overriding Method

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

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;

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.

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

Example Overloaded Constructors

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;

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.

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

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.

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.

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.

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.

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.

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.

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