Inheritance

Java Inheritance (Subclass and Superclass):

In Java, it is possible to inherit attributes and methods from one class to another. We group the “inheritance concept” (child object/ class behaves like parent object/class) into two categories:

  • subclass (child) – the class that inherits from another class
  • superclass (parent) – the class being inherited from

To inherit from a class, use the extends keyword.

A subclass inherits all the members (fields, methods, and nested classes) from its superclass. Constructors are not members, so they are not inherited by subclasses, but the constructor of the superclass can be invoked from the subclass.[TBD]

Types of inheritance in java:

On the basis of class, there can be three types of inheritance in java: single, multilevel and hierarchical.

In java programming, multiple and hybrid inheritance is supported through interface only. We will learn about interfaces later.

When one class inherits multiple classes, it is known as multiple inheritance. For Example:

Single Inheritance:

When a class inherits another class, it is known as a single inheritance. In the example given below, Dog class inherits the Animal class, so there is the single inheritance.

Multilevel Inheritance:

When there is a chain of inheritance, it is known as multilevel inheritance.

Hierarchical Inheritance:[TBD]

When two or more classes inherits a single class, it is known as hierarchical inheritance.

Multiple Inheritance (Through Interfaces):[TBD]

 In Multiple inheritances, one class can have more than one superclass and inherit features from all parent classes. Please note that Java does not support multiple inheritances with classes. In java, we can achieve multiple inheritances only through Interfaces.

Hybrid Inheritance(Through Interfaces): [TBD]

It is a mix of two or more of the above types of inheritance. Since java doesn’t support multiple inheritances with classes, hybrid inheritance is also not possible with classes. In java, we can achieve hybrid inheritance only through Interfaces.

Why And When To Use “Inheritance”?

It is useful for code reusability: reuse attributes and methods of an existing class when you create a new class.

Important terminology: 

  • Super Class: The class whose features are inherited is known as a superclass(or a base class or a parent class).
  • Sub Class: The class that inherits the other class is known as a subclass(or a derived class, extended class, or child class). The subclass can add its own fields and methods in addition to the superclass fields and methods.
  • Reusability: Inheritance supports the concept of “reusability”, i.e. when we want to create a new class and there is already a class that includes some of the code that we want, we can derive our new class from the existing class. By doing this, we are reusing the fields and methods of the existing class.

The Java Platform Class Hierarchy:[TBD]

The Object class, defined in the java.lang package, defines and implements behavior common to all classes—including the ones that you write. In the Java platform, many classes derive directly from Object, other classes derive from some of those classes, and so on, forming a hierarchy of classes.

All Classes in the Java Platform are Descendants of Object

At the top of the hierarchy, Object is the most general of all classes. Classes near the bottom of the hierarchy provide more specialized behavior.

Reference:

https://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html

https://www.geeksforgeeks.org/inheritance-in-java/

https://www.javatpoint.com/inheritance-in-java

https://www.w3schools.com/java/java_inheritance.asp

Leave a comment

Design a site like this with WordPress.com
Get started