java

Inheritance in Java programming language

Inheritance in Java programming language

In this tutorial, we will discuss the subject of Inheritance in Java programming language

Inheritance is one of the most effective concepts or feature of Oop paradigm. This helps to establish a link or connectivity between two or more classes. Inheritance helps to access properties(State and behaviours) and methods of one or more classes from another class. In other words, the derived class (subclass) inherit the states and behaviours from the base class(superclass). Java uses ‘extends’ keyword to establish this relationship.

A Base (super) class can have any number of derived(sub) class but a derived class can only have one base class. It’s because Java does not support multiple inheritances.

A superclass (parent class) can have any number of subclasses(child class). However, subclasses can only have one superclass.

“extends” is the keyword used to inherit the properties of a class.

The derived class inherits all the data members and methods that are declared as public or protected. If it’s declared as private, it cannot be inherited by the derived class.
The private members can be accessed only from their own class.

Types of inheritance

  • Single inheritance
  • Multiple inheritance
  • Hierarchical inheritance
  • Multilevel inheritance

Single level inheritance

Single inheritance is easy to understand as a class extends only to one another class. It is known as single inheritance. Single inheritance is having only one parent and child class. Child class has its own properties and those authorised by the parent class.

 

 

Public class A{ //parent class
Block of code…
}public class B extends A{ //child class
Block of code…
}

 Multi-level inheritance

Multilevel inheritance means one base class extends to two or more (more than one) derived classes. A class can be the child(derived) class as well as a parent class to other classes. As seen in the diagram below, Class B inherits the properties of class A and Class C inherits the properties of class B.  Class A is the parent to Class B and Class B is the parent to Class C. Multilevel inheritance in java with Example

 

Public class A{

 

Block of code…
}
public class B extends A{
Block of code…
}
public class C extends B{
Block of code…
}
……may continue…

..

Multiple inheritances

In multiple inheritance, one class extends to more than one base class(one child and two or more parent class). Multiple inheritance is not supported in java. Java supports interface instead of multiple inheritance.

 

Public class A{
Block of code…
}
public class B {
Block of code…
}
public class C extends A,B{
Block of code…
}
Java not support this inheritance
In the hierarchical inheritance, one parent class will be inherited by many children classes. In the example below, class A is inherited by Class B,  Class C  and class D. Class A will be acting as the base class for class B and class C and D. Hierarchical inheritance with example in java
Public class A{

 

Block of code…
}
public class B extends A{
Block of code…
}
public class C extends A{
Block of code…
}
Related Links  
Interface in Java                                Inheritance in C++

 

Karmehavannan

Recent Posts

Using function or method to Write temperature conversion : Fahrenheit into Celsius

Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…

11 months ago

Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user

Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…

11 months ago

Write temperature conversion program: Fahrenheit into Celsius

Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…

11 months ago

How to write a program to convert Fahrenheit into Celsius

How to write a program to convert Fahrenheit into Celsius In this article, we will…

11 months ago

Function/method to convert Celsius into Fahrenheit -Entered by user

Function/method to convert Celsius into Fahrenheit -Entered by user In this article, we will discuss…

11 months ago

Temperature conversion: Celsius into Fahrenheit using function or method

Temperature conversion: Celsius into Fahrenheit using a function or method In this article, we will…

11 months ago

This website uses cookies.