OOP Lecture13 POLYMORPHISM
OOP Lecture13 POLYMORPHISM
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.
A reference variable can refer to any object of its declared type or any
subtype of its declared type. A reference variable can be declared as a class
or interface type.
Example:
Let us look at an example.
Now, the Deer class is considered to be polymorphic since this has multiple
inheritance. Following are true for the above example:
Animal a = d;
Vegetarian v = d;
Object o = d;
All the reference variables d,a,v,o refer to the same Deer object in the
heap.
Virtual Methods:
In this section, I will show you how the behaviour of overridden methods in
Java allows you to take advantage of polymorphism when designing your
classes.
System.out.println("Constructing an Employee");
this.name = name;
this.address = address;
this.number = number;
return name;
return address;
address = newAddress;
return number;
salary)
{
setSalary(salary);
return salary;
salary = newSalary;
return salary/52;
Now, you study the following program carefully and try to determine its
output:
{
public static void main(String [] args)
s.mailCheck();
e.mailCheck();
Constructing an Employee
Constructing an Employee
Call mailCheck using Salary reference --
Within mailCheck of Salary class
ailing check to Mohd Mohtashim with salary 3600.0