Docsity

OOPS - Subtypepolymorphism, Study notes of Object Oriented Programming

Toggle sidebar
bg1
Inheritance and Polymorphism
Subtype Polymorphism
Dynamic Binding
Virtual Methods
Dynamic Dispatch
Abstract Class
pf3
pf4
pf5
pf8
pf9
pfa
pfd

Partial preview of the text

Download OOPS - Subtypepolymorphism and more Study notes Object Oriented Programming in PDF only on Docsity!

Inheritance and Polymorphism

 (^) Subtype Polymorphism  (^) Dynamic Binding  (^) Virtual Methods  (^) Dynamic Dispatch  (^) Abstract Class

Dynamic Method Binding

 Inheritance in OOP opens up many interesting

properties.

 One of the main advantages of inheritance is that

some derived class D has all of the members of its

base class B.

 Once D is not hiding any of the public members of B,

then it is possible for an object of D to represent B in

any context where a B could be used.

 This feature is known as subtype polymorphism.

 Lets illustrate an example:

Two Types of Binding

 Static Binding (the default in C++)

 px->print() uses X’s print

 this is known at compile time

 Dynamic Binding

 px->print() uses the print() in the object pointed at

 this is only known at run time

 coded in C++ with virtual functions

Why “only known at run time”?

Assume dynamic binding is being used: X x; Y y; Z z; X *px; : cin >> val; if (val == 1) px = &x; else px = &y; px->print(); // which print() is used?

Example cont…

 Does the choice of the method to be called depend on

the types of x and y?

 If so then this is known as static binding.

 Does the choice of the method to be called depend on

the classes of the objects s and t to which those

variables refer?

 If so then is known as dynamic binding.

 Dynamic binding is central to OOP and ensures that

even if we are using the base class to refer to a child

class, the correct methods will always be called.

Virtual Methods

 C++ uses static binding by default.

 Hence we must explicitly state that we wish to use dynamic

binding.

 To do this we can use the virtual keyword.

 By changing the code in the previous example to look like this:

class person { … virtual void print () { // print person details } };

 With virtual methods, calls are dispatched to the appropriate

implementation at runtime, based upon the class of the object.

Use

int main()

B b;

B *pb;

D d;

// initilise i values in objects

b.i = 3;

d.i = 5;

pb = &b; cout << "pb now points to b\n"; cout << "Calling pb->print()\n"; pb->print(); // uses B::print() pb = &d; cout << "pb now points to d\n"; cout << "Calling pb->print()\n"; pb->print(); // uses D::print() return 0; }

Abstract Classes cont…

 If a class contains at least one abstract method, then

the entire class is abstract.

 This means that it is impossible to create instances of

that class.

 The sub-classes must provide an implementation of

the abstract class and the abstract method provides a

``clue’’ for dynamic method binding.

 Classes that contain abstract methods are known as

interfaces.

 An appropriate scheme for dynamic method binding

must be used.