Java Inheritance

Inheritance allows code reuse in Object oriented programming language.

Inheritance can be defined as the process where one class requires the properties (methods and fields) of another. With the use of inheritance the information is made manageable in a hierarchical order.

Inheritance in Java is supported using extends and implements keyword, extends keyword is used to inherit from another Java Class and allow to reuse functionality of Parent class. While implements keyword is used to implement Interface in Java.

The class which inherits the properties of other is known as subclass (derived class, child class) as the class whose properties are inherited is known as superclass (base class, parent class).


extends Keyword

extends is the keyword used to inherit the properties (methods and fields) of a class. When one class extends another class, it inherit all non private members including fields and method

class Super {

class Sub extends Super {

The super keyword

The super keyword is used in:

  • It is used to differentiate the members of superclass from the members of subclass, if they have same names.
  • It is used to invoke the superclass constructor from subclass. If a class is inheriting the properties of another class, the subclass automatically acquires the default constructor of the superclass. But if you want to call a parametrized constructor of the superclass, you need to use the super keyword

implements Keyword

Generally, the implements keyword is used with classes to inherit the properties of an interface. interface can never be extended by a class.

public interface Animal {

public class Mammal implements Animal {

public class Dog extends Mammal {

The instanceof Keyword

interface Animal {}

class Mammal implements Animal {}

public class Dog extends Mammal {
    public static void main(String[] args) {
          Mammal m = new Mammal();
          Dog d = new Dog();

          System.out.println(m instanceof Animal);
          System.out.println(d instanceof Mammal);
          System.out.println(d instanceof Animal);
// output is:
// true
// true
// true

Types of inheritance

The various type of inheritance as demonstrated below Types of inheritance

A very important fact to remember is that Java does not support multiple inheritance. This means, a class cannot extend more than one class.

However, a class can implement one or more interfaces.

Importance points about Inheritance in Java

  1. Inheritance in Java is supported using extends and implements keyword, extends keyword is used to inherit from another Java Class and allow to reuse functionality of Parent class. While implements keyword is used to implement Interface in Java. Implementing an interface in Java doesn’t actually meant for code reuse but provides Type hierarchy support. You can also use extends keyword when one interface extends another interface in Java.
  2. If you do not want to allow Inheritance for your class then you can make it final. final classes can not be extended in Java and any attempt to inherit final class will result in compile time error.
  3. Constructor in Java are not inherited by SubClass. In face Constructors are chained, first statement in constructor is always a call to another constructor, either implicitly or explicitly. If you don’t call any other constructor compiler will insert super(), which calls no argument constructor of super class in Java. this keyword represent current instance of class and super keyword represent instance of super class in Java.
  4. Inheritance in Java represents IS-A relationship. If you see IS-A relationship between your domain Objects and Classes then consider using Inheritance e.g. if you have a class called ProgrammingLanguage than Java IS-A ProgrammingLanguage and should inherit from ProgrammingLanguage class.
  5. Private members of Super class is not visible to Sub class even after using Inheritance in Java. Private members include any private field or method in Java.
  6. Java has a special access modifier known as protected which is meant to support Inheritance in Java. Any protected member including protected method and field are only accessible in Child class or Subclass outside the package on which they are declared. A class member declared protected becomes private member of subclass.
  7. Fields and methods with default (package) access modifiers can be accessed by subclasses only if the subclass is located in the same package as the superclass. Private fields and methods of the superclass can never be referenced directly by subclasses. They can, however, be referenced indirectly via methods reachable from the subclass (e.g default (package), protected and public methods).
  8. One of the risk of Inheritance in Java is that derived class can alter behavior of base class by overriding methods, which can compromise variants of base class e.g. if a malicious class overrides String’s equals method in Java to change the comparison logic, you may get different behavior when String reference variable points to that class. to prevent such malicious overriding, you can make your class final to disallow inheritance. But beware making a class final severely implements its client’s ability to reuse code. It make more sense from security perspective and that’s one of the reason Why String is final in Java.
  9. Use @Override annotation while overriding super class’s method in subclass. This will ensure a compile time check on whether overriding method actually overrides super class method or not. Its common mistake to overload a method instead of overriding it mostly when super class method accept Object type, common examples are equals method, compareTo method and compare() method in Java.

When to use Inheritance in Java

  • General policy to decide whether to use Inheritance or not is to check IS-A relationship. E.g. HashSet IS-A Set.
  • Conversely if you find HAS-A relationship between two classes than use Composition e.g. Car HAS-A Seat.

Leave a Reply

Your email address will not be published. Required fields are marked *