0% found this document useful (0 votes)
31 views9 pages

Chapter 6 Polymorphism

This document discusses polymorphism in object-oriented programming using Java. It defines polymorphism as an object taking on many forms, allowing a single action to be performed in different ways. There are two types of polymorphism in Java: compile-time polymorphism, which includes method overloading, and runtime polymorphism, which is achieved through method overriding and allows an overridden method to be called based on the object's type at runtime. Examples demonstrate how polymorphism works through method overloading and overriding.

Uploaded by

eyobeshete16
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views9 pages

Chapter 6 Polymorphism

This document discusses polymorphism in object-oriented programming using Java. It defines polymorphism as an object taking on many forms, allowing a single action to be performed in different ways. There are two types of polymorphism in Java: compile-time polymorphism, which includes method overloading, and runtime polymorphism, which is achieved through method overriding and allows an overridden method to be called based on the object's type at runtime. Examples demonstrate how polymorphism works through method overloading and overriding.

Uploaded by

eyobeshete16
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Object oriented programming using java

Prepared by Daniel K

Chapter Six
Polymorphism
Introduction
 Polymorphism is the ability of an object to take on many forms.
 Polymorphism in Java is a concept by which we can perform a single action in different ways.
 Polymorphism is derived from 2 Greek words: poly and morphs.
o The word "poly" means many and "morphs" means forms.
o So polymorphism means many forms.
 Real-life Illustration Polymorphism:
o A person at the same time can have different characteristics. Like a man at the same
time is a father, a husband, and an employee. So the same person possesses
different behavior in different situations. This is called polymorphism.
What is Polymorphism in Java?
 Polymorphism is considered one of the important features of OOP
 Polymorphism allows us to perform a single action in different ways.
 Polymorphism allows you to define one interface and have multiple implementations.
Types of Java polymorphism
 In Java polymorphism is mainly divided into two types:
o Compile-time Polymorphism
o Runtime Polymorphism

Compile-Time Polymorphism
 It is also known as static polymorphism.
 This type of polymorphism is achieved by function overloading or operator overloading.
o Note: But Java doesn’t support the Operator Overloading.
Object oriented programming using java
Prepared by Daniel K

Method Overloading
o When there are multiple functions with the same name but different parameters then these
functions are said to be overloaded. Functions can be overloaded by changes in the number
of arguments or/and a change in the type of arguments.
Example for Java program for Method Overloading
// By using Different Types of Arguments // Class 2
// Class 1 // Main class
// Helper class
class GFG
class Helper
{
{
// Main driver method
// Method with 2 integer parameters
public static void main(String[] args)
static int Multiply(int a, int b)
{
{
// Calling method by passing
// Returns product of integer numbers
// input as in arguments
return a * b;
System.out.println(Helper.Multiply(2, 4));
}
System.out.println(Helper.Multiply(5.5, 6.3));
// Method 2
}
// With same name but with 2 double parameters
}
static double Multiply(double a, double b)
{
// Returns product of double numbers
return a * b;
}
}
Object oriented programming using java
Prepared by Daniel K
Example 2
// Java program for Method Overloading // Return product

// by Using Different Numbers of Arguments return a * b * c;

// Class 1 }

// Helper class }

class Helper { // Class 2

// Method 1 // Main class

// Multiplication of 2 numbers class GFG {

static int Multiply(int a, int b) // Main driver method

{ public static void main(String[] args)

// Return product {

return a * b; // Calling method by passing

} // input as in arguments

// Method 2 System.out.println(Helper.Multiply(2, 4));

// // Multiplication of 3 numbers System.out.println(Helper.Multiply(2, 7, 3));

static int Multiply(int a, int b, int c) }

{ }

Output

42
Object oriented programming using java
Prepared by Daniel K

Subtypes of Compile-time Polymorphism


1. Function Overloading
 It is a feature in C++ where multiple functions can have the same name but with different
parameter lists.
 The compiler will decide which function to call based on the number and types of arguments
passed to the function.
2. Operator Overloading
 It is a feature in C++ where the operators such as +, -, *, etc. can be given additional
meanings when applied to user-defined data types.
3. Template
 It is a powerful feature in C++ that allows us to write generic functions and classes.
 A template is a blueprint for creating a family of functions or classes.
Runtime 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.
 Method overriding, on the other hand, occurs when a derived class has a definition for one of
the member functions of the base class.
 That base function is said to be overridden.
Upcasting
 If the reference variable of Parent class refers to the object of Child class, it is known as
Upcasting. For example:

Example
class A{}
class B extends A{}

