Sotirios-Efstathios Maneas

About Sotirios-Efstathios Maneas

Sotirios-Efstathios (Stathis) Maneas is a postgraduate student at the Department of Informatics and Telecommunications of The National and Kapodistrian University of Athens. His main interests include distributed systems, web crawling, model checking, operating systems, programming languages and web applications.

Java Inheritance example

In this tutorial we will discuss about the inheritance in Java. The most fundamental element of Java is the class. A class represents an entity and also, defines and implements its functionality. In Java, classes can be derived from other classes, in order to create more complex relationships.

A class that is derived from another class is called subclass and inherits all fields and methods of its superclass. In Java, only single inheritance is allowed and thus, every class can have at most one direct superclass. A class can be derived from another class that is derived from another class and so on. Finally, we must mention that each class in Java is implicitly a subclass of the Object class.

 
Suppose we have declared and implemented a class A. In order to declare a class B that is derived from A, Java offers the extend keyword that is used as shown below:

class A {
	//Members and methods declarations.
}

class B extends A {
	//Members and methods from A are inherited.
	//Members and methods declarations of B.
}

Java supports only public inheritance and thus, all fields and methods of the superclass are inherited and can be used by the subclass. The only exception are the private members of the superclass that cannot be accessed directly from the subclass. Also, constructors are not members, thus they are not inherited by subclasses, but the constructor of the superclass can be invoked from the subclass. In order to call the constructor of the superclass Java provides the keyword super, as shown below:

class A {
	public A() {
		System.out.println("New A");
	}
}

class B extends A {
	public B() {
		super();
		System.out.println("New B");
	}
}

A sample example to present the inheritance in Java is shown below:

Animal.java:

public class Animal {
	public Animal() {
		System.out.println("A new animal has been created!");
	}
	
	public void sleep() {
		System.out.println("An animal sleeps...");
	}
	
	public void eat() {
		System.out.println("An animal eats...");
	}
}

Bird.java:

public class Bird extends Animal {
	public Bird() {
		super();
		System.out.println("A new bird has been created!");
	}
	
	@Override
	public void sleep() {
		System.out.println("A bird sleeps...");
	}
	
	@Override
	public void eat() {
		System.out.println("A bird eats...");
	}
}

Dog.java:

public class Dog extends Animal {
	public Dog() {
		super();
		System.out.println("A new dog has been created!");
	}
	
	@Override
	public void sleep() {
		System.out.println("A dog sleeps...");
	}
	
	@Override
	public void eat() {
		System.out.println("A dog eats...");
	}
}

MainClass.java:

public class MainClass {
	public static void main(String[] args) {
		Animal animal = new Animal();
		Bird bird = new Bird();
		Dog dog = new Dog();
		
		System.out.println();
		
		animal.sleep();
		animal.eat();
		
		bird.sleep();
		bird.eat();
		
		dog.sleep();
		dog.eat();
	}
}

In this example we created three distinct classes, Animal, Dog and Bird. Both Dog and Bird classes extend the Animal class and thus, they inherit its members and methods. Moreover, as we can see below, each class overrides the methods of Animal and thus, both the Dog and Bird classes redefine the functionality of Animal’s methods.

A sample execution is shown below:

A new animal has been created!
A new animal has been created!
A new bird has been created!
A new animal has been created!
A new dog has been created!

An animal sleeps...
An animal eats...
A bird sleeps...
A bird eats...
A dog sleeps...
A dog eats...

A nested class has access to all the private members of its enclosing class, both fields and methods. Therefore, a public or protected nested class inherited by a subclass has indirect access to all of the private members of the superclass.

As already mentioned, a subclass inherits all of the public and protected members of its superclass. If the subclass is in the same package as its superclass, it also inherits the package-private members of the superclass. The inheritance in Java provides the following features:

  • You can declare a field in the subclass with the same name as the one in the superclass thus, hiding it. This is called shadowing.
  • You can declare new fields in the subclass that are not in the superclass.
  • You can write a new instance method in the subclass that has the same signature as the one in the superclass, thus overriding it.
  • You can declare new methods in the subclass that are not in the superclass.

Final abstract classes can exist in a hierarchy of types. For more information about abstract classes and how are used in Java, please refer to the Java abstract tutorial here.

Inheritance and Casting

When a class B extends a class A, then an instance of the B class is of type B, but also of type A. Thus, such an instance can be used in all cases where a class B or class A object is required. However, the reverse is not true! An instance of the class A is of course of type A, but it is not of type B.

Thus, we can use casting between the instances of classes. The cast inserts a runtime check, in order for the compiler to safely assume that the cast is used properly and is correct. If not, a runtime exception will be thrown.

A simple example that demonstrates the usage of casting is shown below:

Animal a1 = new Dog();
Animal a2 = new Bird();
		
a1.eat();
a2.sleep();
		
// The following statements are incorrect.
// Dog d = new Animal();
// Bird b = new Animal();

A sample execution is shown below:

A dog eats...
A bird sleeps...

The instanceof operator

The instanceof operator can be used, in order to determine if an object is a valid instance of a specific type. It can be used to test if an object is an instance of a class, an instance of a subclass, or an instance of a class that implements a particular interface. A simple example is shown below:

Dog d = new Dog();
if(d instanceof Animal) {
	Animal a = (Animal) d;
	a.sleep();
}
d.sleep();

Interfaces

An interface in Java is an abstract type that is used to declare and specify a set of public methods and members. An interface can be implemented by a class. In this case, the class must provide an implementation for every method defined in the interface. A significant advantage of using interfaces is the fact that in Java, multiple interfaces can be implemented by a single class.

A sample example that uses both classes and multiple interfaces is shown below:

BasketballTeam.java:

public interface BasketballTeam {
	public void printBasketballName();
}

FootballTeam.java:

public interface FootballTeam {
	public void printFootballName();
}

Team.java:

public class Team implements BasketballTeam, FootballTeam {

	private String name = null;
	
	public Team(String name) {
		this.name = name;
	}

	@Override
	public void printFootballName() {
		System.out.println("Football Team: \"" + name + " F.C.\"");
	}

	@Override
	public void printBasketballName() {
		System.out.println("Basketball Team: \"" + name + " B.C.\"");
	}
	
	public static void main(String[] args) {
		Team t = new Team("Team A");
		t.printBasketballName();
		t.printFootballName();
	}
}

A sample execution is shown below:

Basketball Team: "Team A B.C."
Football Team: "Team A F.C."

Download the Eclipse Project

The Eclipse project of this example: InheritanceExample.zip.

This was a tutorial about the inheritance in Java.

Don’t forget to check out our Academy premium site for advanced Java training!

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.
Examples Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy | Contact
All trademarks and registered trademarks appearing on Examples Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Examples Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close