Quick Intro to Polymorphism with Java.

      No Comments on Quick Intro to Polymorphism with Java.

Polymorphism is the capability of an action or method to do different things based on the object that it is acting upon. This is the third basic principle of object oriented programming. Overloading and overriding are two types of polymorphism. In Java, the type of variable does not completely determine the type of the object to which it refers. For example, a variable of type BankAccount can hold a reference to an actual BankAccount object or a subclass object such as SavingsAccount. What happens when you invoke a method? for example:

BankAccount anAccount = new CheckingAccount();

anAccount.deposit(1000);

Which deposit method is called? The anAccount parameter has Type BankAccount, so it would appear as if BankAccount class proviedes its own deposit method that updates transaction count. The anAccount field actually refers to an object of the subclass checking Account. so it would be appropriate if the checkingAccount.deposit method were called instead.

In Java, method calls are always determined by the type of the actual object, not the type of the object reference. That is, if the actual object has the type CheckingAccount, then the CheckingAccount.deposit method is called. It does not matter that the object reference is stored in a field of type BankAccount. The ability to refer to objects of multiple types with varying behavior is called polymorphism.

Have a look at the following code and notice that the showPay() method is completely polymorphic as it doesn’t care what kind of worker it is given, it always compute the correct pay.

public class WorkerTester {
private static void showPay(Worker w, double hours)
{
System.out.println(w.getName()+": "+w.computePay(hours));
}
public static void main(String[] args)
{
showPay(new SalariedWorker("John", 40), 35.5);
showPay(new SalariedWorker("Mary", 40), 45);
showPay(new HourlyWorker("Tom", 40), 37.25);
showPay(new HourlyWorker("Jim", 40), 57);
}
}

Worker Class

public class Worker {
//variables
double rate;
String name;
double weekSalary;
double hours;
//construct worker that takes in name and salary
public Worker(String name, double rate)
{
this.name = name;
this.rate = rate;
}
//method to compute week pay
public double computePay(double hours)
{
this.weekSalary = this.rate * 40;
return this.weekSalary;
}
//getters
public String getName()
{
return name;
}
}

HourlyWorker Class

public class HourlyWorker extends Worker{
//construct HourlyWorker that takes name and rate
public HourlyWorker(String name, double rate)
{
super(name, rate);
}
//method for calculating weekly pay
@Override
public double computePay(double hours)
{
if(hours <= 40)
{
this.weekSalary = this.rate * hours;
}
else
{
this.weekSalary = this.rate * 40;
double excess = hours - 40;
double overTime = (excess * this.rate) * 1.5;
this.weekSalary += overTime;
}
return this.weekSalary;
}
@Override
public String getName()
{
return super.getName();
}
}

SalariedWorker Class

public class SalariedWorker extends Worker{
//construc SalariedWorker that takes in name and rate
public SalariedWorker(String name, double rate)
{
super(name, rate);
}
//method to compute week pay
@Override
public double computePay(double hour)
{
return super.computePay(hour);
}
@Override
public String getName()
{
return super.getName();
}
}
Share This!

Leave a Reply

Your email address will not be published.