Polymorphism: Object Oriented Programming
Polymorphism: Object Oriented Programming
Lecture 11
Object Oriented Programming
Programming Concept – Binding
• The process of associating a method definition with a
method invocation is called binding
Obj.demo(10, 20);
result = Obj .demo(5.5);
System.out.println("O/P : " + result);
}
}
Run-time Polymorphism
• Runtime polymorphism or Dynamic Method Dispatch
is a process in which a call to an overridden method
is resolved at runtime rather than compile-time
Holiday
• The compiler only checks the compile time error and
does not bind the method call with any actual
method
Christmas
• This method binding if is done at runtime by runtime
environment then we say dynamic polymorphism is
being implemented in the program Holiday day;
day = new
• An object reference can refer to an object of its
Christmas();
class, or to an object of any class related to it by
inheritance
• For example, if the Holiday class is used to derive a
class called Christmas, then a Holiday reference
could be used to point to a Christmas object
• Lets suppose we have a class Animal
• So, which version of makeSound() would you use if you saw Animal
animal = new Dog() ?
• The answer is that Java will choose the true type, and the true type is the
object that actually gets created
• In this example, what we are really creating is a Dog, even though we are
referencing the variable as an Animal
Dynamic Binding
Why use Polymorphism?
• Why in the world wouldn't we just do Dog dog = new Dog() ?
• The answer is that by using abstraction, you can actually group
many different types of objects together
• Let's see how we can use this concept for storing objects:
• We could not do the above if the Cat objects and Dog objects were
made the traditional way and not by referencing them as Animals
Run-time Polymorphism
• In Java, a variable declared type of class A can hold a
reference to an object of class A or an object belonging
to any subclasses of class A.
• Program is able to resolve the correct method related to
the subclass object at runtime. -runtime polymorphism
• Provides the ability to override functionality already
available in the class hierarchy tree.
• At runtime, which version of the method will be invoked
is based on the type of actual object stored in that
reference variable and not on the type of the reference
variable
Run-time Polymorphism
Example
UpCasting & DownCasting
• UpCasting
‒ Assigning a child object to a parent reference
‒ Considered to be a widening conversion or Upcasting
‒ Can be performed by simple assignment
• DownCasting
‒ Assigning a parent object to a child reference
‒ Considered a narrowing conversion or Downcasting
‒ Must be done with a cast
UpCasting
• Upcasting is when an object of a derived class is
assigned to a variable of a base class (or any ancestor
class)
• For Example:
‒ Suppose we defined a class “Base” and one another class with
name “Child”.
‒ We will create the object of child class but we will use the
reference variable of parent class to store the reference of
Child’s object.
Base b = new Child(); //upCasting
Polymorphism via Inheritance
• It is the type of the object being referenced, not the
reference type, that determines which method is invoked
class.
• Since it refers to the subclass object and subclass method overrides
the Parent class method, subclass method is invoked at runtime.
Rule: Runtime
Java Runtime Polymorphism
polymorphism
can't be achieved
by data members
Example - data member
• Since we are accessing the data member which is not overridden, hence it
will access the data member of Parent class always.
Java Runtime Polymorphism
Example – Multi-level Inheritance
DownCasting
• UpCasting is when an object of a derived class is assigned to a
variable of a base class (or any ancestor class)
Vehicle v1 = new Vehicle(); //Base class object
Bus b1 = new Bus(2, 55000, 37); //Derived class object
v1 = b1;