Inheritance in Java

All Object Oriented languages I’ve worked with, have a concept of Inheritance. A debatable issue in this regards might be JavaScript… as many think of it as a true Object Oriented language, but it lacks the a true inheritance model.

At any rate this article is about Basic Java and how inheritance works. The idea of inheritance is to divide the code up into specific classes. A base class might have all the base fields of an object. But other derived objects may share these fields and add their own, or have only a subset of fields.

Example: We could think of a Building as being a base class. A building has walls, doors and a floor (amongst other things.) But there are different types of buildings. A Condo is a building that might also have a shared wall(s), limited square feet, plumbing and room requirements (bedrooms, bathrooms, closets, etc.) Conversely a Warehouse is a building that might have larger square feet requirements, but no dedicated rooms.

The base class here is Building, which could be created as such:

public class Building {

    private int walls;
    private int height;
    private int squareFeet;
    private int numberOfDoors;
    private boolean skylight;
    private String flooringMaterial;

    public Building(int walls, int height, int squareFeet, int numberOfDoors, boolean skylight, String flooringMaterial) {
        this.walls = walls;
        this.height = height;
        this.squareFeet = squareFeet;
        this.numberOfDoors = numberOfDoors;
        this.skylight = skylight;
        this.flooringMaterial = flooringMaterial;
    }

    public int getWalls() {
        return walls;
    }

    public void setWalls(int walls) {
        this.walls = walls;
    }

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }

    public int getSquareFeet() {
        return squareFeet;
    }

    public void setSquareFeet(int squareFeet) {
        this.squareFeet = squareFeet;
    }

    public int getNumberOfDoors() {
        return numberOfDoors;
    }

    public void setNumberOfDoors(int numberOfDoors) {
        this.numberOfDoors = numberOfDoors;
    }

    public boolean isSkylight() {
        return skylight;
    }

    public void setSkylight(boolean skylight) {
        this.skylight = skylight;
    }

    public String getFlooringMaterial() {
        return flooringMaterial;
    }

    public void setFlooringMaterial(String flooringMaterial) {
        this.flooringMaterial = flooringMaterial;
    }
}

A sub-class based of this, is said to inherit the same base functionality. After all a condo has walls, height, square feet, doors, etc. But a condo might also have special fields such as numberOfBedrooms, numberOfBathrooms, fireplace, hoa and so on.

It would require us to rewrite a lot of the same functionality each time, if we didn’t inherit. We’d have to have each sub class redefine all the base classes fields.

In order to reduce code duplication, we can inherit from a base class.

The base class above (Building) can be used to now create a Condo class, like so:

public class Condo extends Building{

}

In an IDE if you create the sub-class (like Condo above), we’ll initially get an error:

“There is no default constructor available in ‘com.bwarner/Building'”

IntelliJ wants us to create our default constructor. By clicking on the class name “Condo” and then clicking Control+N, we’ll get the option to generate a constructor:

Above, IntelliJ has generated our default constructor. This pulled in our default fields from the base class (Building) and also added a way of initializing the fields using the keyword super.

Like “this” the “super” keyword is assigning values to the fields (in this case the base fields that we are extending) to the values sent in at the time of Class Instantiation.

For the Condo class, I want to add a boolean for hoa (home owners association), a boolean for sharedWall, an integer for number of Bathrooms and an integer for the number of bedrooms:

public class Condo extends Building {

    private boolean hoa;
    private boolean sharedWall;
    private int numberOfBedrooms;
    private int numberOfBathrooms;

    public Condo(int walls, int height, int squareFeet, int numberOfDoors, boolean skylight, String flooringMaterial,
                 boolean hoa, boolean sharedWall, int numberOfBathrooms, int numberOfBedrooms) {
        super(walls, height, squareFeet, numberOfDoors, skylight, flooringMaterial);
        this.hoa = hoa;
        this.sharedWall = sharedWall;
        this.numberOfBathrooms = numberOfBathrooms;
        this.numberOfBedrooms = numberOfBedrooms;
    }
    public boolean isHoa() {
        return hoa;
    }

    public void setHoa(boolean hoa) {
        this.hoa = hoa;
    }

    public boolean isSharedWall() {
        return sharedWall;
    }

    public void setSharedWall(boolean sharedWall) {
        this.sharedWall = sharedWall;
    }

    public int getNumberOfBedrooms() {
        return numberOfBedrooms;
    }

    public void setNumberOfBedrooms(int numberOfBedrooms) {
        this.numberOfBedrooms = numberOfBedrooms;
    }

    public int getNumberOfBathrooms() {
        return numberOfBathrooms;
    }

    public void setNumberOfBathrooms(int numberOfBathrooms) {
        this.numberOfBathrooms = numberOfBathrooms;
    }
}

Like a normal class, I added in the private fields specific to Condo. These fields are not accessible in the base class, only in this sub-class. Within the parameters of the initialization constructor, I appended those fields and using the “this” keyword, assigned the Condo class specific fields to the values passed in through the parameters.

Below it all I added in Getters & Setters for each field.

Calling Methods from Inherited Classes

Back in the base class (Building), I’ve added a method called getPrice:

    public void getPrice(int listingNumber){
        if(listingNumber == 1){
            System.out.println("Condo #1 is selling at $890,000");
        }else{ System.out.println("Not a Valid Listing")}
    }

Our sub-class Condo inherits this method. We don’t need to add anything to the Condo class to access it. It’s already available as part of the inheritance model.

From the Main class, we can instantiate the Condo class, and this Building class method will be available:

public class Main {

    public static void main(String[] args) {
	    Condo condoOne = new Condo(4, 10, 1800, 4, true, "Marble",
                true, false, 2, 3);

	    condoOne.getPrice(1);
    }
}


Leave a Reply

Your email address will not be published. Required fields are marked *