0% found this document useful (0 votes)
13 views25 pages

Polymorphism

Polymorphism in object-oriented programming refers to the ability of a single interface to represent different underlying forms (data types). It allows objects of different classes to be treated as objects of a common superclass, enabling method overriding and dynamic method dispatch. This concept enhances code reusability and flexibility, allowing for easier maintenance and debugging.

Uploaded by

irejkayani740
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views25 pages

Polymorphism

Polymorphism in object-oriented programming refers to the ability of a single interface to represent different underlying forms (data types). It allows objects of different classes to be treated as objects of a common superclass, enabling method overriding and dynamic method dispatch. This concept enhances code reusability and flexibility, allowing for easier maintenance and debugging.

Uploaded by

irejkayani740
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 25

OBJECT ORIENTED

PROGRAMMING
POLYMORPHI
SM
3

DEFINITION and REAL LIFE EXAMPLE OF POLYMORPHISM

 Polymorphism
 Poly Many
 Morphs  Forms
 Polymorphism Many
 forms
 A person at the same time can have different characteristic.
 Student and an employee.
 So the same person posses different behavior in different situations.
 In other words, polymorphism allows you to define one interface and have
multiple implementations.
 This is called polymorphism.
4

Polymorphism

It simply means “many forms”. That means, the same entity


(function or operator or object) behaves differently in
different scenarios.

Ability of an object to behave in multiple ways means ability of


an Object of base class to respond to own class function and
also respond to other functions with same function name in
other classes. This is called Polymorphism.
5

POLYMORPHISM TYPES
 Runtime polymorphism is a process in which call to the overridden
method is resolved at Runtime (that means which overridden function
will run is decided at runtime).
.
USING SUPER TO ACCESS HIDDEN Members Functions OF
SUPERCLASS
class Parent
{
void show() / Driver class
{ class Main
System.out.println("Parent's {
show()"); public static void main(String[]
} args)
} {
/ Inherited class Child obj = new
class Child extends Parent Child(); obj.show();
{ }
/ This method overrides show() of }
Parent @Override
void show()
{
super.show(); 22

System.out.println("Child's
show()");
Summary
DYNAMIC METHOD DISPATCH
Important point
◾ Since a superclass reference variable can refer to a subclass
object, Java uses this fact to resolve calls to overridden methods at run
time.

◾ This means: Ability of an object to behave in multiple ways


DYNAMIC METHOD DISPATCH
◾ When an overridden method is called through a superclass
reference, Java determines which version of that method to execute
based upon the type of the object being referred to at the time
the call occurs.

◾ When different types of objects are referred to, different


versions of an overridden method will be called.

◾ In other words, it is the type of the object being referred to (not the
type of the reference variable) that determines which version of an
ASSIGNING OBJECT REFERENCE VARIABLES
◾ Object reference variables act differently when an assignment takes
place.
◾ Example:
◾ Box b1 = new Box();
◾ Box b2 = b1;

◾ Here, b1 and b2 will both refer to the same object.


◾ The assignment of b1 and b2 did not allocate any memory or copy any
part of the original object.
◾ It simply makes b2 refer to the same object as does b1.
◾ Thus, any changes made to the object through b2 will effect the object
to which b1 is referring, since they are the same object.
EXAMPLE PROGRAM –METHOD
OVERRIDDEN

; // No object is created here. Reference is created


Inside A's callme
method Inside B's
callme method Inside
DYNAMIC METHOD DISPATCH C's callme methodX
class A { class Dispatch {
void callme() { public static void main(String args[]) {
System.out.println("Inside A's callme A a = new A(); / object of type A
method"); B b = new B(); / object of type B
} C c = new C(); / object of
} type C A r; / obtain a
class B extends A { / override callme() reference of type A r = a; / r
void callme() { refers to an A object
System.out.println("Inside B's callme r.callme(); // calls A's
method");
version of callme
}
r = b; / r refers to a B object
}
r.callme(); // calls B's
class C extends A { / override callme() version of callme
void callme() { r = c; / r refers to a C object
System.out.println("Inside C's callme
r.callme(); // calls C's
method");
UPCASTING IN JAVA
 Typecasting of a child object to a parent object.
 A superclass reference variable can
refer to a subclass object.
 This is also known as upcasting. Java uses this fact to
resolve calls to
overridden methods at run time
ADVANTAGES OF POLYMORPHISM

 It helps the programmer to reuse the codes, i.e.,


objects once written, tested and
implemented can be reused as required.
 Saves a lot of time.

 Single variable can be used to store multiple data types.


 Easy to debug the codes.
APPLYING METHOD OVERRIDING
/ Using run-time / override area for
polymorphism. class Figure rectangle double area() {
{ System.out.println("Inside Area for
double Rectangle."); return dim1 * dim2;
dim1; }
double }
dim2;
class Triangle extends
Figure(dou
Figure { Triangle(double
ble a,
double b) a, double b) {
{ super(a, b);
dim1 = }
a; dim2 / override area for right
= b; triangle double area() {
} System.out.println("Inside Area for
double Triangle."); return dim1 * dim2 / 2;
area() { }
APPLYING METHOD OVERRIDING
class FindAreas {
public static void main(String
args[]) { Figure f = new
Figure(10, 10); Rectangle r =
new Rectangle(9, 5); Triangle t
= new Triangle(10, 8); Figure
figref;

figref = r;
System.out.println("Area is " +
figref.area());
Inside Area for
figref = t; Rectangle. Area is
System.out.println("Area is " + 45.0
figref.area());
Inside Area for
Triangle. Area is
figref = f;
40.0
System.out.println("Area is " +
In Java, we can override methods only, not
the variables(data members), so runtime
polymorphism cannot be achieved by data
members.

You might also like