Abstract Class Polymorphism

This program demonstrates polymorphism with abstract class.

Problem Statement

Write a Java program to show polymorphism using abstract class Shape.

Source Code

1 abstract class Shape {
2 abstract void draw();
3 }
4 class Circle extends Shape {
5 void draw(){ System.out.println("Drawing Circle"); }
6 }
7 class Rectangle extends Shape {
8 void draw(){ System.out.println("Drawing Rectangle"); }
9 }
10 public class AbstractPolymorphism {
11 public static void main(String[] args) {
12 System.out.println("Eduinq Abstract Polymorphism");
13 Shape s1=new Circle();
14 Shape s2=new Rectangle();
15 s1.draw(); s2.draw();
16 }
17 }

Program Output

Eduinq Abstract Polymorphism
Drawing Circle
Drawing Rectangle

Explanation

Abstract class defines method signature without implementation. - Subclasses provide their own implementations. - Polymorphism allows superclass reference to point to different subclass objects, executing appropriate method at runtime. - This demonstrates how abstract classes and polymorphism combine to enforce design contracts while enabling flexibility. Conclusion: Shows how abstract classes support polymorphism.

Quick Links to Explore