0% found this document useful (0 votes)
97 views6 pages

Java Inheritance and Polymorphism Example

The document describes a JAVA program that implements inheritance and polymorphism through a Shape class and its subclasses: Circle, Triangle, and Square. Each subclass overrides the draw() and erase() methods to provide specific implementations. The main program demonstrates polymorphism by creating an array of Shape objects and invoking the overridden methods for each shape instance.

Uploaded by

indusindu72
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)
97 views6 pages

Java Inheritance and Polymorphism Example

The document describes a JAVA program that implements inheritance and polymorphism through a Shape class and its subclasses: Circle, Triangle, and Square. Each subclass overrides the draw() and erase() methods to provide specific implementations. The main program demonstrates polymorphism by creating an array of Shape objects and invoking the overridden methods for each shape instance.

Uploaded by

indusindu72
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

Program 05 : Inheritance & Polymorphism – Shape Class

Develop a JAVA program to create a class named shape. Create three


sub classes namely: circle, triangle and square, each class has two
member functions named draw () and erase (). Demonstrate
polymorphism concepts by developing suitable methods, defining
member data and main program.

class Shape {
protected String name;

public Shape(String name) {


[Link] = name;
}

public void draw() {


[Link]("Drawing a " + name);
}

public void erase() {


[Link]("Erasing a " + name);
}
}
class Circle extends Shape {
private double radius;

public Circle(String name, double radius) {


super(name);
[Link] = radius;
}

@Override
public void draw() {
[Link]("Drawing a circle with radius " + radius);
}

@Override
public void erase() {
[Link]("Erasing a circle with radius " + radius);
}
}

class Triangle extends Shape {


private double base;
private double height;
public Triangle(String name, double base, double height) {
super(name);
[Link] = base;
[Link] = height;
}

@Override
public void draw() {
[Link]("Drawing a triangle with base " + base + " and
height " + height);
}

@Override
public void erase() {
[Link]("Erasing a triangle with base " + base + " and
height " + height);
}
}

class Square extends Shape {


private double side;
public Square(String name, double side) {
super(name);
[Link] = side;
}

@Override
public void draw() {
[Link]("Drawing a square with side length " + side);
}

@Override
public void erase() {
[Link]("Erasing a square with side length " + side);
}
}

public class ShapeDemo {


public static void main(String[] args) {
Shape[] shapes = new Shape[3];

shapes[0] = new Circle("Circle", 5.0);


shapes[1] = new Triangle("Triangle", 4.0, 6.0);
shapes[2] = new Square("Square", 3.0);

for (Shape shape : shapes) {


[Link]();
[Link]();
[Link]();
}
}
}
In this program, the Shape class is the superclass, and Circle, Triangle,
and Square are its subclasses. The draw() and erase() methods are
overridden in each subclass. The main method creates an array
of Shape objects and initializes it with instances of the different
subclasses. When iterating through the array and calling
the draw() and erase() methods, polymorphism allows the appropriate
overridden methods in each subclass to be executed.
Output
$ java ShapeDemo
Drawing a circle with radius 5.0
Erasing a circle with radius 5.0

Drawing a triangle with base 4.0 and height 6.0


Erasing a triangle with base 4.0 and height 6.0
Drawing a square with side length 3.0
Erasing a square with side length 3.0

Common questions

Powered by AI

To enhance extensibility, the program could implement interfaces such as Drawable or Erasable to define a contract for future shapes that might not naturally fit the existing hierarchy. This would separate interface from implementation and promote more flexible design. Additionally, leveraging design patterns like Factory or Strategy could further decouple object creation and usage specifics, allowing easier integration of new shape types in the future. Such improvements would support easier maintenance and scalability of the code base .

The program demonstrates encapsulation by keeping the data related to each shape (such as radius, base, height, or side) private within their respective classes (Circle, Triangle, Square). This encapsulation ensures that access to these data members is controlled and can only be modified through defined interfaces (constructors and methods). This practice minimizes external interference and reduces the potential for bugs by maintaining strict control over data state and exposure .

The '@Override' annotation in the program signals that a method is intended to override a method in a superclass. Its use is important for ensuring accuracy in method overriding, as it alerts the compiler to any discrepancies, such as misspelling of method names or incorrect parameter types. If omitted, these errors could lead to unintended method resolutions without any compilation errors, resulting in logical errors or method hiding. The '@Override' ensures the method contracts are correctly respected, enhancing code reliability and maintainability .

The use of an array of Shape objects is significant as it enables the program to handle multiple shape types uniformly. Each element of the array is a Shape reference, but it can point to objects of the subclass types Circle, Triangle, and Square. This allows the main() method to iterate over the array and invoke the draw() and erase() methods polymorphically. Despite being stored in a common data structure, the actual method executed depends on the specific subclass type of the object, demonstrating polymorphism effectively .

Handling different shape objects in the program relies on a common superclass interface (Shape) for invoking methods such as draw() and erase(). However, the actual method invoked is determined by the object's runtime type (Circle, Triangle, Square) due to polymorphism. While the method calls appear the same from the perspective of the Shape type, the behavior differs because each subclass provides its own implementation for these methods. This design allows uniform method invocation while ensuring specific behavior aligned with each shape's unique characteristics .

The program illustrates abstraction by defining a generalized Shape class that outlines fundamental methods draw() and erase(). Despite this abstraction, each subclass (Circle, Triangle, Square) implements these methods differently to account for shape-specific operations. This approach highlights abstraction by allowing the Shape class to provide a general framework or interface for shapes, while the specific details are abstracted away into the subclasses. Users can thus interact with shape objects at a high level without needing to understand the implementation details .

Polymorphism in the given Java program is demonstrated through method overriding. The Shape class defines basic draw() and erase() methods which are then overridden in each of the Circle, Triangle, and Square subclasses. When an array of Shape objects is created, it contains objects of these subclasses. During the iteration over this array, calling the draw() and erase() methods on Shape references actually invokes the overridden methods in the respective subclass, thus demonstrating polymorphism. This allows a unified interface to work with different forms of objects .

Constructors in the program are pivotal for initializing objects with specific attributes. Each class (Shape, Circle, Triangle, Square) has a constructor that sets up its unique properties. The Shape class constructor initializes the name attribute, while subclasses' constructors initialize specific attributes such as radius for Circle, base and height for Triangle, and side for Square. Constructors ensure that object creation is consistent and attributes are correctly initialized, which is crucial for the proper functioning of polymorphic methods in the program .

Inheritance is a key structure in the Java program, facilitating the creation of subtype relationships between the Shape class and its subclasses Circle, Triangle, and Square. The subclasses inherit attributes and methods from the Shape superclass, allowing them to extend and customize behavior via method overriding. This relationship allows for the use of polymorphism, where an object of a subclass can be treated as an object of its superclass, enabling flexible code .

Method overriding provides several benefits in the given Java program: it allows each subclass (Circle, Triangle, Square) to provide a specific implementation of the draw() and erase() methods, which is necessary for accurately representing the actions of drawing and erasing different shapes. This implementation detail makes the code flexible and maintainable, as changes to shape behavior do not require changes to the superclass or affect other subclasses. It also leverages polymorphism to simplify object management, allowing a general interface while executing subclass-specific behavior .

You might also like