What is inheritance? | Java Advanced | Part-4

what is inheritance

What is inheritanc in java

Why do I look so similar to my father? My nose is also exactly like him.

That’s because you have inherited his features.

Inheritance is when a class derives some properties from another class.

Just like human beings inherit features from their parents.

The class which takes/inherits the properties is known as child class.

And the class which gives/shares the properties are known as the parent class.

To inherit properties of a class, extends keyword is used.

 class ParentClass{

//body

}

class ChildClass extends ParentClass{

//body

}

This is how we extend a class. Here ChildClass is an inherited class from ParentClass.

child class inherits all the members of the parent class.

An object created of type child class can be used to access all the members of the parent class.

The constructor is never inherited.

Inheritance in action

Let’s learn more about inheritance with the help of the following example.

public class AnotherClass{

public void add(int num1, int num2){

System.out.println(num1+num2);

}

}class Main extends AnotherClass{

public static void main(String[] args) {

Main obj1 = new Main(); //obj1 of type Main

obj1.add(3,5); //Calling add method inherited from AnotherClass

}

}

In the above snippet we have a class Main which inherits the AnotherClass class.

Can you guess the output?

We are calling the add method declared in AnotherClass from the Main class.

And we are passing 3 and 5 as parameters.

So, if your answer was 8 then you are awesome.

Types of inheritance

Classes can inherit from other classes and in different ways.

This brings us to the study of different types of inheritances.

TYPES OF INHERITANCE

There are broadly three types of inheritance in Java – Single inheritance, Multilevel inheritance and Hierarchical inheritance.

Single inheritance

single inheritance

It is the simplest type of inheritance.

In this a child class inherits the properties of a parent class.

class Parent{

//body

}

class child extends parent{

//body

}

Just that.

Can you guess the type of inheritance we used in the add example we saw recently?

If your answer was single inheritance then keep rolling.

In Single inheritance one and only one class can extend another class.

A single parent class and a single child class.

Multilevel Inheritance

multilevel inheritance

In multilevel inheritance there is a chaining of classes.

From above image we can understand that class C is child of class B and class B is a child of class A.

It can be syntactically represented as

class A{

//body

}

class B extends A{

//body

}

class C extends B{

//body

}

Here, class C is child of class B and class B is a child of class A.

There is no limit of this chaining. There can be a class D which extends class C and so on.

Hierarchical Inheritance

hierachical

In hierarchical inheritance more than one classes extend the properties of one class.

That is, a single parent class has multiple child classes.

There can be as many child classes as we want.

But all these classes must have only a single parent class.

class A{

//body

}

class B extends A{

//body

}

class C extends A{

//body

}

Here, A is a parent class of class B and class C.

There can be a class D which also extends class A and so on.

In Java, a single class cannot inherit from more than one class.

Multiple inheritance: In this a child can have more than one parent. and this type of inheritance is not supported in Java.

To summarize

  • Inheritance is when a class derives some properties from another class.
  • The class which takes/inherits the properties is known as child class and the class from which the properties are taken/inherited is known as the parent class.
  • There are mainly three types of inheritance – Single inheritance, Multilevel inheritance and Hierarchical inheritance.
  • Multipleinheritance is not supported by Java.

Recommended books for java

Don't Miss

Leave a Reply

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