Polymorphism
is the ability of an object to take on many forms. The most common use
of polymorphism in OOP occurs when a parent class reference is used to
refer to a child class object.
Following example Shows the how to achieve Polymorphism.
*************************************************
public class PolyMorphismSample2 {
private String name;
private String address;
private int number;
public PolyMorphismSample2(String name, String address, int number) {
System.out.println("Constructing an Employee");
this.name = name;
this.address = address;
this.number = number;
}
public void polyMethod() {
System.out.println("PolyMethod to PolyMorphismSample2" + this.name + " "
+ this.address);
+ this.address);
}
public String toString() {
System.out.println("toString");
return name + " " + address + " " + number;
}
public String getName() {
System.out.println("getName of PolyMorphismSample2");
return name;
}
public String getAddress() {
return address;
}
public void setAddress(String newAddress) {
address = newAddress;
}
public int getNumber() {
return number;
}
}
**********************************************************
public class PolyMorphismSample1 extends PolyMorphismSample2 {
private double salary; // Annual salary
public PolyMorphismSample1(String name, String address, int number, double
salary) {
salary) {
super(name, address, number);
setSalary(salary);
}
public void polyMethod() {
System.out.println("PolyMethod to PolyMorphismSample1" + getName() + "
with salary " + salary);
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;
}
}
**************************************************
public class PolymorphismSampleDemo {
public static void main(String[] args) {
PolyMorphismSample1 s = new PolyMorphismSample1("Mohd Mohtashim",
"Ambehta, UP", 3, 3600.0);
"Ambehta, UP", 3, 3600.0);
PolyMorphismSample2 e = new PolyMorphismSample1("John Adams",
"Boston, MA", 2, 2400.0);
"Boston, MA", 2, 2400.0);
System.out.println("Call PolyMethod using Salary reference --");
s.polyMethod();
System.out.println("\n Call PolyMethod using Employee reference--");
e.polyMethod();
e.toString();
}
}
O/P:---
Constructing an Employee
Constructing an Employee
Call PolyMethod using Salary reference --
getName of PolyMorphismSample2
PolyMethod to PolyMorphismSample1Mohd Mohtashim with salary 3600.0
Call PolyMethod using Employee reference--
getName of PolyMorphismSample2
PolyMethod to PolyMorphismSample1John Adams with salary 2400.0
toString
************************************************
Here s is reference of PolyMorphismSample1 and e is reference of PolyMorphismSample2 but s.polyMethod() and e.polyMethod() calls the method of PolyMorphismSample1. Where as e.toString() calls the method in PolyMorphismSample2. Why because we are assignig the child class object to the reference of parent class and we override the polyMethod() method but toString() is not overriden method. So polyMethod() of PolyMorphismSample1 is called using reference of PolyMorphismSample2 i.e e.In this way can achieve Polymorphism.
************************************************
No comments:
Post a Comment