0% found this document useful (0 votes)
4 views

2.polymorphism Types Method Overloading and Method Overriding

java
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

2.polymorphism Types Method Overloading and Method Overriding

java
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Polymorphism

Module-2

Professor & Technical Trainer / CSE


Saveetha School of Engineering, SIMATS
[email protected]
+91-9500600868
Polymorphism
 Polymorphism in OOP is the ability of an entity to take several forms. i.e, it refers to the ability of
an object (or a reference to an object) to take different forms of objects.

Example: Person

Dr.M.Sivakumar SIMATS 2
Polymorphism
 Types

Dr.M.Sivakumar SIMATS 3
1.Compile-time Polymorphism
 As the meaning is implicit, this is used to write the program in such a way, that flow of control is
decided in compile time itself. It is achieved using method overloading.

 When a class has two or more than two methods which are having the same name but different
types of order or number of parameters, it is known as Method overloading.

 Java allows a function to have the same name if it can distinguish them by their number and type
of arguments.

Dr.M.Sivakumar SIMATS 4
1.Compile-time Polymorphism
 Example

Dr.M.Sivakumar SIMATS 5
2.Run-time Polymorphism [Dynamic]
 Dynamic polymorphism is a process in which a call to an overridden method is resolved at
runtime, that’s why it is called runtime polymorphism.

 Method Overriding is one of the ways to achieve Dynamic Polymorphism.

 Method overriding is a feature which you get when you implement inheritance in your program.

A simple example can be from real world e.g. animals. An application can have Animal class, and
its specialized sub classes like Cat and Dog. These subclasses will override the default behavior
provided by Animal class + some of its own specific behavior

Dr.M.Sivakumar SIMATS 6
2.Run-time Polymorphism [Dynamic]
 Example: class Cat extends Animal{
public void makeNoise()
public class Animal { {
public void makeNoise() System.out.println("Meawoo");
{ }
System.out.println("Some sound"); }
} public class Demo
} {
class Dog extends Animal{ public static void main(String[] args) {
public void makeNoise() Animal a1 = new Cat();
{ a1.makeNoise(); //Prints Meowoo
System.out.println("Bark"); Animal a2 = new Dog();
} a2.makeNoise(); //Prints Bark
} }
}

Dr.M.Sivakumar SIMATS 7
Polymorphism
Advantages

 Method overloading allows methods that perform similar or closely related functions to be
accessed through a common name.

 Constructors allowing different ways to initialize objects of a class can implement method
overloading.

 Method overriding allows a subclass to use all the general definitions that a superclass provides
and add specialized definitions through overridden methods.

 Method overriding works together with inheritance to enable code-reuse of existing classes
without the need for re-compilation
Dr.M.Sivakumar SIMATS 8

You might also like