wellnwill Marathahalli Bangalore

Polymorphism

The basic meaning of polymorphism is “one thing in many form”. The word poly means many and morphism means forms.

There are two types of Polymorphism:

  • Compile time Polymorphism
  • Runtime Polymorphism

Compile time Polymorphism

When the object type is determined by the compiler at compile time, it is known as compile time polymorphism or static binding or early binding. Ex-method overloading

Runtime Polymorphism

When the object type is determined at run time, it is known as runtime polymorphism or dynamic binding or late binding. Ex-method overriding

Method Overriding

  • If the child class is not satisfied with the parent class method implementation then it is possible to redefine that method in the child class based on child class requirement. This process is known as method overriding.
  • Inheriting a method from parent class to child class and changing the implementation of inherited method in child class according to child class requirement is known as method overriding.
  • The parent class method which will be overridden is called overridden method and child class method which is overriding is called overriding method.

Rules for method overriding

  • For method overriding, “is a” i.e. inheritance relationship is mandatory.
  • In the overriding concept the child class and parent class method name, method parameters (type of parameters, numbers of parameters and order of parameters) and return type of method must be same.
  • We cannot override private method because private method is not accessible from outside class. If we are writing same method in child class, it will be treated as newly created method in child class.
  • We cannot override final method of parent class in child class even final methods are inherited in sub class.
  • We can override non-final method of parent class as final method in child class.
  • We have to override parent class abstract method in child class to provide implementation if we are not providing implementation then make that method as abstract in child class also.
  • Non-abstract method of parent class can be overridden as abstract method in child class. The advantage of this approach is we can stop the availability of parent class method implementation to the next level child classes.
  • We cannot override static method of parent class as non-static method in child class.
  • We cannot override non static method of parent class as static method in child class.
  • We cannot override static method because static method is bound with class whereas non static method is bound with object. If we are writhing same method in child class, it is data hiding not overriding.

Covariant return type

From java 1.5, if parent class method return type is class type then while overriding child class method return type can be same as parent class type or its child class type. This concept is known as covariant return type.

Method Hiding vs Method Overriding

Method Hiding Method Overriding
Both parent class method and child class method should be static. Both parent class method and child class method should be non-static.
Connecting to method call to method body will be resolved by compile at compile time based on reference type. Connecting to method call to method body will be resolved by JVM at run time based on run time object.
It is also known as compile time polymorphism or early binding or static binding. Ex-method overloading It is also known as run time polymorphism or late binding or dynamic binding. Ex-method overriding

Method Overriding with Access Modifier

When we override parent class method in child class, the access modifier must be same or higher then in term of scope (accessibility) or in other word access modifier of child class should be less restrictive then parent class method.

Accessibility of modifier in increasing order

Parent class method access modifier Child class method access modifier
private private, default, protected, public (no overriding concept, method will be treated as new method of child class).
default default, protected, public
protected protected, public
public public

How to achieve Runtime time Polymorphism

  • The reference variable of super class can be used to hold the object of its subclass. This process is known as upcasting.
  • At compile time, compiler treats the method call based on reference type but method call will be resolved by JVM at run time based on run time object.
  • We can hold child class object reference into parent class reference variable but we cannot call newly created methods of child class by using that reference.
  • If we want to call sub class method then down casting is required.
class Parent
{
void saving()
{
 System.out.println("Parent's saved money");
}
void lifeStyle()
{
 System.out.println("eat, sleep, work");
}}
class Child extends Parent
{
void lifeStyle()
{
 System.out.println("eat, sleep, work, play, party");
}

void keepLearning()
{
 System.out.println("keep Learning");
}
}
class Test195
{
 public static void main(String args[])
 {
  Parent p1=new Child();
  p1.keepLearning();
 }
}
output:O/P-CTE- cannot find symbol > p1.keepLearning 
class Parent
{
void saving()
{
 System.out.println("Parent's saved money");
}
void lifeStyle()
{
 System.out.println("eat, sleep, work");
}
}
class Child extends Parent
{
void lifeStyle() // overriding method
{
 System.out.println("eat, sleep, work, play, party");
}
void keepLearning()
{
 System.out.println("keep Learning");
}
}
class Test212
{
 public static void main(String args[])
 {
  Parent p1=new Child();
  p1.lifeStyle();
  } 
}
//Output:eat, sleep, work, play, party
Footer with Map

Top