Saturday, January 9, 2021

Inheritance in Java

Inheritance is one of the four fundamental OOP concepts. The other three being- Encapsulation, Polymorphism, Abstraction.


Inheritance Concept

Inheritance is a mechanism, by which one class acquires, all the properties and behaviors of another class. The class whose members are inherited is called the Super class (or base class), and the class that inherits those members is called the Sub class (or derived class).

The relationship between the Super and inherited subclasses is known as IS-A relationship.

As Example- If Shape is a super class and there are subclasses Circle and Rectangle derived from the Shape super class then Circle IS-A Shape and Rectangle IS-A shape.

Inheritance in Java

Inheritance in Java is done using extends keyword.

  • extends– A class in Java inherits from another class using extends keyword. In Java, you extend an already existing class or abstract class. Syntax of inheritance in Java using extends keyword is as follows-
    class Sub_class extends Super_class{
        ...
        ...
    }
    

Inheritance in Java - What is inherited

In Java when a class is extended, sub-class inherits all the public, protected and default (Only if the sub-class is located in the same package as the super class) methods and fields of the super class.

Inheritance in Java - What is not inherited

  • Private fields and methods of the super class are not inherited by the sub-class and can’t be accessed directly by the subclass.
  • Constructors of the super-class are not inherited. There is a concept of constructor chaining in Java which determines in what order constructors are called in case of inheritance.

Java example code showing inheritance using extends

//Super class
class A{
 public int a = 10;
 private int b = 15;
 public void displayFromA(){
  System.out.println("Value of a " + a);
 }
}

// sub class 
class B extends A{
 int c = 20;
 public void displayFromB(){
  System.out.println("Value of field a " + a);
  // This line will give compiler error as b 
  // is private and not visible
  System.out.println("Value of field b " + b);
  // Calling inherited method directly as if 
  // it is a method of this class, because it is inherited 
  // from super class
  displayFromA();
  // ok, field of this class
  System.out.println("Value of field c " + c);
 }
}

public class InheritanceDemo {
 public static void main(String[] args) {
  B b = new B();
  // calling the method of class B using class B object
  b.displayFromB();
  // calling the method of class A using class B object
  b.displayFromA();

 }
}

Output

Value of field a 10
Value of a 10
Value of field c 20
Value of a 10

It can be seen how methods and fields of super class A are visible in class B, private field b is not visible.

Types of Inheritance

Going by OOPS concepts there are 5 types of Inheritance-

Single Inheritance

In this type of inheritance a sub class is derived from a single Super class.
Single Inheritance in Java

Multi-level inheritance

In this type of inheritance, a subclass is created from another sub class thus having levels of hierarchy.
Multi-level inheritance in java

Multiple Inheritance

In this type of inheritance, a sub class is created using more than one super class. Note Java does not support multiple inheritance.
Multiple Inheritance

Hierarchical Inheritance

In this type of inheritance more than one sub classes are created from the same super class.
Hierarchical Inheritance

Hybrid inheritance

This type of inheritance is the combination of more than one inheritance type as described above. Hence, it may be a combination of Multilevel and Multiple inheritance or Hierarchical and Multilevel inheritance or Hierarchical and Multiple inheritance.
Hybrid inheritance

Multiple Inheritance in Java

Java doesn’t support multiple inheritance which means a class can extend at most one super class only. Though, a class can implement more than one interface.

Points to note-

  • In Java inheritance is done using extends keyword.
  • Constructors of the super class are not inherited by the sub class.
  • Private fields and methods of the superclass are not inherited by the subclass and can’t be directly accessed.
  • Members with default access in the super class are inherited by the subclass only if they both reside in the same package.
  • Using final keyword in Java, a class can restrict that no class can extend it.
  • If both super class and sub class have a method with same signature then subclass method is said to be overriding the super class method.
  • Multiple inheritance is not allowed in Java, a class can extend at most one super class. Though a class can implement more than one interface and that's one way to get multiple inheritance in Java.

That's all for this topic Inheritance in Java. If you have any doubt or any suggestions to make please drop a comment. Thanks!

>>>Return to Java Basics Tutorial Page


Related Topics

  1. Method Overriding in Java
  2. Interface in Java With Examples
  3. Java Abstract Class and Abstract Method
  4. Association, Aggregation and Composition in Java
  5. Java OOP Interview Questions And Answers

You may also like-

  1. Difference Between Abstract Class And Interface in Java
  2. What are JVM, JRE and JDK in Java
  3. this Keyword in Java With Examples
  4. static Keyword in Java With Examples
  5. Core Java Basics Interview Questions And Answers
  6. How to Loop or Iterate an Arraylist in Java
  7. Creating a Thread in Java
  8. Try-With-Resources in Java With Examples

1 comment:

  1. Even i have made a post on this topic have a look at it and please let me know if i need to make any changes https://letml.blogspot.com/2019/02/inheritancejavaoopconcepts.html

    ReplyDelete