In the last tutorial we discussed Polymorphism in Java. In this guide we will see types of polymorphism. There are two types of polymorphism in java:
1) Static Polymorphism also known as compile time polymorphism
2) Dynamic Polymorphism also known as runtime polymorphism
Compile time Polymorphism (or Static polymorphism)
Polymorphism that is resolved during compiler time is known as static polymorphism. Method overloading is an example of compile time polymorphism.
Method Overloading: This allows us to have more than one method having the same name, if the parameters of methods are different in number, sequence and data types of parameters. We have already discussed Method overloading here: If you didn’t read that guide, refer: Method Overloading in Java
Example of static Polymorphism
Method overloading is one of the way java supports static polymorphism. Here we have two definitions of the same method add() which add method would be called is determined by the parameter list at the compile time. That is the reason this is also known as compile time polymorphism.
class SimpleCalculator { int add(int a, int b) { return a+b; } int add(int a, int b, int c) { return a+b+c; } } public class Demo { public static void main(String args[]) { SimpleCalculator obj = new SimpleCalculator(); System.out.println(obj.add(10, 20)); System.out.println(obj.add(10, 20, 30)); } }
Output:
30 60
Runtime Polymorphism (or Dynamic polymorphism)
It is also known as Dynamic Method Dispatch. Dynamic polymorphism is a process in which a call to an overridden method is resolved at runtime, thats why it is called runtime polymorphism. I have already discussed method overriding in detail in a separate tutorial, refer it: Method Overriding in Java.
Example
In this example we have two classes ABC and XYZ. ABC is a parent class and XYZ is a child class. The child class is overriding the method myMethod() of parent class. In this example we have child class object assigned to the parent class reference so in order to determine which method would be called, the type of the object would be determined at run-time. It is the type of object that determines which version of the method would be called (not the type of reference).
To understand the concept of overriding, you should have the basic knowledge of inheritance in Java.
class ABC{ public void myMethod(){ System.out.println("Overridden Method"); } } public class XYZ extends ABC{ public void myMethod(){ System.out.println("Overriding Method"); } public static void main(String args[]){ ABC obj = new XYZ(); obj.myMethod(); } }
Output:
Overriding Method
When an overridden method is called through a reference of parent class, then type of the object determines which method is to be executed. Thus, this determination is made at run time.
Since both the classes, child class and parent class have the same method animalSound
. Which version of the method(child class or parent class) will be called is determined at runtime by JVM.
Few more overriding examples:
ABC obj = new ABC(); obj.myMethod(); // This would call the myMethod() of parent class ABC XYZ obj = new XYZ(); obj.myMethod(); // This would call the myMethod() of child class XYZ ABC obj = new XYZ(); obj.myMethod(); // This would call the myMethod() of child class XYZ
In the third case the method of child class is to be executed because which method is to be executed is determined by the type of object and since the object belongs to the child class, the child class version of myMethod() is called.
Pinky says
public static add(int a,int b)
{
}
public int add(int a)
{
}
this is overloading or not?
rajajisubramanian says
no its not a method overloading
public static add(int a,int b)-its like a constructor description so its not a method and if its a constructor means the constructor wont be a static and its violate the inheritance rules in oops so the method name may or may not be have the return type, those method only be used in the method overloading,
abhi says
no it is not overloading … you will get compile time Error overloading can happen only either between static method or non static method..
because non static is instance member and static is class member
Shivam says
There will be no compile time error
It can be called as
A.add(2,3); // A is name of the class
a.add(2); // a is reference to object of class A
Compile time error will only occur when number and type of arguments are same.
Rajesh says
Yes , it will overload. don’t think about static or non static. its just because of missing return type for the above static method and respected returning values.other wise rest of thing works fine.
INDnrj says
Yes of course! static members take part in overloading but not in overriding
kiran kishore barik says
no no that is not a overloading method . static method is a class level method , where as plain method is a object level method , so that both method is not same .
Rahul says
it will be not overload because overloading concept will be fulfil when both would be same like both method should be static or non-static.
santhosh says
can any of you say what are built in functions for class and objects????
Rohit says
I dont think built in is the right phrase.
But you have Object class functions which are available to any newly created Class whose object’s can take advantage of. Ex wait(), notify(), equals(), etc.
manish says
May i know what is runtime polymorphism?
because all i only can see here is to how to achieve runtime and compile time polymorphism.
Shashi says
The method overriding is an example of runtime polymorphism.
Mona says
A compile time polymorphism is handled during compilation, when the program is compiled, hence “compile-time”.
A runtime polymorphism is handled “live” when the program is “run”, hence “run-time”
Neha says
what is the difference between static and dynamic polymorphism?
Saadat says
Dynamic (run time) polymorphism is the polymorphism existed at run-time. Here, Java compiler does not understand which method is called at compilation time. Only JVM decides which method is called at run-time. Method overloading and method overriding using instance methods are the examples for dynamic polymorphism.