Java - (Inheritance|Class Hierarchy) - (Subclass|Superclass) - (Extends, Super) - (“is a” relationship)


See Object - Inheritance.

The Java programming language does not permit multiple inheritance, but interfaces provide an alternative.



The syntax for creating a subclass is simple. At the beginning of your class declaration, use the extends keyword, followed by the name of the class to inherit from:

class MountainBike extends Bicycle {

     // new fields and methods defining a mountain bike would go here


This gives MountainBike all the same fields and methods as Bicycle, yet allows its code to focus exclusively on the features that make it unique.


If your method overrides one of its superclass's methods, you can invoke the overridden method through the use of the keyword super.

You get the parent object with this statement:


The call to the constructor of the parent is made with this statement:


The Java Platform Class Hierarchy

The Object class, defined in the java.lang package, defines and implements behavior common to all classes—including the ones that you write. In the Java platform, many classes derive directly from Object, other classes derive from some of those classes, and so on, forming a hierarchy of classes.

All Classes in the Java Platform are Descendants of Object

At the top of the hierarchy, Object is the most general of all classes. Classes near the bottom of the hierarchy provide more specialized behavior.


The SuperClass: The bicycle

The superclass gets the abstract declaration.

package myBicycle;

public abstract class Bicycle {
	public Bicycle(String BicycleName) {
		System.out.println("Hello from the Constructor of Bicycle to the bicycle"+BicycleName);
	public int getTheNumberOfWheel() {
		return 2;
	public abstract String getTheTypeOfBicycle();

The Subclass: The MountainBike

The subclass MountainBike class extends the superclass Bicycle.

It then:

  • inherits the method getTheNumberOfWheel of the superclass Bicycle
  • (overwrites|implements) the abstract method getTheTypeOfBicycle() of the superclass with the @Override annotation
  • call the constructor of the bicycle superclass with the super method.
package myBicycle;

public class MountainBike extends Bicycle {

	public MountainBike(String bicycle) {

	public String getTheTypeOfBicycle() {
		return "Mountain";


The Client Application

package myClient;

import myBicycle.MountainBike;

public class myClient {

	public static void main(String[] args) {

		MountainBike myMountainBike = new MountainBike("my Blue Mountain");
		System.out.println("Number of Wheel: "+myMountainBike.getTheNumberOfWheel());
		System.out.println("Type           : "+myMountainBike.getTheTypeOfBicycle());


The Result

Running the client class will give as output:

Hello from the Constructor of Bicycle to the bicycle my Blue Mountain
Number of Wheel: 2
Type           : Mountain

Documentation / Reference

Powered by ComboStrap