What is INHERITANCE in JAVA – Definition,Syntax,Types and Program

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

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.

1

Reusability

subclass can access the code of superclass without writing it again.

2

Extensibility

you can extend the logic of superclass in subclass.

3

Data Hiding

Superclass can decide to keep some data private from the subclass. so that subclass can not access that data from superclass.

4

Overriding

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.

1

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.

  1. Single Inheritance
  2. Multiple Inheritance
  3. Hierarchical Inheritance
  4. Multilevel Inheritance
  5. Hybrid Inheritance

Single Inheritance

single inheritance in java, different types of inheritance in java, types of inheritance in java
single Inheritance

In single inheritance, there is one superclass per subclass.

Multiple Inheritance

multiple inheritance in java, types of inheritance ,
Multiple Inheritance

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.

Hierarchical Inheritance

Hierarchical Inheritance , types of inheritance,
Hierarchical Inheritance

In Hierarchical Inheritance, multiple subclass have one common superclass.

Multilevel Inheritance

Multilevel Inheritance, types of inheritance
Multilevel Inheritance

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.

Hybrid Inheritance

Hybrid Inheritance, types of inheritance
Hybrid 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 superclass_name

{
//variables
//methods
}

class subclass_name extends superclass_name

{
//some_additional_own_properties
}

Example using Extends keyword in JAVA

class abc 
{
int a=10;
}

class abc2 extends abc
{
int b=20;
}

class inheritdemo
{
public static void main(String[] args)
{
abc2 obj = new abc2();

int c = obj.a + obj.b;

System.out.println("Answer is"+c);
}
}

Output

Answer is30

Leave a Comment