0% found this document useful (0 votes)
2 views44 pages

Java Runtime Polymorphism Explained

The document explains key concepts of inheritance and method overriding in Java, highlighting the use of the 'super' keyword to access parent class methods. It discusses runtime polymorphism, where method calls are resolved at runtime based on the actual object type, and introduces dynamic method lookup as a mechanism for determining which method to execute. An example is provided to illustrate how dynamic method lookup works with different shapes in a programming context.

Uploaded by

Mitali
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)
2 views44 pages

Java Runtime Polymorphism Explained

The document explains key concepts of inheritance and method overriding in Java, highlighting the use of the 'super' keyword to access parent class methods. It discusses runtime polymorphism, where method calls are resolved at runtime based on the actual object type, and introduces dynamic method lookup as a mechanism for determining which method to execute. An example is provided to illustrate how dynamic method lookup works with different shapes in a programming context.

Uploaded by

Mitali
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

Inheritance In Java

Method Overriding in Java

❖ If subclass (child class) has the same method as


declared in the parent class, it is known as method
overriding in Java.
Super Class

❖ Super is a keyword of Java which refers to the immediate


parent of a class and is used inside the subclass method
definition for calling a method defined in the superclass.
❖ A superclass having methods as private cannot be called.
Only the methods which are public and protected can be
called by the keyword super.
Run Time Polymorphism Or Dynamic Method dispatch
● It is a process in which a call to an overridden method is resolved at runtime
rather than compile-time.
● In this process, an overridden method is called through the reference variable
of a superclass. The determination of the method to be called is based on the
object being referred to by the reference variable.
● If the reference variable of Parent class refers to the object of Child class, it is
known as upcasting.
Example of Java Runtime Polymorphism
Dynamic Method Look UP

● Dynamic method lookup refers to the process by which a


programming language or runtime environment
determines the appropriate method or function to execute
at runtime, based on the actual type or class of an object.
● In other words, the method or function to be called is
resolved dynamically during program execution rather
than being determined at compile-time.
Example:-
class Shape:
def draw(self):
print("Drawing a generic shape")

class Circle(Shape):
def draw(self):
print("Drawing a circle")

class Square(Shape):
def draw(self): In this example, the draw method is called
print("Drawing a square") on objects of different types (Circle and
# Dynamic method lookup
Square). The dynamic method lookup
shapes = [Circle(), Square()] ensures that the appropriate draw method
for shape in shapes: associated with the actual type of each
[Link]()
object is invoked at runtime.

You might also like