Docsity

Polymorphism and Dynamic Binding in Object-Oriented Programming by Dr. Jamil Ahmed, Slides of Object Oriented Programming

Partial preview of the text

Download Polymorphism and Dynamic Binding in Object-Oriented Programming by Dr. Jamil Ahmed and more Slides Object Oriented Programming in PDF only on Docsity!

1

Polymorphism

and

Dynamic Binding

Lecture 8

2

Messaging between Objects

n A computer program involving objects is a sequence of message-sends n When an object receives a message that it understands, it invokes some behavior (encapsulated)

n Various kinds of objects can understand the same message, but respond differently

3

An Example…

The World of Traffic

4

Polymorphism

n The ability for different kinds of objects to respond differently to the same message is called Polymorphism

5

Lecturer and HoD Example

6

What does your OOL offer?

n aLecturer.canDo(someDuty)

n aHoD.canDo(anotherDuty)

n Is that all? Yes, Monomorphic methods

Inherited behavior; Code reused

No, there is more than that

10

Dynamic Binding

n With an interpreted language like BASIC each line of code is dealt with as it comes to the interpreter and only then it is converted into machine-readable code n This is termed dynamic binding (or late binding ) n OO languages’ dynamic binding makes polymorphism really powerful by allowing to connect the appropriate method to the object at run-time

11

How it works?

that is, polymorphism and dynamic binding…together…

12

Substitution

n Remember, we said earlier that whenever we talk about objects belonging to a class we include objects belonging to subclasses

n What does this imply?

Lecturer HoD

13

Implication

n Suppose the code of class A uses an object belonging to class B. The class A should continue to work expectedly (correctly) if the object of class B is substituted by any other object belonging to any subclass of class B.

14

The Real Flavor

n Suppose lecturer is a collection of Lecturer objects

n The collection includes Lecturers and HoDs n Consider the implementation: for(int i = 0; i<lecturer.length;i++) lecturer[i].canDo(seminarOrganization)

n Message-send is dynamically bound to the appropriate code

15

Warning!!!

n In C++, only virtual methods have this behavior

19

A

Three-Minutes

Review

20

Core OO Concepts

n Message sending

n Object Composition: Objects can be defined in terms of other objects

n Behavioral Refinement or Generalization: Classes can be described in terms of other classes

21

What is “Object-Oriented”?

n Something is object-oriented if it can be extended by composition of existing parts or by refinement of behaviors.

Changes in the original parts propagate, so that compositions and refinements that reuse these parts change appropriately

22

Summary

n “Object Oriented” begins…