2. 13-04-2021 POLYMORPHISM 2
Polymorphism is the ability of an object to take on many
forms.
In programming languages polymorphism is the capability of
an action or method to do different things based on the object
that it is acting upon.
Polymorphism is one of the OOPs feature that allows us to
perform a single action in different ways.
The three types of polymorphism are:
ad-hoc (overloading and overriding), parametric (generics)
and dynamic method binding.
Introduction
5. 13-04-2021 Polymorphism 5
Method Overriding in Java
This is an example of runtime time (or dynamic polymorphism)
Method in subclass with same signature overrides method
from base class
Overriding method is the one used for objects of the derived
class
Overriding method must return same type of value
8. 13-04-2021 Polymorphism 8
Overloading happens at compile-time while Overriding happens at runtime: The
binding of overloaded method call to its definition has happens at compile-time
however binding of overridden method call to its definition happens at runtime.
Static methods can be overloaded which means a class can have more than one static
method of same name. Static methods cannot be overridden, even if you declare a
same static method in child class it has nothing to do with the same method of
parent class.
The most basic difference is that overloading is being done in the same class while
for overriding base and child classes are required. Overriding is all about giving a
specific implementation to the inherited method of parent class.
Static binding is being used for overloaded methods and dynamic binding is being
used for overridden/overriding methods.
Overloading vs Overriding in Java
9. 13-04-2021 Polymorphism 9
Performance: Overloading gives better performance compared to
overriding. The reason is that the binding of overridden methods is
being done at runtime.
private and final methods can be overloaded but they cannot be
overridden.
Return type of method does not matter in case of method
overloading, it can be same or different. However in case of
method overriding the overriding method can have more specific
return type (refer this).
Argument list should be different while doing method overloading.
Argument list should be same in method Overriding.
Overloading vs Overriding in Java
10. 13-04-2021 Polymorphism 10
Association of method call to the method body is known
as binding.
There are two types of binding:
Static Binding that happens at compile time
Dynamic Binding that happens at runtime. Before
I explain static and dynamic binding in java, lets see few
terms that will help you understand this concept better.
Static and dynamic binding in java
11. 13-04-2021 Polymorphism 11
class Human{
……
}
class Boy extends Human{
public static void main( String args[]) {
/*This statement simply creates an object of class
*Boy and assigns a reference of Boy to it*/
Boy obj1 = new Boy();
/* Since Boy extends Human class. The object creation
* can be done in this way. Parent class reference
* can have child class reference assigned to it
*/
Human obj2 = new Boy();
}
}
What is reference and object?
12. 13-04-2021 Polymorphism 12
The binding which can be resolved at compile time by compiler is
known as static or early binding.
The binding of static, private and final methods is compile-time.
Why?
The reason is that the these method cannot be overridden and the
type of the class is determined at the compile time.
Static Binding or Early Binding
13. 13-04-2021 Polymorphism 13
class Human{
public static void walk()
{
System.out.println("Human walks");
}
}
class Boy extends Human{
public static void walk(){
System.out.println("Boywalks");
}
public static void main( String args[]) {
/* Reference is of Human type and object is
Boy type
*/
Human obj = new Boy();
/* Reference is of HUman type and
object is of Human type.
*/
Human obj2 = new Human();
obj.walk();
obj2.walk();
}}
Static binding example
Output:
Human walks
Human walks
14. 13-04-2021 Polymorphism 14
When compiler is not able to resolve the call/binding at
compile time, such binding is known as Dynamic or late
Binding.
Method Overriding is a perfect example of dynamic binding
as in overriding both parent and child classes have same
method and in this case the type of the object determines
which method is to be executed.
The type of object is determined at the run time so this is
known as dynamic binding.
Dynamic Binding or Late Binding
16. 13-04-2021 Polymorphism 16
Lets discuss the difference between static and dynamic binding in
Java.
Static binding happens at compile-time while dynamic binding
happens at runtime.
Binding of private, static and final methods always happen at
compile time since these methods cannot be overridden.
When the method overriding is actually happening and the reference
of parent type is assigned to the object of child class type then such
binding is resolved during runtime.
The binding of overloaded methods is static and the binding of
overridden methods is dynamic.
Static Binding vs Dynamic Binding
17. 13-04-2021 Polymorphism 18
What is Abstract Class in Java with example?
Why we need an abstract class?
Abstract class declaration and rules
What is Interface in java with example programs
What is the use of interface in Java?
Multiple inheritance and interfaces
What are the different between Abstract and concrete class ?
what are the Difference Between Abstract Class and Interface in Java?
Home work
Editor's Notes
Overloaded methods are methods with the same name signature but either a different number of parameters or different types in the parameter list. For example 'spinning' a number may mean increase it, 'spinning' an image may mean rotate it by 90 degrees. By defining a method for handling each type of parameter you control the desired effect.
Overridden methods are methods that are redefined within an inherited or subclass. They have the same signature and the subclass definition is used.
Parametrics are generic typing procedures.
Dynamic (or late) method binding is the ability of a program to resolve references to subclass methods at runtime. For example assume that three subclasses (Cow, Dog and Snake) have been created based on the Animal abstract class, each having their own speak() method. Although each method reference is to an Animal (but no animal objects exist), the program is will resolve the correct method reference at runtime.
Here the method demo() is overloaded 3 times: first method has 1 int parameter, second method has 2 int parameters and third one is having double parameter. Which method is to be called is determined by the arguments we pass while calling methods. This happens at compile time.
so this type of polymorphism is known as compile time polymorphism.
Here we have 4 versions of same method add. We are overloading the method add() here.
Here we have two classes Human and Boy. Both the classes have same method walk() but the method is static, which means it cannot be overriden so even though I have used the object of Boy class while creating object obj, the parent class method is called by it. Because the reference is of Human type (parent class). So whenever a binding of static, private and final methods happen, type of the class is determined by the compiler at compile time and the binding happens then and there.
this is the same example that we have seen above. The only difference here is that in this example, overriding is actually happening since these methods are not static, private and final. In case of overriding the call to the overriden method is determined at runtime by the type of object thus late binding happens. Lets see an example to understand this:
Here we have two classes Human and Boy. Both the classes have same method walk() but the method is static, which means it cannot be overriden so even though I have used the object of Boy class while creating object obj, the parent class method is called by it. Because the reference is of Human type (parent class). So whenever a binding of static, private and final methods happen, type of the class is determined by the compiler at compile time and the binding happens then and there.