Lesson 13 & 14 - OOPs Concepts in Java

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 6

OOPs Concepts in Java

and
Real-time Example
Polymorphism in Java | Compile Time, Runtime, Example
 The word polymorphism is derived from two Greek words: poly and morphs. The word
“poly” implies many and “morphs” means forms.
 Therefore, polymorphism means “many forms”. That is one thing that can take many
forms.
Types of Polymorphism in Java
Basically, there are two types of polymorphism in java.
They are:
1. Static polymorphism
2. Dynamic polymorphism
Polymorphism in Java | Compile Time, Runtime, Example
Polymorphism in Java | Compile Time, Runtime, Example

Static Polymorphism in Java


 A polymorphism that is exhibited during compilation is called static polymorphism in
java. In static polymorphism, the behavior of a method is decided at compile-time.
 Hence, Java compiler binds method calls with method definition/body during
compilation. Therefore, this type of polymorphism is also called compile-time
polymorphism in Java.
 Since binding is performed at compile-time, it is also known as early binding.
Compile-time polymorphism can be achieved/implemented by method overloading in
java.
Polymorphism in Java | Compile Time, Runtime, Example
Static Polymorphism in Java
 Method overloading is a mechanism in which a class has multiple methods having the
same name but different signatures.
 It is one of the ways that Java implements polymorphism.
 Another example of static polymorphism is constructor overloading and method hiding.
Dynamic Polymorphism in Java
 A polymorphism that is exhibited at runtime is called dynamic polymorphism in java.
 The behavior of a method is decided at runtime,
therefore, the JVM (Java Virtual Machine) binds the method call with method
definition/body at runtime and invokes the relevant method during runtime when the
method is called.
Polymorphism in Java | Compile Time, Runtime, Example

Dynamic Polymorphism in Java…


 Dynamic or runtime polymorphism can be achieved/implemented in java using method
overriding. Method overriding is a mechanism where a method of Base class is
overridden in the derived class to provide a more specific implementation.
 The signature of method in both base and derived classes is the same but they only differ
in their implementation.

*Java Runtime Polymorphism Example Program refer to material*

You might also like