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

> Procedural Languages > Java

1 - About

See Object - Inheritance.

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

Advertising

3 - Keyword

3.1 - 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.

3.2 - 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();

4 - 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.

Advertising

5 - Example

5.1 - 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();
 
}

5.2 - 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";
	}
 
}

5.3 - 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());
 
	}
 
}

5.4 - 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
Advertising

6 - Documentation / Reference

lang/java/subclass.txt · Last modified: 2019/02/18 13:49 by gerardnico