Lecture 12 Polymorphism
Lecture 12 Polymorphism
Polymorphism
Contact Info:
Room No: BS-04, CED
1 Email: [email protected]
Course Books
Text Book:
Herbert Schildt, Java: The Complete Reference, 12th
Edition ,McGraw-Hill Education.
Deitel, Paul, Java How to Program, 11th Edition, Pearson,
2017
Reference Books:
Horton, Ivor, Beginning Java, 7th Edition, Wrox, 2011
2
Course Instructors
20
30
Polymorphism
Types of Polymorphism
Run time Polymorphism
Compile time Polymorphism
Upcasting
Java Runtime Polymorphism Example:
Bank
Java Runtime Polymorphism with Data
Member
Java Runtime Polymorphism with
5
Multilevel Inheritance
6 Polymorphism
Polymorphism is one of the object oriented programming feature that
allows us to perform a single action in different ways.
For example, lets say we have a class Animal that has a method
sound(). Since this is a generic class so we can’t give it a
implementation like: Roar, Meow, Oink etc. We had to give a generic
message..
7 Polymorphism Example
# Example 01
and
# Example 01
public class Cat extends Animal{
...
@Override
public void sound(){
System.out.println("Meow");
}
}
9 Polymorphism
As you can see that although we had the common action for all
subclasses sound().
But there were different ways to do the same action. This is a perfect
example of polymorphism
Feature that allows us to perform a single action in different ways
10 Polymorphism
It would not make any sense to just call the generic sound() method
As each Animal has a different sound.
Thus we can say that the action this method performs is based on the
type of object.
11 Polymorphism in Programming
Polymorphism is the capability of a method to do different things
based on the object that it is acting upon.
In other words, polymorphism allows you define one interface and
have multiple implementations.
As we have seen in the above example that we have defined the
method sound() and have the multiple implementations of it in the
different-2 sub classes.
12 Polymorphism in Programming
Which sound() method will be called is determined at runtime so the
example we gave above is a runtime polymorphism example.
UML Diagram: Rectangle
13
class Box
public double area()
{
return 2 * (getLength() * getWidth()
+ getLength() * height
+ getWidth() * height);
}
final Methods
17
public Box()
{
super();
height = 0;
}
class A{}
class B extends A{}
A a=new B();//upcasting
p instanceof BoxShape
33 Upcasting
class Bike{ In this example,
void run()
{System.out.println("running");}
we are creating
} two classes Bike
class Splendor extends Bike{
void run() and Splendor.
{System.out.println("running safely Splendor class
with 60km");}
extends Bike
public static void main(String args[]) class and
{
Bike b = new overrides its run()
Splendor();//upcasting
b.run();
method.
} We are calling
}
the run method
by the reference
variable of Parent
34 Upcasting
class Bike{ Since it refers to
void run()
{System.out.println("running");}
the subclass
} object and
class Splendor extends Bike{
void run() subclass method
{System.out.println("running safely overrides the
with 60km");}
Parent class
public static void main(String args[]) method, the
{
Bike b = new subclass method
Splendor();//upcasting
b.run();
is invoked at
} runtime.
} Since method
invocation is
determined by
35 Upcasting
class Bike{ Since method
void run()
{System.out.println("running");}
invocation is
} determined by
class Splendor extends Bike{
void run() the JVM not
{System.out.println("running safely compiler, it is
with 60km");}
known as
public static void main(String args[]) runtime
{
Bike b = new polymorphism.
Output:
Splendor();//upcasting
b.run(); running safely with
} 60km.
}
36 Example Casting
public class Animal {
String name="Animal";
public void sound();
{ public class Dog extends
Animal {
System.out.println("Animal
String name=“Dog";
Sound");
@Override
}}
public void sound(){
public class Duck extends
System.out.println("Dog
Animal {
Sound");
String name="Duck"; }
@Override }
public void sound(){ public class Cat extends
System.out.println("Duck Animal {
Sound"); String name=“Cat";
} @Override
System.out.println(""+c.nam
e);
38 Java Runtime Polymorphism
Example: Bank
Consider a scenario where Bank is a class that provides a method to
get the rate of interest.
However, the rate of interest may differ according to banks. For
example, SBI, ICICI, and AXIS banks are providing 8.4%, 7.3%, and
9.7% rate of interest.
39 Java Runtime Polymorphism
Example: Bank
40 Java Runtime Polymorphism
Example: Bank
class TestPolymorphism{
public static void main(String args[]){
class Bank{
Bank b;
float getRateOfInterest(){return 0;}
b=new SBI();
}
System.out.println("SBI Rate of Interest:
class SBI extends Bank{
"+b.getRateOfInterest());
float getRateOfInterest(){return 8.4f;}
b=new ICICI();
}
System.out.println("ICICI Rate of
class ICICI extends Bank{
Interest: "+b.getRateOfInterest());
float getRateOfInterest(){return 7.3f;}
b=new AXIS();
}
System.out.println("AXIS Rate of
class AXIS extends Bank{
Interest: "+b.getRateOfInterest());
float getRateOfInterest(){return 9.7f;}
}
}
}
41 Java Runtime Polymorphism
Example: Bank
class TestPolymorphism{
public static void main(String args[]){
class Bank{
Bank b;
float getRateOfInterest(){return 0;}
b=new SBI();
}
System.out.println("SBI Rate of Interest:
class SBI extends Bank{
Output: "+b.getRateOfInterest());
float getRateOfInterest(){return 8.4f;}
b=new ICICI();
}
SBI Rate of Interest: 8.4 System.out.println("ICICI Rate of
class ICICI extends Bank{
ICICI Rate of Interest: 7.3
float getRateOfInterest(){return 7.3f;}
Interest: "+b.getRateOfInterest());
AXIS Rate of Interest: 9.7 b=new AXIS();
}
System.out.println("AXIS Rate of
class AXIS extends Bank{
Interest: "+b.getRateOfInterest());
float getRateOfInterest(){return 9.7f;}
}
}
}
42 Java Runtime Polymorphism with
Data Member
System.out.println(obj.speedlimit);//90
}
44 Java Runtime Polymorphism with
Data Member
Since we are accessing the data
member which is not overridden,
class Bike{ hence it will access the data
int speedlimit=90; member of the Parent class
} always.
class Honda3 extends Bike{
int speedlimit=150;
System.out.println(obj.speedlimit);//90
}
45 Java Runtime Polymorphism with
Data Member
Since we are accessing the data
member which is not overridden,
class Bike{ hence it will access the data
int speedlimit=90; member of the Parent class
} always.
class Honda3 extends Bike{
int speedlimit=150;
Output:
public static void main(String args[]){
90
Bike obj=new Honda3();
System.out.println(obj.speedlimit);//90
}
46 Java Runtime Polymorphism with
Multilevel Inheritance