What is Inheritance in Java?

One of the aunty in my neighbor said, my nose looks like my dad. My mom says your behavior is the same as your father; both of you don’t listen to me. There are lots of anecdotes to tell about my similarity with my kin. You must have received a similar kind of compliments from the persons in your life. Let’s not go to the biological terms, but simply, they can find similarities because you inherited some characters from your parents. Additionally, you also develop some unique attributes, which your child is going to inherit. It’s all about Inheritance. When we are talking about inheritance in Java which means there will be child class and parent class, the object of child class is going to inherit objects and properties of the parent class, along with retaining some unique properties of the child class itself. Thus, Inheritance in Java is the first OOPs topic which we are going to discuss on inviul.com.

We are going to use some previously discussed techniques here, please find the links of the article below:

Inheritance in Java

It is the technique in which a child object carries all the properties of its parent object. This mechanism is achieved by building a child class over an existing class which we technically call as parent class by using extends keyword in Java. In this process, a child class can add new methods as well.

The process of implementing Inheritance in Java makes code reusable, readable, and efficient. The entire flow of the parent-child relationship technically called the IS-A relationship.

We are going to achieve Runtime polymorphism through Inheritance. Technically, we implement Method Overriding to achieve this as runtime polymorphism.

Terminologies:

Sub Class: This is a child class that is built over the existing class called a parent class.

Super Class: This is a parent class which is extended by the child class. The references of the super class are defined by super keyword.

Sample Program

package com.sample;

public class ChildClass extends ParentClass {
    
}

Types of Successful Inheritance in Java

Inheritance in Java

1. Single Level Inheritance: When a child class extends only one parent class and there is no further inheritance.

Sample Program

package com.sample;

class Shape{
    void draw(){System.out.println("shape draw...");}
}
class Rectangle extends Shape{
    void shapeName(){System.out.println("Rectangle...");}
}
public class Inheritence{
    public static void main(String args[]){
        Rectangle r=new Rectangle();
        r.shapeName();
        r.draw();
    }}

2. Multi-Level Inheritance: When a child class extends a parent class and further parent class extends another parent class and so on.

Sample Program

package com.sample;

class Shape{
    void draw(){System.out.println("shape draw...");}
}
class Rectangle extends Shape{
    void shapeName(){System.out.println("Rectangle...");}
}

class Square extends Rectangle{
    void sides(){System.out.println("Square sides...");}
}
public class Inheritence{
    public static void main(String args[]){
        Square r=new Square();
        r.shapeName();
        r.draw();
        r.sides();
    }}

3. Hierarchical Inheritance: When a single parent class is extended by multiple child class then it is called Hierarchical Inheritance.

Sample Program

package com.sample;

class Shape{
    void draw(){System.out.println("shape draw...");}
}
class Rectangle extends Shape{
    void shapeName(){System.out.println("Rectangle...");}
}

class Square extends Shape{
    void sides(){System.out.println("Square sides...");}
}
public class Inheritence{
    public static void main(String args[]){
        Square r=new Square();
        r.draw();
        r.sides();
    }}

Multiple Inheritance in Java

We can’t achieve multiple inheritances in Java through the class.  It gives a compile-time error. Let me explain to you why! suppose there is a child class C and it extends class A & class B. In this scenario, suppose A & B classes have the method with the same name and child class C is trying to call that method, but JVM gets confused with two methods having the same name during its calling from the classes A or B; hence, it throws an error.

However, Multiple Inheritance in Java is achieved by Interface. We will discuss it in the further article.

This was all about Inheritance in Java. It is one of the ways to achieve runtime polymorphism in Java. Please share your questions and feedback by using the comment area below. We are going to discuss Aggregation in the next article. Don’t miss to join our Facebook group.

Join Inviul fb group

Leave a Reply