Home » Java programming language

Polymorphism in Java with Example

Learn: Polymorphism in Java, this article will explain about Polymorphism. In Java and the concept of Dynamic Method Dispatch (DMD).
Submitted by Mayank Singh, on June 11, 2017

Prerequisite: Method Overriding in Java

As we know,
In Java we have the concept of Inheritance, the features of Parent Class can be inherited/extended to the Child Class, using that concept we can understand, what is Method Overriding in Java? We have already seen the example of method overriding in this article: Inheritance in Java with Example. We discussed that methods with private data members cannot be overridden, we come to same example again and discuss the use of Super Keyword.

Consider the program:

import java.util.Scanner;
class Headquarters
{
	int totalemployees; // Data Member 1
	String cityname; // Data Member 2
	Scanner KB=new Scanner(System.in);
	void getDetails()
	{
		System.out.println("Enter City Where Headquarters is Sitiuated :");
		cityname=KB.nextLine();
		System.out.println("Enter Total Number of Employees in Headquarters");
		totalemployees=KB.nextInt();
	}
	void showDetails()
	{
		System.out.println("Company Headquarters is Sitiuated in "+cityname+" and has "+totalemployees+" Employees");
	}
}

class Mainbranch extends Headquarters
{	
	int totalMBemployees;
	String citynameMB;
	
	void getDetails()
	{  
		System.out.println("Headquarters:");
		super.getDetails();
		System.out.println("Main Branch:");
		System.out.println("Enter City Where Main Branch is Sitiuated");
		KB.nextLine();//to understand why we used this statement visit my first article at this LINK
		citynameMB=KB.nextLine();
		System.out.println("Enter The Total Number of Employees In Main Branch");
		totalMBemployees=KB.nextInt();
	}
	
	void showDetails()
	{	
		System.out.println("Headquarters:");
		super.showDetails();
		System.out.println("Main Branch:");
		System.out.println("Company Main Branch is Sitiuated in "+citynameMB+" and has "+totalMBemployees+" Employees");
	}
}

class Company
{
	public static void main(String args[])
	{
			
		Mainbranch M=new Mainbranch();//only the inherited class was instantiated and we now invoke the getDetails() and showDetails() method of the Headquarters class with the help of Super Keyword
		M.getDetails(); //When this method is called, first it will invoke the getDetails() method of Headquarters and then will progress to the Mainbranch class.
		M.showDetails();//Similary , first this method will show the details of Headquarters Class and then it will progress to the Mainbranch class.
	}
}

Output

Headquarters:
Enter City Where Headquarters is Sitiuated :
Delhi
Enter Total Number of Employees in Headquarters
1500
Main Branch:
Enter City Where Main Branch is Sitiuated
Indore
Enter The Total Number of Employees In Main Branch
500
Headquarters:
Company Headquarters is Sitiuated in Delhi and has 1500 Employees
Main Branch:
Company Main Branch is Sitiuated in Indore and has 500 Employees

NOTE: that in the above program we are using two Reference Variables for calling the same method name: Super and M but this type of program cannot be considered to be following Polymorphism.

Polymorphism is generally referred to as accessing the Child Class Methods with the Base Class Reference, we will discuss this type of polymorphism in the coming article, this type of polymorphism is known as Run Time Polymorphism and is achieved with of help of programming mechanism known as Dynamic Method Dispatch (DMD).

Before diving into Dynamic Method Dispatch please read the use of abstract keyword in Java Programming through this link: Abstract Classes in Java with Example.


Consider the Program for DMD

import java.util.Scanner;

abstract class Shape
{
	Scanner KB=new Scanner(System.in);
	abstract void getDimensions();
	abstract void showArea();
}

class Reactangle extends Shape
{
	private double length, breadth,area;
	void getDimensions()
	{  	
		System.out.println("Enter Length of Rectangle");
		length=KB.nextDouble();
		System.out.println("Enter Breadth of Rectangle");
		breadth=KB.nextDouble();
	}

	void showArea()
	{
		System.out.println("Length of Reactangle:"+length);
		System.out.println("Breadth of Reactangle:"+breadth);
		System.out.println("Area of the Rectangle is:"+length*breadth);
	}
}

class Circle extends Shape
{
	private double radius,area;
	void getDimensions()
	{		
		System.out.println("Enter Radius of Circle");
		radius=KB.nextDouble();
	}
	void showArea()
	{		
		System.out.println("Radius of the Circle is:"+radius);
		System.out.println("Area of Circle :"+3.14*radius*radius);	
	}
}

class Triangle extends Shape
{
	private double baselength, height,area;
	void getDimensions()
	{
		System.out.println("Enter Base Length of Triangle");
		baselength=KB.nextDouble();
		System.out.println("Enter height of Triangle");
		height=KB.nextDouble();
	}

	void showArea()
	{
		System.out.println("Base Length of Triangle:"+baselength);
		System.out.println("Height of Triangle:"+height);
		System.out.println("Area of the Rectangle is:"+0.5*baselength*height);			
	}
	
	
}

class DMDShape
{
	public static void main(String args[])
	{
		Shape S;//will not be instantiated
		Reactangle R=new Reactangle();
		S=R;
		S.getDimensions();
		S.showArea();
		Circle C=new Circle();
		S=C;
		S.getDimensions();
		S.showArea();
		Triangle T=new Triangle();
		S=T;
		S.getDimensions();
		S.showArea();
		
	}	
}

Output

Enter Length of Rectangle
10
Enter Breadth of Rectangle
5
Length of Reactangle:10.0
Breadth of Reactangle:5.0
Area of the Rectangle is:50.0
Enter Radius of Circle
5
Radius of the Circle is:5.0
Area of Circle :78.5
Enter Base Length of Triangle
4
Enter height of Triangle
1
Base Length of Triangle:4.0
Height of Triangle:1.0
Area of the Rectangle is:2.0

In the above program, we have seen that the methods which were defined in the Shape class were of abstract type and the Shape Class was inherited in the Rectangle, Circle and Triangle class, it is important to note that abstract methods are to be redefined in the derived classes and abstract methods must not contain body structures in the base class, thus in the main method we did not instantiate the Shape Class object as that is the main point which has to be remembered while using abstract classes, with the help of above program we have practiced the concept of Run Time Polymorphism.



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.