Polymorphism
Polymorphism
between entities with the same name efficiently. This is done by Java with the help of the signature and declaration
of these entities.
Example:
// Overloaded sum().
// This sum takes two int parameters
public int sum(int x, int y) {
return (x + y);
}
// Overloaded sum().
// This sum takes three int parameters
public int sum(int x, int y, int z) {
return (x + y + z);
}
// Overloaded sum().
// This sum takes two double parameters
public double sum(double x, double y) {
return (x + y);
}
1. Overloading –
➢ It allows different methods to have the same name, but different signatures. It can be done by -
• The number of parameters in two methods.
• The data types of the parameters of methods.
• The order of the parameters of methods.
➢ We cannot overload by return type. This behavior is same in C++.
➢ We cannot overload two methods in Java if they differ only by static keyword (number of parameters
and types of parameters is same).
➢ Overridden method in child class has the same name, same parameters, and same return type as a
method in its super-class.
➢ Method overriding is one of the way by which java achieve Run Time Polymorphism.
➢ The version of a method that is executed will be determined by the object that is used to invoke it.
➢ If an object of a parent class is used to invoke the method, then the version in the parent class will be
executed and vice-versa for object of child class.
➢ In other words, it is the type of the object being referred to (not the type of the reference variable) that
determines which version will be executed.
➢ The access modifier for an overriding method can allow more, but not less, access than the overridden
method. Doing so (providing less access), will generate compile-time error. For example, a protected method in
the super-class can be made public, but not private, in the subclass.
➢ Final methods can not be overridden. If we don’t want a method to be overridden, we declare it as final.
➢ When you define a static method with same signature as a static method in base class, it is known
as method hiding (i.e. static methods can’t be overridden).
➢ Summary of what happens when we define a method with same signature in child class in different
cases:-
SUPERCLASS INSTANCE METHOD SUPERCLASS STATIC METHOD
SUBCLASS INSTANCE METHOD Overrides Generates a compile-time error
SUBCLASS STATIC METHOD Generates a compile-time error Hides
➢ Example of Method Hiding:-
class Parent {
// Static method in base class which will be hidden in subclass
static void m1() {
System.out.println("From parent " + "static m1()");
}
➢ Private methods can not be overridden. It is so because private methods are bonded during compile
time (No Run Time Polymorphism) and it is the type of the reference variable – not the type of object that it
refers to – that determines what method to be called. This behavior is different from C++. In C++, we can have
virtual private methods.
➢ The overriding method must have same return type (or subtype).
➢ We can call parent class method inside the definition of overriding method using super keyword. Example
void show() {
super.show(); // Calling Overridden Parent Class Method.
System.out.println("Child's show()");
}
➢ We can not override constructor as parent and child class can never have constructor with same name.
➢ Abstract methods in an interface or abstract class must be overridden in derived concrete classes
otherwise a compile-time error will be thrown.
➢ The presence of synchronized/strictfp modifier with method have no effect on the rules of overriding.
➢ In C++, we need virtual keyword to achieve overriding or Run Time Polymorphism. In Java, methods
are virtual by default.
➢ We can have multilevel method-overriding i.e. GrandChildren can also override original Parent’s methods.
Overloading Overriding
1. Example of Compile-time Polymorphism. 1. Example of Run-time Polymorphism.
2. It is about same method have different 2. It is about same method, same signature
signatures. but different classes connected through
inheritance.
Run Time Polymorphism (Dynamic Method Dispatch) – It is the mechanism by which a call to an
overridden method is resolved at run time by JVM, rather than compile time.
Upcasting - A superclass reference variable can refer to a subclass object. Java uses this fact to resolve
calls to overridden methods at run time.
Example:
// Inside main() method
Parent obj = new Child(); // Reference of Parent class is referring to object of Child Class.
➢ When an overridden method is called through a superclass reference, Java determines which
version(superclass/subclasses) of that method is to be executed based upon the type of the object being
referred to at the time the call occurs. Thus, this determination is made at run time.
➢ In Java, we can override methods only, not the variables(data members), so runtime
polymorphism cannot be achieved by data members.
Example:
// Java program to illustrate the fact that runtime polymorphism cannot be achieved by data members
class A {
int x = 10;
}
class B extends A {
int x = 20;
}
public class Test {
public static void main(String args[]) {
A a = new B(); // object of type B
System.out.println(a.x); // Data member of class A will be accessed
}
}
Output:
10
Advantages of Dynamic Method Dispatch -
• Dynamic method dispatch allow Java to support overriding of methods which is central for run
time polymorphism.
• It allows a class to specify methods that will be common to all of its derivatives, while allowing
subclasses to define the specific implementation of some or all of those methods.
• It also allow subclasses to add its specific methods subclasses to define the specific
implementation of some.
Compile Time Polymorphism – In this an object is bound with their functionality at the compile-time.
Also k/a or static or early binding.