July 5, 2022

Inheritance in Java – OOPS Concepts

In this post we’ll see the usage of Inheritance in Java which is one of the Object Oriented principle.

What is inheritance

Inheritance is one of the four fundamental principles of Object Oriented Programming along with abstraction, polymorphism and encapsulation.

Inheritance is a mechanism by which a class inherits the properties and methods of another class.

Super Class- The class whose members are inherited by another class is known as the super class (also base class or parent class).

Sub Class- The inheriting class is known as the sub class (also as derived or child class). Sub class can also add fields and methods of its own other than what it inherits from the parent class. Sub class can also override the method of the parent class.

Inheritance in Java

Inheritance in Java is done using extends keyword.
class ChildClass extends ParentClass{
  ....
  ....
}

By using extends keyword class in Java can inherit from another class or abstract class.

How inheritance in Java helps

1- By using inheritance in Java you can design modular (hierarchical) classes moving from general to more specific.

As example– Shape – Rectangle

Here you start with a general class Shape which has sides and area. Rectangle is more specific class.

Same way: Shape – Triangle

Another example is: Animal – Mammal – Dog
Or Animal – Bird – Eagle

2- Inheritance helps in writing reusable code. Since you design from general to more specific using inheritance so you can put the fields and methods which are used by all the classes in the parent class and the more specific implementations in the respective child classes. That way your code is more reusable.

3- Inheritance represents a IS-A relationship. So Rectangle IS-A Shape, Dog IS-A Mammal and also Dog IS-A animal. So using inheritance you can also achieve polymorphism as using a reference of Super class type you can refer to the sub class type.

As example- Shape s = new Rectangle();

Inheritance in Java – What is inherited and what is not

  • public- All public fields and methods of the super class are inherited without any constraint.
  • protected- All protected fields and methods are inherited even if sub class is in another package.
  • default- All fields and methods with default access of the super class are inherited only if sub class is in the same package.
  • private- Private fields and methods are not inherited.
  • Constructors– Constructors of the super class are not inherited but the default no-arg constructor of the super class will be called while creating object of the child class or you can explicitly called the constructor of the super class from the child class constructor using the super keyword.

Inheritance in Java example

class A {
  private int x;
  private int y;
  public int p;

  A(int x, int y){
    this.x = x;
    this.y = y;		
  }
  public void display() {
    System.out.println("In display method of super class");
    System.out.println("x= " + x + " y= " + y);
  }
}

public class B extends A{
  int z;
  public B(int x, int y, int z) {
    //invoking parent class constructor
    super(x, y);
    this.z = z;	
  }
  public void display() {
    // invoking display method of parent class
    super.display();
    System.out.println("In display method of child class");
    System.out.println("z= " + z);
    
  }
  public static void main(String[] args) {
    B obj = new B(5, 6, 7);
    // not possible as x is private in super class
    //obj.x = 6;
    // public field of super class can be acceessed
    obj.p = 140;
    obj.display();		
  }
}

In the example code class B extends class A. From the constructor of class B constructor of class A is called using the super keyword to initialize the fields of class A.

In class B there is a method with same name display() as in class A, which means display() method in class B overrides the method in class A. From the display() method of class B, class A’s display() method is called that way you are using the logic already written in the method of class A rather than writing it in class B again.

Object class – Super class of all the Classes

Each and every class in Java either explicitly or implicitly extends Object class, in the java.lang package. Object class in Java sits at the top of the class hierarchy tree in Java.

Types of inheritance

As per the object oriented concepts there are five types of inheritance. Note that not all types on inheritance are supported in Java.

Single inheritance

In Single inheritance a sub-class extends a single super class.

Single Inheritance

Single inheritance in Java example-

Class B extends A

Multilevel inheritance

Multilevel inheritance denotes a hierarchy where one class extends another which in turn is extended by another and so on.

Multilevel inheritance

Multilevel inheritance in Java example–

Class B extends A
Class C extends B

Hierarchical Inheritance

In hierarchical inheritance more than one sub classes are created from the same super class.

Hierarchical inheritance in Java example–

Class B extends A
Class C extends A

Multiple inheritance

In multiple inheritance a class extends more than one class.

Multiple inheritance

Java does not support multiple inheritance so a Java class cannot extend more than one class.

Hybrid inheritance

If you combine more than one types of inheritance that is a hybrid inheritance. Hybrid inheritance can be a combination of multiple inheritance and hierarchical, multilevel and multiple and so on.

Hybrid inheritance

That's all for the topic Inheritance in Java – OOPS Concepts. If something is missing or you have something to share about the topic please write a comment.


You may also like

No comments:

Post a Comment