Java Tut – Polymorphism

Polymorphism the most common use of OOP occurs when a parent class reference is used to refer to a child class object.
Any Java object that can pass more than one IS-A test is considered to be polymorphic.In java all java objects are polymorphic since any object will pass the IS-A test for their own type and for the class object.
It is important to know that only possible way to access an object is through a reference variable.A reference varibale can only be one type.Once declared, the type of a reference variable cannot be changed.

A reference variable can be declared as a class or interface type

Let us look an example

public interface Vergetarian{
}
public class Animal{}
public class Deer extends Animal implements Vegetarian{}

Now the Deer class is considered to be polymorphic since this has multiple inheritance.Following are true for the above example:

Virtual Methods:
In this section,I will show you how the behavious of overriden methods in java allows you to take advantage of poly

Employees.java

public class Employees {
private String name;
private String address;
private int number;
public Employees(String name,String address,int number)
{
	System.out.println("Constructing an employee");
	this.name=name;
	this.address=address;
	this.number=number;
}
public void mailCheck(){
	System.out.println("Mailing a check to "+this.name+" "+this.address);
}
public String tooString(){
	return name+" "+address+" "+number;
}
public String getName()
{
	return name;
}
public String getAddress(){
	return address;
}
public void setAddress(String newAddress){
this.address=newAddress;
}
public int getNumber()
{
	return number;
}
}

Salary.java

public class Salary extends Employees
{
  private double salary;//Annual salary
  public Salary(String name,String address,int number,double salary)
  {
	  super(name,address,number);
	  setSalary(salary);
  }
  public void mailCheck()
  {
	  System.out.println("Within mailCheck of Salary class");
	  System.out.println("Mailing check to "+getName()+" with salary "+salary);
  }
  
  public double getSalary()
  {
	  return salary;
  }
  public void setSalary(double newSalary)
  {
	  if(newSalary>0.0)
	  {
		  salary=newSalary;
	  }
  }
  public double computePay(){
	  System.out.println("Computing salary pay for "+getName());
	  return salary/52;
  }
}

VirtualDemo.java

public class VirtualDemo {
public static void main(String[] args)
{
	Salary s=new Salary("Mohd Mohtasim", "Ambehta, UP", 3, 3600.00);
	Employees e=new Salary("John Adams","Boston, MA", 2, 2400.0);
	System.out.println("Call mailCheck using salary reference");
	s.mailCheck();
	System.out.println("\n Call mailcheck using Employee reference");
	e.mailCheck();
}
}

 

Output:

Constructing an employee
Constructing an employee
Call mailCheck using salary reference
Within mailCheck of Salary class
Mailing check to Mohd Mohtasim with salary 3600.0

 Call mailcheck using Employee reference
Within mailCheck of Salary class
Mailing check to John Adams with salary 2400.0

References:
http://www.tutorialspoint.com/java/java_polymorphism.htm
http://www.javatpoint.com/runtime-polymorphism-in-java

It would be a great help, if you support by sharing :)
Author: zakilive

Leave a Reply

Your email address will not be published.