If you have any doubts about inheritance in java or any question related to inheritance then read this full article carefully. you will get the definition, syntax, examples, different types, advantages and disadvantages of inheritance in java. here i uploaded the ppt and pdf of inheritance…hope you like it.
- INHERITANCE in java Definition
- Advantages (use) of Inheritance in Java
- Disadvantages of Inheritance in Java
- Different types of inheritance in Java
- Syntax of Inheritance in Java
- Example using Extends keyword in JAVA
INHERITANCE in java Definition
Basically the concept of inheritance is used in object oriented languages like c++, java etc..
By using concept of inheritance, you can access all the properties(data members, methods) of superclass from the subclass.
Inheritance is a mechanism in java by which subclass can borrow the properties of superclass and at the same time the subclass may have some additional properties.Inheritance in java definition
What is Superclass in Java ?
The class whose properties (data members, methods) are used by another class is called as Superclass in java.
superclass is also known as parent class , base class.
What is Subclass in Java ?
The class which uses the properties of other class is called as Subclass in java.
subclass is also known as child class , derived class.
Advantages (use) of Inheritance in Java
The most important feature of inheritance is that you don’t need to write the duplicate code in a program. you can use that code again without writing it again.
subclass can access the code of superclass without writing it again.
you can extend the logic of superclass in subclass.
Superclass can decide to keep some data private from the subclass. so that subclass can not access that data from superclass.
If we are unsatisfied with some methods of superclass then we can design that method in subclass. this means we can override the method of superclass in subclass.
Disadvantages of Inheritance in Java
main disadvantage of inheritance is code complexity.
Overuse of inheritance in the program can make it very complex and because of that, it makes debugging very difficult.
Different types of inheritance in Java
There are five types of inheritance.
- Single Inheritance
- Multiple Inheritance
- Hierarchical Inheritance
- Multilevel Inheritance
- Hybrid Inheritance
In single inheritance, there is one superclass per subclass.
In multiple Inheritance, subclass have more than one superclass. Java does not support multiple inheritance by using class directly. Java uses concept of Interface to implement this type of inheritance.
In Hierarchical Inheritance, multiple subclass have one common superclass.
When subclass is derived from superclass and that superclass is also a subclass, which is derived from one another superclass then it is known as multilevel inheritance.
When two or more types of inheritance are combined together then it forms the Hybrid inheritance.
Syntax of Inheritance in Java
To define the relationship between subclass and superclass we are using the keyword EXTENDS. The keyword extends represents that the properties of superclass are extended to the subclass.
class subclass_name extends superclass_name
Example using Extends keyword in JAVA
class abc2 extends abc
public static void main(String args)
abc2 obj = new abc2();
int c = obj.a + obj.b;