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

Java Conceptuel Diagram

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





Discover More
Code Refactoring
Code - Refactoring

The process of modifying the code structure, without modifying its current behavior, is called Refactoring. Refactoring is a method that search to improve the code quality of a program. code duplication...
Card Puncher Data Processing
Data Type - Hierarchy

Hierarchy relationship in data type. The hierarchy of type defines how the types are implicitly converted. Implicit conversion is allowed for types from child to an ancestor. Example of Hierarchy...
Card Puncher Data Processing
How to develop a Task for Ant?

How to develop a Task for Ant A class is registered as an AnT task if it's provide a method with the signature “public void execute()”. The class doesn't need to extends no superclass and...
Java Conceptuel Diagram
Java - (Data Type|Type)

data type in the java world. The Java programming language is statically-typed, which means that all variables must first be declared before they can be used. The language supports four kinds...
Java Conceptuel Diagram
Java - (Descriptor|Metadata)

Example of (Descriptor|Metadata) implementation in java. The source of the descriptor can be: in the code in an external file (xml, properties, ...) or generated (see ) The java/lang/Class...
Java Conceptuel Diagram
Java - (Generic|Parameterized) type - (Class|Interface|Method) Parametrization

and Integer.class is of type Class Stronger type checks at compile time. Fixing compile-time errors is easier than fixing runtime errors, which can be difficult to find. The compiler can check the type...
Java Conceptuel Diagram
Java - Abstract Modifier

Abstract is a modifier that require override. An abstract class is a class that can't be instantiated. It's only purpose to be extended by other classes. See enum Abstract class provides a skeletal...
Classes Access
Java - Access Modifier (private, public, )

in Java. Access level modifiers determine whether other classes can use a particular field or invoke a particular method. There are two levels of access control: At the top level: public, or package-private...
Java Conceptuel Diagram
Java - Bounded Type Parameters

A generic method or class that operates on numbers will usedbounded type parameters to restrict the type parameters accepted. The extends keyword, followed by one or several upper bounds. where:...
Simple Class
Java - Class (Definition)

A java/lang/Classclass provides the blueprint for objects; you create an object from a class. All classes are derived from the Object class. A class declaration names the class and encloses the class...



Share this page:
Follow us:
Task Runner