w3resource

Java Inheritance Programming - Create a class called Shape with a method called getArea and a subclass called Rectangle

Java Inheritance: Exercise-3 with Solution

Write a Java program to create a class called Shape with a method called getArea(). Create a subclass called Rectangle that overrides the getArea() method to calculate the area of a rectangle.

Sample Solution:

Java Code:

// Define the parent class Shape
public class Shape {
    // Define a public method named getArea that returns a double
    public double getArea() {
        // Return 0.0 as the default area
        return 0.0;
    }
} 

// Define the child class Rectangle that extends Shape
public class Rectangle extends Shape {
    // Define private instance variables length and width
    private double length;
    private double width;
    
    // Define the constructor that takes length and width as parameters
    public Rectangle(double length, double width) {
        // Assign the length parameter to the instance variable length
        this.length = length;
        // Assign the width parameter to the instance variable width
        this.width = width;
    }
    
    // Use the @Override annotation to indicate that this method overrides a method in the superclass
    @Override
    // Define the getArea method that returns a double
    public double getArea() {
        // Return the area of the rectangle (length * width)
        return length * width;
    }
} 
// Define the main class
public class Main {
    // Define the main method
    public static void main(String[] args) {
        // Create an instance of Rectangle with length 3.0 and width 10.0
        Rectangle rectangle = new Rectangle(3.0, 10.0);
        // Call the getArea method on the rectangle instance and store the result in the area variable
        double area = rectangle.getArea();
        // Print the area of the rectangle to the console
        System.out.println("The area of the rectangle is: " + area);
    }
} 

Output:

The area of the rectangle is: 30.0

Explanation:

In the above exercise, the Shape class has a single method called getArea() that returns a double value. The Rectangle class is a subclass of Shape and overrides the getArea() method to calculate the area of a rectangle using the formula length x width. The Rectangle class constructor sets length and width values.

Finally in the main() method we create an instance of the Rectangle class and call its getArea() method to get the rectangle's area.

Flowchart:

Flowchart: class Shape.
Flowchart: class Rectangle extends Shape.
Flowchart:  Main class.

Java Code Editor:

Contribute your code and comments through Disqus.

Previous: Create a class called Vehicle with a method called drive(). Create a subclass called Car that overrides the drive() method to print "Repairing a car".
Next: Employee class with methods called work, getSalary.

What is the difficulty level of this exercise?



Follow us on Facebook and Twitter for latest update.