Introduction to Inheritance Tutorial

Inheritance is one of the key components to understanding object-oriented programming. The word inheritance implies receiving certain possessions upon the death of a family member or a close friend. In real life, one can draw up a will specifying who will receive a possession and who will not. One can even designate that possessions can be shared or even say that nobody can have them. Many lawyers make a decent living writing up wills and acting as an executor.
In Java, the concept of inheritance is similar; the relatives are the classes, the possessions are its members, the family is a package. A will specifies the ownership and access to possessions, whereas in Java, the ownership and access to class members is handled by access modifiers. There is one major difference between real life and Java - no classes have to die for inheritance to occur. The topic of inheritance is quite extensive and will apply to many advanced features of Java. The goal of this tutorial is to lay the first building block in the foundation of understanding inheritance - the extends keyword.
The extends keyword 'ties' the members of a parent class to a child class. The extends keyword is placed after the name of the child class and before the name of the parent class that we are inheriting from. The parent class is known as the superclass and the child class is known as the subclass.
(class) (className subclass) (extends) (className superclass) ({class body})



Open the command prompt (CMD - see the Getting Started ) and type in the following commands.

C:\Windows\System32>cd \
C:\>md Java
C:\>cd Java
C:\Java>
C:\Java>md InheritanceIntro
C:\Java>cd InheritanceIntro
C:\Java\InheritanceIntro>Notepad InheritanceIntro.java

Copy and Paste, or type the following code into Notepad and be sure to save the file when you are done.


class InheritanceIntro {
    public static void main(String args[]) {
        CardboardBox cb1 = new CardboardBox();
        CardboardBox cb2 = new CardboardBox();

        cb1.length = 5;
        cb1.height = 5;
        cb1.width = 5;
        cb1.unitOfMeasurement = "inches";
        cb1.color = "brown";

        cb2.length = 16;
        cb2.height = 41;
        cb2.width = 10;
        cb2.unitOfMeasurement = "centimeters";
        cb2.color = "white";

        System.out.println("Box #1 is a " + cb1.color + " cardboard box with a volume of: " + cb1.returnVolume() + " cubic " + cb1.unitOfMeasurement);
        System.out.println("Box #2 is a " + cb2.color + " cardboard box with a volume of: " + cb2.returnVolume() + " cubic " + cb2.unitOfMeasurement);
    }
}

class Box {
    int length = 0;
    int height = 0;
    int width = 0;
    String unitOfMeasurement = "";

    int returnVolume() {
        return (length * height * width);
    }
}

class CardboardBox extends Box { // Cardboard box is the child or subclass, Box is the parent or superclass
    
    String color=""; // only member declared in the Cardboardbox class
    // the 'extends Box' above inherits all of the members of the Box class 
}
    

Now switch back to the command prompt (CMD) and type in javac InheritanceIntro.java and press Enter.
Now type in java InheritanceIntro and press Enter.


C:\Java\InheritanceIntro>javac InheritanceIntro.java
C:\Java\InheritanceIntro>java InheritanceIntro
Box #1 is a brown cardboard box with a volume of: 125 cubic inches
Box #2 is a white cardboard box with a volume of: 6560 cubic centimeters


Final thoughts

There is a lot to take in from this tutorial. Don't worry about understanding everything; I will be reiterating many of these concepts in future tutorials. Remember, this tutorial was just an introduction to inheritance.


Tutorials