Table of Contents

About

See Object - Inheritance.

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

Keyword

Extends

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.

Super

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:

super;

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

this.super();

The Java Platform Class Hierarchy

Superclass Eclipse Wizard New Java Class

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.

Classes Object

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.

Example

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) {
		super(bicycle);
	}

	@Override
	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