A a=new B();//upcasting
Object oriented programming using java
Prepared by Daniel K
Example 1 on runtime polymorphism
// Java Program for Method Overriding {
// Class 1 // Print statement
// Helper class System.out.println("subclass2");
class Parent { }
// Method of parent class }
void Print() // Class 4
{ // Main class
// Print statement class GFG {
System.out.println("parent class"); // Main driver method
} public static void main(String[] args)
} {
// Class 2 // Creating object of class 1
// Helper class Parent a;
class subclass1 extends Parent { // Now we will be calling print
// Method methods
void Print() { // inside main() method
System.out.println("subclass1"); } a = new subclass1();
} a.Print();
// Class 3 a = new subclass2();
// Helper class a.Print();
class subclass2 extends Parent { } Output
// Method }
subclass1
void Print()
subclass2

Explanation of the above code:


 Here in this program, when an object of a child class is created, then the method inside the

child class is called. This is because the method in the parent class is overridden by the

child class. Since the method is overridden, this method has more priority than the parent

method inside the child class. So, the body inside the child class is executed.
Object oriented programming using java
Prepared by Daniel K
Example 2 on runtime polymorphism
 In this example, we are creating two classes Bike and Splendor. Splendor class extends
Bike class and overrides its run() method. We are calling the run method by the reference
variable of Parent class. Since it refers to the subclass object and subclass method
overrides the Parent class method, the subclass method is invoked at runtime.
 Since method invocation is determined by the JVM not compiler, it is known as runtime
polymorphism.
class Bike{
void run(){System.out.println("running");}
}
Output
class Splendor extends Bike{
void run(){System.out.println("running safely with 60km");} running safely with 60km.

public static void main(String args[]){


Bike b = new Splendor();//upcasting
b.run();
}
}

Java Runtime Polymorphism Example 3 : 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.
Object oriented programming using java
Prepared by Daniel K
class Bank{
float getRateOfInterest(){return 0;}
}
class SBI extends Bank{
float getRateOfInterest(){return 8.4f;}
}
class ICICI extends Bank{
float getRateOfInterest(){return 7.3f;} Output
}
SBI Rate of Interest: 8.4
class AXIS extends Bank{
float getRateOfInterest(){return 9.7f;} ICICI Rate of Interest: 7.3
}
AXIS Rate of Interest: 9.7
class TestPolymorphism{
public static void main(String args[]){
Bank b;
b=new SBI();
System.out.println("SBI Rate of Interest: "+b.getRateOfInterest());
b=new ICICI();
System.out.println("ICICI Rate of Interest: "+b.getRateOfInterest());
b=new AXIS();
System.out.println("AXIS Rate of Interest: "+b.getRateOfInterest());
}
}
Java Runtime Polymorphism Example 4: Shape
class Shape{ void draw(){System.out.println("drawing trian
void draw(){System.out.println("drawing...");} gle...");}
} }
class Rectangle extends Shape{ class TestPolymorphism2{
void draw(){System.out.println("drawing recta public static void main(String args[]){
ngle...");} Shape s;
} s=new Rectangle();
class Circle extends Shape{ s.draw();
void draw(){System.out.println("drawing circle. s=new Circle(); Output
..");} s.draw();
drawing rectangle...
} s=new Triangle();
class Triangle extends Shape{ s.draw(); drawing circle...
}} drawing triangle...
Object oriented programming using java
Prepared by Daniel K

Java Runtime Polymorphism Example 5 : Animal


class Animal{ }
void eat(){System.out.println("eating...");} class TestPolymorphism3{
} public static void main(String[] args){
class Dog extends Animal{ Animal a;
void eat(){System.out.println("eating bread...");} a=new Dog();
} a.eat();
class Cat extends Animal{ a=new Cat();
void eat(){System.out.println("eating rat...");} a.eat();
Output
} a=new Lion();
eating bread...
class Lion extends Animal{ a.eat();
void eat(){System.out.println("eating meat...");} }} eating rat...

eating meat...
Subtype of Run-time Polymorphism
Virtual functions
 It allows an object of a derived class to behave as if it were an object of the base class.
The derived class can override the virtual function of the base class to provide its own
implementation. The function call is resolved at runtime, depending on the actual type of
the object.
Diagram –
Object oriented programming using java
Prepared by Daniel K
 Polymorphism in Java is a concept that allows objects of different classes to be treated as

objects of a common class. It enables objects to behave differently based on their specific

class type.

Advantages of Polymorphism in Java

 Increases code reusability by allowing objects of different classes to be treated as objects

of a common class.

 Improves readability and maintainability of code by reducing the amount of code that

needs to be written and maintained.

 Supports dynamic binding, enabling the correct method to be called at runtime, based on

the actual class of the object.

 Enables objects to be treated as a single type, making it easier to write generic code that

can handle objects of different types.

Disadvantages of Polymorphism in Java

 Can make it more difficult to understand the behavior of an object, especially if the code

is complex.

 This may lead to performance issues, as polymorphic behavior may require additional

computations at runtime.

You might also like