Welcome To
Our JAVA
Presentation
Team-01
OUR CREATIVE TEAM
Md. Robiul Islam Dept Of CSE,BSFMSTU
ID:22111102
B.M. Khalid Hassan Mst. Farzana Islam Md. Mostafizur Rahman Md. Jobayer Hossen
ID:22111101 ID:22111103 ID:22111104 ID:22111105
About Importance of
Polymorphism Polymorphism:
Polymorphism is a
fundamental concept in
Polymorphism is the Object-Oriented Programming
ability of a message to be (OOP) and it plays a crucial
processed in more than role in Java.
one form. - Code Reusability.
- Code Flexibility.
- Separation of Concerns.
- Dynamic Method Dispatch.
Classification of
Classification of Polymorphism Polymorphism
0
Method Overloading:
Under the same class.
Same method.
Different parameterlist.
Information About:
Method Overriding:
Different class.
Same method same parameterlist.
Code:
Method
Overloading
Method Overriding
//Normal
Example:
Problem No-01: Write a Java program to create a base class Animal (Animal Family) with a method
called Sound(). Create two subclasses Bird and Cat. Override the Sound() method in each subclass to
make a specific sound for each animal.
Code: public class Animal {
public void makeSound() { public class Main {
System.out.println("The animal makes a public static void main(String[] args) {
sound"); Animal animal = new Animal();
} Bird bird = new Bird();
} Cat cat = new Cat();
public class Bird extends Animal {
@Override animal.makeSound(); // Output: The animal
public void makeSound() { makes a sound
System.out.println("The bird chirps");
bird.makeSound(); // Output: The bird
}
chirps
}
cat.makeSound(); // Output: The cat
public class Cat extends Animal {
@Override meows
public void makeSound() { }
System.out.println("The cat meows"); }
}
}
Output:
Code:
public class Shape {
public double calculateArea() {
return 0; // Default implementation returns 0
}
Problem No-02: }
public class Circle extends Shape {
Write a Java program to create a base private double radius;
class Shape with a method called public Circle(double radius) {
calculateArea(). Create three subclasses: }
this.radius = radius;
Circle, Rectangle, and Triangle. Override
@Override
the calculateArea() method in each public double calculateArea() {
return Math.PI * radius * radius; // Calculate area of circle
subclass to calculate and return the }
shape's area. }
public class Rectangle extends Shape {
private double width;
private double height;
public Rectangle(double width, double height) {
this.width = width;
this.height = height;
}
@Override
public double calculateArea() {
return width * height; // Calculate area of rectangle
}
}
public class Triangle extends Shape {
private double base;
private double height;
Output:
public Triangle(double base, double height) {
this.base = base;
this.height = height;
}
@Override
public double calculateArea() {
return 0.5 * base * height; // Calculate area of triangle
}
}
public class Main {
public static void main(String[] args) {
Circle circle = new Circle(4);
System.out.println("Area of Circle: " +
circle.calculateArea());
Rectangle rectangle = new Rectangle(12, 34);
System.out.println("\nArea of Rectangle: " +
rectangle.calculateArea());
Triangle triangle = new Triangle(5, 9);
System.out.println("\nArea of Triangle: " +
triangle.calculateArea());
}
}