In the article What is Inheritance in Java, you understand the WHAT, the WHY and a little about the HOW of inheritance in the Java programming language.

In this article, we are going to dive deeper into the HOW of inheritance with the following 12 rules and examples about inheritance in Java:

 

1. A class implements an interface:

When a class implements an interface, it has to provide implementation details for all the methods of that interface (overriding).

Consider the following interface:

public interface Animal {
	public void eat();

	public void move();

	public void sleep();

}
And here is a class that implements this interface:

public class Dog implements Animal {

	public void eat() {
		System.out.print("Eating...");
	}

	public void move() {
		System.out.print("Moving...");
	}

	public void sleep() {
		System.out.print("Sleeping...");
	}
}
Here, you can see that the Animal interface declares 3 abstract methods with empty body. Being a subtype, the Dog class must provide implementation details for these methods - this is called overriding

Learn more: 12 Rules of Overriding in Java You Should Know

Besides the responsibility of overriding the methods from the super interface, the Dog class can also implement methods of its own:

public class Dog implements Animal {
	// overrides methods from Animal interface..
	
	
	// dog-specific behavior
	public void bark() {
		System.out.print("Gow Gow!");
	}
}


  

2. An abstract class implements an interface:

When an abstract class implements an interface, it’s not required to override methods of the super interface. But the first non-abstract class in the inheritance tree must override the methods.

Consider the following interface:

public interface Moveable {
	public void start();

	public void stop();

	public void move();
}
And the abstract subclass:

public abstract class Vehicle implements Moveable {
	public void start() {
		System.out.print("Starting...");
	}

	public void stop() {
		System.out.print("Stopping...");
	}
}
As the Vehicle is abstract, it’s legal to override only two methods from the Moveable interface. And the move() method must be implemented by the first non-abstract in the inheritance tree.

Suppose that Car is a subclass of Vehicle, so we have an inheritance tree like this:

                Moveable > Vehicle > Car

Since Car is a non-abstract class, it must implement the move() method of the Moveable interface:

public class Car extends Vehicle {
	public void move() {
		System.out.print("Moving by engine...");
	}
}
 

3. A class extends another class:

When a class extends another class, the subclass inherits all the public and protected members of the super class. The default members are inherited only in the same package.

Let’s see the following Car class:

public class Car {
	String name;
	private int age;
	protected int numberOfWheels = 4;

	protected void start() {
		System.out.print("The car is starting...");
	}

	public void move() {
		System.out.print("The car is moving...");
	}

	private void test() {
		System.out.print("The car is being tested...");
	}
}
And the following is the Truck class:

public class Truck extends Car {
	public Truck() {
		numberOfWheels = 8;
		name = "Truck";
	}
}
Let’s look at the constructor of the Truck class - it can access the protected field numberOfWheels and the default field name. However, it cannot access the private field age. We can write:

Truck truck = new Truck();
truck.start();
truck.move();
But the Java compiler will complain if we try to invoke the private method test():

truck.test();	// COMPILE ERROR, since private member is not inherited
 

4. An interface extends another interface:

When an interface extends another interface, the sub interface inherits all the methods declared in the super interface, and it’s free to re-declare the methods or not. For example:

public interface Moveable {
	public void start();

	public void stop();

	public void move();
}
And here is a sub interface:

public interface Controllable extends Moveable {
	public void turn();

	public void brake();
}
 

5. A class extends another class and implements another interface:

In Java, we can have a class inherits both an interface and a class directly. Consider the following example:

public interface Moveable {
	public void move();
}

public class Car {
	protected void start() {
		System.out.print("Starting...");
	}

	public void stop() {
		System.out.print("Stopping...");		
	}
}

public class Truck extends Car implements Moveable {
	public void move() {
		System.out.print("Moving...");
	}
}
In this case, the Truck class inherits the start() and stop() methods of its super class Car, and has to override the move() method from its super interface Moveable.

In practice with Swing development, it’s common to have a class extended a JFrame and implements an event listener interface, for example:

public class MyFrame extends JFrame implements ActionListener {
}
 

6. Multiple inheritance of state is not allowed:

Remember that Java does not allow a class inherits two or more classes directly. To understand why multiple inheritance is not allowed, consider the following example:

public class A {
	public void methodA() {

	}

	public void foo() {

	}
}


public class B {
	public void methodB() {

	}

	public void foo() {

	}
}
Suppose that we want to write a class C that extends both A and B like this:

