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:
super;
The call to the constructor of the parent is made with this statement:
this.super();
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 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 MountainBike class extends the superclass Bicycle.
It then:
package myBicycle;
public class MountainBike extends Bicycle {
public MountainBike(String bicycle) {
super(bicycle);
}
@Override
public String getTheTypeOfBicycle() {
return "Mountain";
}
}
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());
}
}
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