How to Java programming- Polymorphism

The process of representing one form in multiple forms is known as Polymorphism. It is not a programming concept but it is one of the principals of OOPs. For many objects oriented programming language polymorphism principle is common but whose implementations are varying from one objects oriented programming language to another object-oriented programming language. Before you continue, it’s recommended to check the previous guide

Java Polymorphism


In java language, polymorphism is essentially considered into two versions.

1.Compile time polymorphism (static binding or method overloading).
2.Runtime polymorphism (dynamic binding or method overriding).


1.Compile-time polymorphism

In compile-time polymorphism, a method’s behavior is bound to the definition at the time of method compilation by the compiler. This type of binding is known as static binding, and it cannot be rebound. Example: method overloading.

Method Overloading
	class Calculation {  
	  void sum(int a,int b){System.out.println(a+b);}  
	  void sum(int a,int b,int c){System.out.println(a+b+c);}  

	  public static void main(String args[]) {  
		Calculation obj=new Calculation();  
		obj.sum(10,10,10);  // 30
		obj.sum(20,20);     //40 


2.Runtime polymorphism

Run-time polymorphism can be achieved by fulfilling the following

a) Inheritance b) Method overriding and c) Upcasting
Method overriding: Child class has the same method as of base class. In such cases, child class overrides the parent class method without even touching the source code of the base class. This feature is known as method overriding.

In below example we create two class Person an Employee, Employee class extends Person class override walk() method. We are calling the walk() method by the reference variable of the Parent class. Since it refers to the subclass object and subclass method overrides the Parent class method, subclass method is invoked at runtime. Here method invocation is determined by the JVM, not a compiler, So it is known as runtime polymorphism.

Runtime polymorphism
	class Person{
		void walk(){
			System.out.println("Can Run....");
	class Employee extends Person{
		void walk(){
			System.out.println("Running Fast...");
		public static void main(String arg[]){
			Person p=new Employee(); //upcasting


     Running Fast...


Anwar Yakkiparamban

Anwar Yakkiparamban is the founder of Lauyou Learning. Prior to Lauyou learning, Anwar worked at ARD Engineering & Development, Qatar. He holds bachelor degree in Electronics and Communication Engineering from Govt. Engineering College Idukki.

You may also like...