Polymorphism, Subtyping,
and Casting
Outline
2
What is Polymorphism
Subtyping, Subclassing and Subtype Polymorphism
What is Casting
Types of Casting
What is polymorphism
3
Polymorphism: poly
and morphs. The word
"poly" means many
and "morphs“ means
forms.
What is polymorphism
4
Compile time polymorphism: It is also
known as static polymorphism. This type
of polymorphism is achieved by function
overloading.
Run time polymorphism: It is also
known as Dynamic Method Dispatch. It
is a process in which a function call to
the overridden method is resolved at
Runtime. This type of polymorphism is
achieved by Method Overriding.
Polymorphism (Example)
5
Subtyping, subclassing, and subtype polymorphism
6
Subtype-polymorphism is
about implementing an
interface, and so being able to
substitute different
implementations of that
interface at run-time.
The implementers of the
interfaces are considered
subtypes.
Subclassing and Subtype Polymorphsim (example)
7
Subclassing
class Base
{ //interface with included definitions}
class Derived extends Base
{
//Add some additional functionality.
//Reuse Base without having to explicitly
forward //the functions in Base
} Subtype polymorpshim
Interface XYZ
{ //some abstract interface, no definitions
included } class Implementation implements
XYZ
{ //provide all the operations //required by the
interface
}
Casting in java
8
Type casting is when you assign a value of one primitive data type to another type.
Widening Casting (automatically) - converting a smaller type to a larger type size
byte -> short -> char -> int -> long -> float -> double
Narrowing Casting (manually) - converting a larger type to a smaller size type
double -> float -> long -> int -> char -> short -> byte
Object Type Casting in Java
Reference variables are different; the reference variable only refers to an object
but doesn’t contain the object itself.
Upcasting and downcasting
9
Upcasting is casting to a supertype, while downcasting is casting to a subtype.
Upcasting is always allowed, but downcasting involves a type check and can
throw a ClassCastException
Upcasting: When we want to cast a Sub class to Super class, we use
Upcasting(generalization or widening). It happens automatically, no
need to do anything explicitly.
Downcasting : When we want to cast a Super class to Sub class, we
use Downcasting (specialization or narrowing), and Downcasting is not
directly possible in Java, but possible indirectly.
Upcasting and downcasting
10
Upcasting / runtime polymorphism
11
class Bike
{ If the reference variable of Parent
class refers to the object of Child
void run() class, it is known as up-casting.
{System.out.println("running");}
}
class Splendor extends Bike
{
void run()
{System.out.println("running safely with 60km");}
public static void main(String args[])
{
Bike b = new Splendor();//upcasting
b.run();
}
}
Downcasting
12
References
13
• https://www.javatpoint.com/runtime-polymorphism-in-java
• https://www.edureka.co/blog/coupling-in-java/
• https://www.geeksforgeeks.org/coupling-in-java/
• https://www.slideshare.net/AdilAslam4/object-oriented-programming-in-java-slide-56
• https://www.slideshare.net/AdilAslam4/object-oriented-programming-in-java-slide-46
14
THANK YOU