public class C extends A, B {
	public void methodC() {
		foo();
	}
}
As you can see, both A and B has a method called foo(), so which foo() method the class C invokes exactly? from A or B? This case is ambiguous hence Java does not allow.

 

7. Multiple inheritance of type is allowed:

This means Java does allow multiple inheritance between interfaces. For example:

public interface X {
	public void methodX();
}

public interface Y {
	public void methodY();
}

public interface Z extends X, Y {
	public void methodZ();
}
This is allowed because interfaces do not have concrete methods, thus there is no ambiguity.

Likewise, we can have a class implements multiple interfaces:

public class Sub implements X, Y, Z {
	public void methodX() { }

	public void methodY() { }

	public void methodZ() { }
}
 

8. Type Reference and Declaration:

One interesting point with inheritance in Java is that, we can use the super type when creating a new object of the sub type.

Suppose Car is the parent class of the Truck class as shown in the following code:

public class Car {
	public void move() {
	}
}

public class Truck extends Car {
	public void load() {
	}
}
It’s legal to write:

Car vehicle = new Truck();
The rule is: the type on the left side can be super type of the actual object in the right side. Note that the only methods we can invoke are the ones declared in the super type, for example:

vehicle.move();	//okay, since Truck inherits Car
Also note that, if the subclass declares a new method, we cannot invoke it using the super type reference:

vehicle.load();	// compile error
It’s illegal to create a new Car object like this:

Truck car = new Car();	// Compile error
Why? It’s because Truck is the subclass which can declare new methods other than the ones inherited from the Car class, whereas the actual object is of type Car. We may write this:

car.load();
It’s seems okay because the Truck class declares the load() method. But in this case, the actual object is Car which does not have the load() method.

  

9. The super keyword:

In Java, the super keyword is used to access members of the super type from the sub type. For example:

public class Super {
	protected void bar() {
		System.out.println("Super.bar()...");
	}

}

public class Sub extends Super {
	public void bar() {
		System.out.println("Sub.bar()...");
	}


	public void test() {
		super.bar();
		this.bar();
	}
}
Here, we can see that both classes have a method called bar() (overriding). Therefore, if we want to invoke the super’s version of the bar() method, the super keyword is used.

 

10. Only public and protected members are inherited:

Private members are not inherited. Default members are inherited only if both the subclass and super class are in the same package, otherwise default members are not inherited.

 

11. Constructors are not inherited:

The sub class cannot inherit constructors from its super class. Consider the following example:

public class Super {
	protected String text;

	public Super(String text) {
		this.text = text;
	}
}

public class Sub extends Super {

}
We can create a new instance of Super like this:

Super super = new Super(“Hello”);	//OK
But we cannot do the same for the Sub class:

Sub sub = new Sub(“Hello”);	//Compile error, since constructor is not inherited
The solution is to write a constructor for the sub class and call the super’s constructor, for example:

public class Sub extends Super {

	public Sub(String text) {
		super(text);
	}
}
Learn more: 9 Rules about Constructors in Java

 

12. Object is the base class of all classes in Java:

Whenever you use a class in Java, the Java compiler automatically makes the class inherited the Object class. In other words, Object is the top class in any inheritance tree.

For example, when we write a class like this:

public class A {
}
Then the compiler make it extended the Object class implicitly:

public class A extends Object {
}
You know, the Object class provides some behaviors which are common to all objects such as equals() and hashCode().

 

Related Tutorials:


About the Author:

is certified Java programmer (SCJP and SCWCD). He started programming with Java in the time of Java 1.4 and has been falling in love with Java since then. Make friend with him on Facebook and watch his Java videos you YouTube.



Add comment

   


Comments 

#6Pulkit Sharma2020-09-04 00:10
in point 8 it's executing child class method not the parent class
Quote
#5Nam2020-02-16 16:09
Hi Tapan,
In the point 3, it causes compile error because the test() method is private in the Car class, so the Truck class cannot inherit it.
Quote
#4Tapan2020-02-12 21:55
3. A class extends another class:

In this point there should not be any compiler error as String name; by default a public member
Quote
#3Nam2016-08-15 00:51
Quoting karam:
for point 9, there is bar() method but in explanation its foo(), please correct that. although very good information for someone looking for quick information on overloading and overriding.


Very good catch. Updated. Thank you!
Quote
#2karam2016-08-14 10:37
for point 9, there is bar() method but in explanation its foo(), please correct that. although very good information for someone looking for quick information on overloading and overriding.
Quote