0% found this document useful (0 votes)
25 views16 pages

Chapter 4

The document discusses polymorphism in Java. Polymorphism allows the same method to be called for objects of different classes, with different implementations. It covers method overloading, where methods have the same name but different parameters, and method overriding, where subclasses define methods with the same name and parameters as parent classes. Examples demonstrate how polymorphism works through method overloading and overriding.

Uploaded by

alqrshyhsn82
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)
25 views16 pages

Chapter 4

The document discusses polymorphism in Java. Polymorphism allows the same method to be called for objects of different classes, with different implementations. It covers method overloading, where methods have the same name but different parameters, and method overriding, where subclasses define methods with the same name and parameters as parent classes. Examples demonstrate how polymorphism works through method overloading and overriding.

Uploaded by

alqrshyhsn82
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/ 16

Computer Programming (2)

Chapter 3
Inheritance & Polymorphism II
1. Polymorphism
2. Advantages of Polymorphism in Java
3. Disadvantages of Polymorphism in Java
4. Polymorphism Examples
5. Method Overloading and Method Overriding
6. More Example

Contents
Recommended Reading
1. Java Inheritance:
https://www.tutorialspoint.com/java/java_inheritance.htm
• Polymorphism
Polymorphism
• Polymorphism: many forms.
Polymorphism

• Polymorphism
• means "many forms", and it occurs when we have many classes that are
related to each other by inheritance.
• Polymorphism uses those methods to perform different tasks. This allows us
to perform a single action in different ways.
• Requires that there be multiple methods of the same name.

• The choice of which one to execute depends on the object that is in a


variable.
Advantages of Polymorphism in Java

• Advantages:
1.Increases code reusability by allowing objects of different classes to be treated as
objects of a common class.

2.Improves readability and maintainability of code by reducing the amount of code


that needs to be written and maintained.

3.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

• Disadvantages:
1.Can make it more difficult to understand the behavior of an object, especially if
the code is complex.

2.This may lead to performance issues, as polymorphic behavior may require


additional computations at runtime.
Polymorphism Examples
• Example:
class Shapes {
public void area() {
System.out.println("The formula for area of ");
}
}
class Triangle extends Shapes {
public void area() {
System.out.println("Triangle is ½ * base * height ");
Output
}
}
class Circle extends Shapes {
The formula for area of Triangle is ½ * base *
public void area() { height
System.out.println("Circle is 3.14 * radius * radius "); The formula for area of Circle is 3.14 * radius *
}
} radius
class Main {
public static void main(String[] args) {
Shapes myShape = new Shapes(); // Create a Shapes object
Shapes myTriangle = new Triangle(); // Create a Triangle object
Shapes myCircle = new Circle(); // Create a Circle object
myShape.area();
myTriangle.area();
myShape.area();
myCircle.area();
}
} Reference: https://www.mygreatlearning.com/blog/polymorphism-in-java/
Method Overloading and Method Overriding
Polymorphism in Java can be performed by two different methods:

Method Overloading
• Defined as a process that can create multiple methods of the
same name in the same class, and all the methods work in
different ways.
• Method overloading occurs when there is more than one method
of the same name in the class
Method Overriding
• Defined as a process when the subclass or a child class has the
same method as declared in the parent class.
Reference: https://www.mygreatlearning.com/blog/polymorphism-in-java/
Method Overloading and Method Overriding
• Method Overloading Example:
public class Sum {
// Overloaded sum(). This sum takes two int parameters
public int sum(int x, int y) { return (x + y); }

// Overloaded sum(). This sum takes three int parameters


public int sum(int x, int y, int z)
{
return (x + y + z);
}

// Overloaded sum(). This sum takes two double


// parameters
public double sum(double x, double y)
{
return (x + y);
} Output
// Driver code
public static void main(String args[]) 30
{ 60
Sum s = new Sum();
System.out.println(s.sum(10, 20));
31.0
System.out.println(s.sum(10, 20, 30));
System.out.println(s.sum(10.5, 20.5));
}
}
Method Overloading and Method Overriding

• Method Overriding Example:


class Vehicle{
void run(){
System.out.println("Vehicle is moving");
}
}

class Car2 extends Vehicle{


void run(){
System.out.println("car is running safely");
}

Output
public static void main(String args[]){
Car2 obj = new Car2(); Car is running safely
obj.run();//calling method
}
}

Reference: https://www.mygreatlearning.com/blog/polymorphism-in-java/
Method Overloading and Method Overriding

• Overriding is done by using a reference variable of the superclass.


• Which method to be called is determined based on the object which is being
referred to by the reference variable.
• This is also known as Upcasting.

class A{}
class B extends A{}
A a=new B(); //upcasting

Reference: https://www.mygreatlearning.com/blog/polymorphism-in-java/
More Example
• Example: class main{
public static void main(String args[]){
class Animal { Animal A = new Animal();
void eat(){ Animal h = new herbivores(); //upcasting
System.out.println("Animals Eat");
} Animal o = new omnivores(); //upcasting
} Animal c = new carnivores(); //upcasting
class herbivores extends Animal{ A.eat();
h.eat();
void eat(){
o.eat();
System.out.println("Herbivores Eat Plants"); c.eat();
}
} }
}
class omnivores extends Animal{
void eat(){
System.out.println("Omnivores Eat Plants and meat"); Output
}
}
class carnivores extends Animal{ Animals eat
void eat(){ Herbivores Eat Plants
System.out.println("Carnivores Eat meat"); Omnivores Eat Plants and meat
}
} Carnivores Eat meat

Reference: https://www.mygreatlearning.com/blog/polymorphism-in-java/
Thank You

You might also like