Java Polymorphism

Polymorphism means many forms and it is one of the key concepts of Object oriented programming in java.

As you all know, in the Inheritance section we have discussed that the sub classes have their own unique features and also shares some of the common features in their parent class. Sub classes do override some of the functionality of their parent’s. We will learn the behavior of polymorphism using our HumanBeing example below.


Polymorphism:

It is categorized as 3 types as mentioned below.

  1. Method Overloading
  2. Method Overriding
  3. Dynamic or late binding

Since we have already discussed about Method overriding in our previous chapter, we shall see the other two in this chapter.

Method Overloading:

Method overloading is when different methods within the same class will have the same name but different signatures(parameters). So if we need to call the correct method, the exact signatures should match with the arguments. In the below program all three methods have the same name as ‘walk’, but however their parameters change.

Sample Program:


public class HumanBeing {
	
	public static void main(String[] args) {
		
		HumanBeing dummy = new HumanBeing();
		dummy.walk();
		dummy.walk(true);
		dummy.walk(true, true);		
	}
	
	protected void walk(){
		System.out.println("I am simply walking");		
	}
	
	protected void walk(boolean strt){
		System.out.println("I am walking straight");
	}
	
	protected void walk(boolean strt, boolean speak){
		System.out.println("I am speaking as well as walking straight");
	}

}

Output:

I am simply walking
I am walking straight
I am speaking as well as walking straight

Similar to method overloading, we can also do the same for the constructor. It is said to be constructor overloading.

Constructor Overloading:

So when we create the objects, we can call the correct constructor by using the correct arguments.

Sample Program:

Male.java


public class Male extends HumanBeing {
	
	Male(){
		System.out.println("I am a normal guy.");		
	}
	
	Male(int height){
		System.out.println("I am a normal guy with height - " + height + " cms.");
	}
	
	Male(int height, int age){
		System.out.println("I am a normal guy with height - " + height + " cms, " + "and age - " + age);
	}
}

HumanBeing.java


public class HumanBeing {
	
	public static void main(String[] args) {
		
		Male john = new Male();
		Male mike = new Male(183);
		Male jason = new Male(180, 20);				
	}
}

Output:

I am a normal guy.
I am a normal guy with height – 183 cms.
I am a normal guy with height – 180 cms, and age – 20

Dynamic or Late Binding:

It is defined as the execution of actual method by the Java Virtual Machine(JVM) determined by the type of the object and not the type of the reference. This is also called as virtual method invocation. We shall see this in our below example.

Sample Program:

HumanBeing.java – I am having a variable of type HumanBeing and creating a male object using it. Now i am calling the sing method using the object. The JVM automatically calls the sing method in the Male class.


public class HumanBeing {
	
	public static void main(String[] args) {
		
		HumanBeing john;
		john = new Male();
		john.sing();
		
	}	
	
	public void sing() {
		System.out.println("I am singing.");		
	}
}

Male.java – Sing method is overrided here to have its own functionality.


public class Male extends HumanBeing {
	
	public void sing(){
		super.sing();
		System.out.println("Hi I am a male singer.");		
	}	
}

Output:

I am singing.
Hi I am a male singer.

Posted on July 1, 2014 in Java for Beginners

Share the Story

Leave a reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Back to Top