w3resource

Java: Abstract Shape2D Class with Rectangle and Circle Subclasses

Java Abstract Class: Exercise-10 with Solution

Write a Java program to create an abstract class Shape2D with abstract methods draw() and resize(). Create subclasses Rectangle and Circle that extend the Shape2D class and implement the respective methods to draw and resize each shape.

Sample Solution:

Java Code:

//Shape2D.java
// Define an abstract class named Shape2D
abstract class Shape2D {
  // Declare an abstract method draw
  public abstract void draw();

  // Declare an abstract method resize
  public abstract void resize();
} 

// Rectangle.java
// Define a class named Rectangle that extends Shape2D
class Rectangle extends Shape2D {

  // Override the draw method from Shape2D
  @Override
  // Implementation of the draw method that prints a message
  public void draw() {
    System.out.println("Rectangle: Drawing a rectangle.");
  }

  // Override the resize method from Shape2D
  @Override
  // Implementation of the resize method that prints a message
  public void resize() {
    System.out.println("Rectangle: Resizing the rectangle.");
  }
}
// Circle.java
// Define a class named Circle that extends Shape2D
class Circle extends Shape2D {

  // Override the draw method from Shape2D
  @Override
  // Implementation of the draw method that prints a message
  public void draw() {
    System.out.println("Circle: Drawing a circle.");
  }

  // Override the resize method from Shape2D
  @Override
  // Implementation of the resize method that prints a message
  public void resize() {
    System.out.println("Circle: Resizing the circle.");
  }
} 
// Main.java
// Define a public class named Main
public class Main {
  // Define the main method, which is the entry point of the program
  public static void main(String[] args) {
    // Create an instance of Rectangle and assign it to a Shape2D reference
    Shape2D rectangle = new Rectangle();
    // Create an instance of Circle and assign it to a Shape2D reference
    Shape2D circle = new Circle();

    // Call the draw method on the rectangle object
    rectangle.draw();
    // Call the resize method on the rectangle object
    rectangle.resize();

    // Call the draw method on the circle object
    circle.draw();
    // Call the resize method on the circle object
    circle.resize();
  }
} 

Output:

Rectangle: Drawing a rectangle.
Rectangle: Resizing the rectangle.
Circle: Drawing a circle.
Circle: Resizing the circle.

Explanation:

In the above exercise -

  • The abstract class "Shape2D" has two abstract methods: draw() and resize(). The subclasses Rectangle and Circle extend the Shape2D class and provide their own implementations for these abstract methods.
  • The "Rectangle" class describes how to draw and resize a rectangle, while the "Circle" class describes how to draw and resize a circle.
  • In the main method, we create instances of Rectangle and Circle, and then call the draw() and resize() methods on each object to demonstrate how each shape is drawn and resized.

Flowchart:

Flowchart: Person Java
Flowchart: Athlete Java
Flowchart: LazyPerson Java
Flowchart: Main Java

Java Code Editor:

Contribute your code and comments through Disqus.

Previous: Abstract Instrument Class with Glockenspiel and Violin Subclasses.
Next: Abstract Bird Class with Eagle and Hawk Subclasses.

What is the difficulty level of this exercise?



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/java-exercises/java-abstract-class-exercise-10.php