Introduction:
This Java program serves as an example of polymorphism, a fundamental concept in object-oriented programming. Polymorphism allows objects of different types to be treated as objects of a common base type, facilitating flexibility and extensibility in software design. In this example, we have a hierarchy of shapes—Shape, Circle, and Square—to illustrate how polymorphism works.
Algorithm:
- Shape Class:
- The Shape class serves as the base class for all shapes. It contains a method draw() that prints a generic message indicating the act of drawing a shape.
- Circle Class:
- The Circle class extends the Shape class and overrides the draw() method to provide a specialized implementation for drawing a circle. The overridden method prints a message specific to drawing a circle.
- Square Class:
- Similarly, the Square class extends the Shape class and provides its own implementation of the draw() method to handle drawing a square. The overridden method prints a message indicating the act of drawing a square.
- Polymorphism Example Class:
- The PolymorphismExample class contains the main method, serving as the entry point of the program.
- Two objects, shape1 and shape2, are declared of type Shape but instantiated as Circle and Square objects, respectively. This demonstrates the polymorphic behavior, allowing objects of derived classes to be treated as objects of the base class.
- The draw() method is called on both shape1 and shape2. Despite their declared type being Shape, the actual method invoked at runtime is determined by the specific type of the object. Thus, draw() calls the overridden draw() method in the Circle class, and shape2.draw() calls the overridden method in the Square class.
Explanation :
The program showcases the power of polymorphism, emphasizing the ability to treat objects of derived classes uniformly through a common base class. This flexibility promotes code reusability and simplifies maintenance, as new shapes can be added without modifying existing code. The concept of overriding methods enables each shape to define its unique behaviour while adhering to a shared interface provided by the Shape class. The program output demonstrates how the correct draw() method is dynamically selected based on the actual type of the object at runtime, illustrating the essence of polymorphic behaviour in Java.
Code :
class Shape {
public void draw() {
System.out.println("Drawing a shape");
}
}
class Circle extends Shape {
@Override
public void draw() {
System.out.println("Drawing a circle");
}
}
class Square extends Shape {
@Override
public void draw() {
System.out.println("Drawing a square");
}
}
public class PolymorphismExample {
public static void main(String[] args) {
Shape shape1 = new Circle();
Shape shape2 = new Square();
// Polymorphism in action
shape1.draw(); // Calls the draw method of Circle
shape2.draw(); // Calls the draw method of Square
}
}
In this example, Circle and Square are subclasses of the Shape class. Each subclass overrides the draw method to provide its own implementation. In the main method, we create objects of type Circle and Square, but we declare them as type Shape. This allows us to use polymorphism, and when we call the draw method on these objects, it dynamically dispatches to the appropriate method based on the actual type of the object at runtime.
Feel free to run this program and observe the output to see how polymorphism works in Java!
FAQ’s :
Q1: What is polymorphism in Java?
Answer: Polymorphism in Java is a concept that allows objects of different types to be treated as objects of a common base type. It enables a single interface (or method) to be used for various types of objects, providing flexibility and extensibility in object-oriented programming.
Q2: How is polymorphism achieved in Java?
Answer: Polymorphism in Java is achieved through method overriding. When a subclass provides a specific implementation for a method that is already defined in its superclass, it overrides the method. At runtime, the appropriate method is dynamically invoked based on the actual type of the object, allowing for polymorphic behaviour.
Q3: Can you provide an example of polymorphism in a Java program?
Answer: Certainly! In the given Java program for polymorphism, there is a hierarchy of shapes with a base class Shape and two subclasses Circle and Square. Objects of these subclasses are treated as objects of the base class, demonstrating polymorphism in action.
Q4: Why is polymorphism important in Java programming?
Answer: Polymorphism promotes code reusability and flexibility. It allows for the creation of generic code that can work with objects of different types, making it easier to extend and maintain software. Additionally, polymorphism supports the implementation of clean and modular designs, enhancing the overall structure of Java programs.
Q5: What is the significance of method overriding in polymorphism?
Answer: Method overriding is crucial in polymorphism as it enables a subclass to provide a specific implementation for a method defined in its superclass. This allows objects of different types to be treated uniformly through a common interface. During runtime, the JVM dynamically selects the appropriate method based on the actual type of the object, facilitating the polymorphic behaviour in Java programs.