Inheritance: Superclass and Subclass Tutorial

I will begin this tutorial with the definition of the word derive, to receive or obtain from a source or origin. In order to understand inheritance, you will need to understand the relationship between a superclass and a subclass. A subclass is derived from a superclass; the subclass name is located to the left of the extends keyword. The subclass inherits the members of a superclass. The superclass name is located to the right of the extends keyword, and it is the class that will allow access to its members. The superclass does not inherit the members of a subclass.
In Java, a class can only have one direct superclass. If a class declaration does not contain the extends keyword, Java implicitly extends the Object class. When it comes to inheritance, the Object class is the granddaddy of them all. The Object class is at the top of the Java food chain - it has no superclass. A class can extend a class that extends another class that extends another class ... eventually a class will implicitly extend the Object class.

class Nation { // superclass of State, subclass of Object. Object is implicitly the superclass
      String nationName = "";
}
class State extends Nation {
// superclass of County, subclass of Nation
      String stateName = "";
}
class County extends State {
// superclass of City, subclass of State
      String countyName = "";
}
class City extends County {
// subclass of County
      String cityName = "";
}



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 SuperSub
C:\Java>cd SuperSub
C:\Java\SuperSub>Notepad SuperSub.java

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


class SuperSub {
    public static void main(String args[]) {
        City oCity = new City();        
        oCity.cityName = "Orlando";
        oCity.countyName = "Orange County";    
        oCity.stateName = "Florida";
        oCity.nationName = "USA";
        System.out.println("I live in " + oCity.cityName + ", " + oCity.countyName + ", " + oCity.stateName + ", " + oCity.nationName);

        County oCounty = new County();
        oCounty.countyName = "Norfolk County";
        oCounty.nationName = "England";
        //oCounty.cityName = "Hemsby"; // error - County is the superclass of City. Superclasses do not have access to subclass members.
        System.out.println(oCounty.countyName + ", " + oCounty.nationName + ", is a wonderful place to visit.");

        
    }
}

class Nation extends Object { // superclass of State, subclass of Object
    String nationName = "";
}

class State extends Nation { // superclass of County, subclass of Nation
    String stateName = "";
}

class County extends State { // superclass of City, subclass of State
    String countyName = "";
}

class City extends County { // subclass of County
    String cityName = "";
}


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


C:\Java\SuperSub>javac SuperSub.java
C:\Java\SuperSub>java SuperSub
I live in Orlando, Orange County, Florida, USA
Norfolk County, England, is a wonderful place to visit.


Final thoughts

You should now have a clear understanding of the relationship between a superclass and subclass. It is important to remember that the Object class is at the top of the food chain and it is implicitly extended when the extends keyword is not in the class declaration. In future tutorials, the Object class will be the key to understanding more advanced concepts.


Tutorials