In this article on How to Implement Inheritance in Java, I will demonstrate the concept of inheritance using a simple example of a Box class.

Problem Statement

Demonstrate how to create a class called Box with instance variables width, height, and depth. Create another class BoxWeight which inherits class Box with an additional instance variable weight. Compute the volume of the box using inheritance.

Solution

class Box {
    // Instance variables
    protected double width;
    protected double height;
    protected double depth;

    // Constructor for Box class
    public Box(double width, double height, double depth) {
        this.width = width;
        this.height = height;
        this.depth = depth;
    }

    // Method to calculate and return the volume of the box
    public double calculateVolume() {
        return width * height * depth;
    }

    // Display box information
    public void display() {
        System.out.println("Box Dimensions:");
        System.out.println("Width: " + width);
        System.out.println("Height: " + height);
        System.out.println("Depth: " + depth);
    }
}

class BoxWeight extends Box {
    // Additional instance variable
    private double weight;

    // Constructor for BoxWeight class
    public BoxWeight(double width, double height, double depth, double weight) {
        super(width, height, depth); // Call the constructor of the superclass
        this.weight = weight;
    }

    // Display box information including weight
    @Override
    public void display() {
        super.display(); // Call the display method of the superclass
        System.out.println("Weight: " + weight);
    }
}

public class BoxInheritanceDemo {
    public static void main(String[] args) {
        // Create an object of BoxWeight
        BoxWeight boxWithWeight = new BoxWeight(5.0, 3.0, 2.0, 10.0);

        // Display box dimensions and weight
        boxWithWeight.display();

        // Calculate and display the volume
        double volume = boxWithWeight.calculateVolume();
        System.out.println("Volume of the box: " + volume);
    }
}

In this program:

  1. We have a Box class with instance variables width, height, and depth, along with a method to calculate the volume of the box and a display method to print box dimensions.
  2. The BoxWeight class inherits from the Box class and adds an additional instance variable weight. It also overrides the display method to include the weight information.
  3. In the main method, we create an object of BoxWeight with dimensions and weight.
  4. Then, we call the display method of the BoxWeight class, which displays both dimensions and weight.
  5. Filally, we calculate and display the volume of the box using the inherited calculateVolume method from the Box class.

This program showcases the concept of inheritance and also adds a unique feature by overriding the display method in the subclass to include the weight information.



Further Reading

Spring Framework Practice Problems and Their Solutions

From Google to the World: The Story of Go Programming Language

Why Go? Understanding the Advantages of this Emerging Language

Creating and Executing Simple Programs in Go

20+ Interview Questions on Go Programming Language

Java Practice Exercise

programmingempire

Princites