Inheritance in Java is a fundamental principal in OOP ( Object-Oriented Programming ) by which one class is allowed to inherit the properties (fields) and behaviors (methods ) of another class .
A class that inherits the another class can reuse the properties of that class .
Example :
//Parent class
class Gift {
void gifName() {
System.out.println(“Gift is awesome”);
}
}
//Child class
class Studentgift extends Gift {
void giftName () {
System.out.println(“Gift is incredible book “);
}
}
//Child class
class Teachergift extends Gift {
void giftName() {
System.out.println(“Gift is $11000”);
}
}
//Main class
public class InheritanceDemo {
public static void main (String args[] ) {
Gift gif ;
gif = new Studentgift();
gif.giftName();
gif = new Teachergift();
gif.giftName();
}
}
OUTPUT:
Gift is incredible book
Gift is $11000
Advantages of Inheritance in Java :
- Reusability of Code : The code written in parent class(super class ) is common to all subclasses . Child classes ( Sub classes ) can directly use the parent class code .
- Method Overriding : Method overriding is achieved by only inheritance . Method overriding is one of the ways by which Java achieves Run Time Polymorphism.
- Abstraction : Abstraction is a process where implementation details are hidden and only shows the functionality to the user . This concept of abstraction is achieved through inheritance .
HOW INHERITANCE WORKS IN JAVA ?
For inheritance concept, extends keyword is used in Java . This extends keyword enables the subclass to inherit the properties of superclass . When a class extends another class , it inherits the properties of the superclass . When a class extends another class , it means it inherits all the non-primitive members of the superclass . The subclass can also override the methods of superclass or can add new functionality to them .
TYPES OF INHERITANCE IN JAVA
Different types of java inheritance are discussed below .
- Single Inheritance :
In Java single inheritance , a sub-class is derived from only one super class . In single inheritance , the sub class inherits the properties & behavior of the parent class .
Example :
//Super class
class Gift {
Gift() {
System.out.println(“Gift is awesome “);
}
}
//Sub class
class StudentGift extends Gift {
StudentGift () {
System.out.println(” Student Gift is an incredible book”);
}
}
public class DemosingleInheritance {
public static void main (String args[] ) {
//Creating object of subclass invokes the superclass constructor
StudentGift sgif= new StudentGift() ;
}
}
OUTPUT:
Gift is awesome
Student Gift is an incredible book
Multilevel Inheritance:
Multilevel inheritance is an java object-oriented programming concept where a class is derived from another class, creating a hierarchial chain of inheritance.
class Gift {
Gift( ) {
System.out.println(“Gift is awesome”);
}
}
class BestgiftWinner extends Gift {
Gift () {
System.out.println(“Best gift winners are incredible”);
}
}
class Studentgift extends BestgiftWinner {
Gift () {
System.out.println(“Students with more than score 90 are winning best gifts”);
}
}
public class DemoMultilevelInheritance {
public static void main (String args[] ) {
Studentgift sgif = new Studentgift ()
}
}
OUTPUT :
Gift is awesome
Best gift winners are incredible
Students with more than score 90 are winning best gifts.
3. Hierarchial Inheritance :
In Java , Hierarchial Inheritance is an object-oriented programming concept where multiple subclasses inherit from a single superclass (parent class ) .
For example : StudentGift and TeacherGift , both are Gift , where Gift is a superclass and StudentGift , TeacherGift are subclass extending the superclass Gift .
Example :
class Gift {
Gift ( ) {
System.out.println(“Gift is awesome”);
}
}
class StudentGift extends Gift {
Gift () {
System.out.println(“Student’s gift is an incredible book”);
}
}
class TeacherGift extends Gift {
Gift ( ) {
System.out.println(“Teacher’s gift is $11000”) ;
}
}
class DemoHierarchialInheritance {
public static void main (String args [ ] ) {
StudentGift sgif = new StudentGift () ;
TeacherGift tgif = new TeacherGift () ;
}
}
OUTPUT :
Gift is awesome
Student’s gift is an incredible book
Gift is awesome
Teacher’s gift is $11000
4. Multiple Inheritance :
Java multiple inheritance is an object-oriented programming concept where a class can inherit from more than one super class (parent class ) . But with java classes multiple inheritance is not possible due to ambiguity problem.
Java class doesn’t support Multiple Inheritance
Java class doesn’t support multiple inheritance due to ambiguity problem.
Example :
import java.io.*;
//First super class
class Gift1 {
void gift () {
System.out.println(“Gift1”);
}
}
//Second Super Class
class Gift2 {
void gift () {
System.out.println(“Gift2”);
}
}
//Inheriting properties of super class1 and super class2
class Demo extends Gift1 , Gift2 {
//main method
public static void main (String args [] ) {
// Creating instance of Demo
Demo d = new Demo () ;
d.gift() ;
}
}
OUTPUT :
Compile time error is thrown
Java Multiple Inheritance can be achieved with Interfaces
Java allows a class to implement multiple interfaces , thus it supports multiple inheritance safely .
interface Gift1 {
default void gift ( ) {
System.out.println ( “Interface Gift1 display”);
}
}
Interface Gift2 {
default void gift ( ) {
System.out.println(“Interface Gift2 display “);
}
}
class Winner implements Gift1 , Gift2 {
@Override
public void gift () {
//Resolving conflict
Gift1.super.gift ( ) ;
Gift2.super.gift () ;
System.out.println (” Class Winner display “) ;
}
}
public class Demo {
public static void main (String args[] ) {
Winner obj = new Winner () ;
obj . gift () ;
}
}
OUTPUT :
Interface Gift1 display
Interface Gift2 display
class Winner display
5. Hybrid Inheritance :
Java hybrid inheritance is a combination of two or more types of inheritance in a single class hierarchy . If this hybrid inheritance is achieved by combining multiple inheritance , then you have to use interface instead of class as multiple inheritance can be achieved using interface only but not with class .
Example :
In the above example, Gift is a class. Two classes Teacher Gift & Student Gift are extending Gift class which is hierarchial inheritance as in hierarchial inheritance multiple subclasses are inherited from single super class . Then Best Winner class is extending Teacher Gift class and Teacher Gift class is already extending Gift class which is an example of multilevel inheritance. As the above example is combination of two or more inheritance, so it is a hybrid inheritance.
