Inheritance In Java…
Java is an approximately 100% object-oriented programming language. Everything which we deal with in java is an object or part of an object(except primitive data types).Java OOPs concept provides features like Inheritance, Encapsulation, Abstraction, Polymorphism. Here I will discuss Inheritance In Java which is one of the most important features of the Java OPPs concept.
Below are the topics, I will be discussing in this article:
- What is Inheritance?
- Why do we need Inheritance?
- Types of Inheritance in Java.
What is Inheritance?
Inheritance is a mechanism in which one class acquires all the properties and behaviors of a parent class i.e in inheritance we use the variables and methods of the previous class to the new class by adding some more variables and functions.
Syntax:
extends is a keyword that is used to inherit the features of the Superclass.
Subclass:- Subclass is the class that inherits the other class, also known as child class or derived class.
Superclass:-It is the class whose features are inherited by the subclass, also known as parent class or base class.
Why do we need Inheritance?
It is often when we write our code there are more than one classes which are having the same variables and methods. To write the same code in every class is time-consuming. Hence we need to find a way so that we do not need to write the same code again and again in each class and the solution is Inheritance.
Inheritance allows us to write reusable codes and save our time to write the same code again and again. The advantage of inheritance is that the code which is already written in the parent class need not be written in the child class.
Types of Inheritance in Java.
Java supports three types of Inheritance.
a.Single Inheritance
b.Multilevel Inheritance.
c.Hierarchical Inheritance.
a.Single Inheritance
When a subclass inherits the features of one superclass, it is known as single inheritance.
In the above block diagram, Class A is the base class, and Class B is the derived class which is inherited from Class A. For more clarity let’s see an example through code.
In the above example, we can see that the SuperCar class is inheriting the features of the Car class so SuperCar is the derived class and Car is the base class here.
b.Multilevel Inheritance.
When a derived class inherits a base class and that base class also inherits another class, it is known as multilevel inheritance.
In the above block diagram, Class C is the subclass of class B and Class B is the subclass of class A. Let’s see an example through code.
In the given example SuperCar class is inheriting the Car class and the Car class is inheriting the Vehicle class. So we can access the features of the Car class and Vehicle class through the object of the SuperCar class. Here Car class in the base class for SuperCar class and derived class for the Vehicle class.
c.Hierarchical Inheritance.
When two or more classes(derived classes) inherit a single base class, it is known as hierarchical inheritance.
In the given example the SuperCar and MiniCar Classes are inheriting the features of the same base class(Car class).
Points to remember:-
Whenever we use inheritance in our program, there should be always a proper and well-defined relationship between the child and parent classes. Suppose we create a class of Vehicle which inherits the features of an Animal class, when we run our program it will work fine but if we observe carefully, there is no any common features between the Animals and Vehicles in real-world so it’s doesn’t make any sense to use the features of Animal class in Vehicle class.