In this article, you will get to know about ‘Inheritance’ in Java Programming Language. Inheritance is an object-oriented feature that is supported by Java.
In simple words, Inheritance can be defined as a feature that enables a subclass to inherit the properties(including fields and methods) of its superclass.
The subclass is also known as child class or derived class. The class from which a subclass is derived or inherits the properties is called the superclass. It is also known as parent class or base class.
In order to apply this feature to a java program, first you will have to create a class, and then use the keyword ‘extends’ to create another class(subclass/child class) having properties same as the superclass/parent class.
The keyword ‘extends’ cannot be used with a class to inherit properties of an Interface. However, it is applicable while inheriting properties only methods of a parent interface to a child interface.
Also Read how to Modifiers Used In Java Programming Language
TYPES OF INHERITANCE IN JAVA PROGRAMMING LANGUAGE
- Single Inheritance
- Multi-level Inheritance
- Hierarchical Inheritance
Java doesn’t support the multiple inheritance feature of object oriented programming. But the inclusion of the keyword implements’ which is used to inherit properties of one or more interfaces by a class helped as an alternate to multiple inheritance.
[1] SINGLE INHERITANCE
When a single child class inherits the properties of a parent class, it is known as single inheritance. There are no other child classes present.
Syntax:
public class Parentclass {
…………….
}
public class Childclass extends Parentclass {
…………….
}
Example:
public class W3TrainingSchool {
float c ;
public void division ( float a, float b ) {
c = a / b ;
System.out.println(“Division =” + c) ;
}
public void modulus ( float a, float b ) {
c = a % b ;
System.out.println(“Modulus =” + c) ;
}
}
public class W3TS extends W3TrainingSchool {
public static void main (String args[]) {
float a = 50, b = 20 ;
W3TrainingSchool obj = new W3TrainingSchool ( ) ;
obj.division( a, b ) ;
obj.modulus( a, b ) ;
}
}
Output:
Division = 2.5
Modulus = 10.0
[2] MULTI-LEVEL INHERITANCE
When a child class inherits the properties of a parent class and a second child class inherits the properties of the first child class and so on, it is known as multi-level inheritance.
Syntax:
public class Parentclass {
…………….
}
public class Childclass1 extends Parentclass {
…………….
}
public class Childclass2 extends Childclass1 {
…………….
}
Example:
class ParentClass {
public void parentClass ( ) {
System.out.println(“ParentClass Method”) ;
}
}
class ChildClass1 extends ParentClass {
public void childClass1 ( ) {
System.out.println(“ChildClass1 Method”) ;
}
}
class ChildClass2 extends ChildClass1 {
public void childClass2 ( ) {
System.out.println(“ChildClass2 Method”) ;
}
public static void main(String args[]) {
ChildClass2 obj = new ChildClass2 ( ) ;
obj.parentClass ( ) ;
obj.childClass1 ( ) ;
obj.childClass2 ( ) ;
}
}
Output:
ParentClass Method
ChildClass1 Method
ChildClass2 Method
[3] HIERARCHICAL INHERITANCE
When one or more child classes correspond to the same parent class, it is known as hierarchical inheritance. It means all the subclasses will inherit the properties of the same parent class.
Syntax:
public class Parentclass {
…………….
}
public class Childclass1 extends Parentclass {
…………….
}
public class Childclass2 extends Parentclass {
…………….
}
Example:
class ParentClass {
public void parentClass ( ) {
System.out.println(“ParentClass Method”) ;
}
}
class ChildClass1 extends ParentClass {
public void childClass1 ( ) {
System.out.println(“ChildClass1 Method”) ;
}
}
class ChildClass2 extends ParentClass {
public void childClass2 ( ) {
System.out.println(“ChildClass2 Method”) ;
}
public static void main(String args[]) {
ChildClass1 obj1 = new ChildClass1 ( ) ;
ChildClass2 obj2 = new ChildClass2 ( ) ;
obj1.parentClass ( ) ;
obj1.childClass1 ( ) ;
obj2.childClass2 ( ) ;
}
}
Output:
ParentClass Method
ChildClass1 Method
ChildClass2 Method
Also Check: Operators Used In Java Programming Language
ADVANTAGES OF USING INHERITANCE IN JAVA
⏩ Makes the code more flexible.
⏩ It enable the programmers to override methods of a parent class to a child class.
⏩ Improves code reusability.
⏩ Codes are better organized using inheritance.
DISADVANTAGES OF USING INHERITANCE IN JAVA
⏩ The parent class is tightly coupled with the child class. It means, whenever the codes inside the parent class are manipulated, the corresponding derived classes also get affected.
⏩ Using inheritance, the execution time of a java program increases.
We have provided you the best possible description on using the inheritance feature in java programming language. Hope you like this post. For more updates and related information, stay connected to our blogs on java programming language.