Java Inheritance

What is inheritance in Java?

  • It is a mechanism to acquire the properties of one class to another class.

  • By using the concept of inheritance we can inherit the parent class properties from child class.

  • Inheritance is a concept of object oriented programming which allows reusability of code, we can reuse the fields and methods of an existing class.

Types of inheritance in Java

  • Single inheritance

  • Multilevel inheritance

  • Hierarchical inheritance

What is single inheritance

  • In single inheritance the child class can inherit the properties of parent class.

  • Parent class may also called as superclass whereas child class can be called as derived class.

  • Child class extends the features of of another class which is called as super class or base class.

  • Parent class properties can be inherited by child class.

Multilevel inheritance

  • Multilevel inheritance means one class inherits the properties of another class again, and the properties of this class can be inherited by another class.

  • This process is known as multilevel inheritance, like this we can inherit the properties of of parent class to child class and again parent class to child class.

  • For example grandfather class can be extended by father class again father class can be extended by grandson class.

Hierarchical inheritance

  • The process of inheriting one class to many child classes. For example class d, class e, class f can inherit the same class of a.

Multiple inheritance

  • The process of inheriting one derived class from two parent classes is known as multiple inheritance.

  • But this concept is not allowed in Java, because Java doesn't support multiple inheritance.

  • For achieving multiple inheritance we can use interfaces.

Why Java doesn't support multiple inheritance

Java doesn't support multiple inheritance because of the following scenario.

  • Suppose child class C wants to inherit the same method from the super classes a and b then there will be an ambiguity.

  • Java compiler doesn't know which method is accessing from the child class. So to avoid this conflict Java removed the concept of multiple inheritance.

Hybrid inheritance

  • Java doesn't support the concept of hybrid inheritance.

  • The combination of single and multiple inheritance is known as hybrid inheritance.

  • Java doesn't support multiple inheritance so hybrid inheritance is also not supported by Java.

  • In hybrid inheritance one class tries to inherit the same property from two super classes which doesn't allow in Java.

click here to learn java training in vizag

What is is-a relationship?

The process of inheriting one class to another class is like is a relationship. For example

  • Carrot is a fruit

  • Dog is an animal

  • Bike is a vehicle

Super keyword in Java

  • In order to to access the feels and methods of parent class which was overridden in child class we use super keyword.

  • This super keyword can be used only when the parent class variable or method is overridden in child class.

What are the advantages of using inheritance

  • The main advantage of inheritance is code reusability.

  • The methods and fields which you're already defined can be used many times without defining again and again.

  • Also to achieve runtime polymorphism which is the concept of method overriding. We can override the method of super class from the method of child class this is known as polymorphism.

What is polymorphism in Java

The capability of a method to do many things with single method is known as polymorphism.

Types of polymorphism

  • Method overloading

  • Method overriding

Method overriding can be achieved in Java by using inheritance.

What is Java interface

  • Java doesn't support multiple inheritance, in order to to use multiple inheritance we can use the concept interfaces in Java. By using interfaces we can also achieve abstraction.

  • Abstraction is a process hiding internal details and showing functionality.

  • Java interface consists of abstract methods, which means methods without body are known as abstract methods.

In Java abstraction can be achieved in two ways

  • Abstract class

  • Interface

Abstract class may contain abstract and non abstract methods but interface contains only abstract methods. So by using interface we can achieve complete abstraction.


Best coding classes in Vizag

Java Features

Java Collections

Java Abstraction