Programming





Inheritance in Java

Inheritance in Java


by Administrator | 04-Apr-2020
Java

Tags:    Inheritance  Multilevel Inheritance  Multiple Inheritance  Hybrid Inheritance  Hierarchical Inheritance  Core Java

Share: 


The process of acquiring properties and behavior of a class by another class is called inheritance. Inheritance is an important feature of Object-Oriented Programming.

Advantages of Inheritance

  1. Code reusability
  2. Prevent code duplication
  3. Maintainability
  4. Code readability
  5. Promotes flexibility

Properties of Inheritance

  1. The class whose properties are being extended is known as base/super class
  2. The class that is extending the super class is known as derived/sub-class.
  3. Sub-class can access the super class member and properties as its own.
  4. Both class and interface help achieve inheritance.
  5. The keyword extends is used for inheriting a class.

Syntax

class SubClass extends SuperClass{
    statements
}
  1. The keyword implements is used for inheriting an interface.

Syntax

class SubClass implements SuperInterface{
    statements
}
  1. A final class cannot be subclassed.
  2. A class can extend only one class but implement any number of interfaces.
  3. In Java, multiple inheritance is not allowed to prevent the problem in Deadly Diamond of Death. It means that if class A is extending two classes B and C. Both the superclasses have one common method. Now, if subclass A wants to extend that method then which version of that method it will inherit. 

Types of Inheritance

1. Simple inheritance

When a class inherits from only one superclass

simple-inheritance

Program

class A {
    void doStuff() {
        System.out.println("Doing Stuff.");
    }
}
class B extends A {
    void sleep() {
        System.out.println("Sleeping.");
    }
}
public class SimpleInheritance {
    public static void main(String args[]) {
        B b = new B();
        b.doStuff();
        b.sleep();
    }
}

2. Multiple inheritance(Through Interfaces)

When a class can inherits properties and behaviors of multiple interfaces. Java does not support multiple inheritance through classes.

multiple-inheritance

Program 

interface A {
    public void run();
}
interface B {
    public void walk();
}
interface C extends A, B {
    public void run();
}
class child implements C {
    @Override
    public void run() {
        System.out.println("Running.");
    }
    public void walk() {
        System.out.println("Walking.");
    }
}
public class MultipleInheritance {
    public static void main(String[] args) {
        child c = new child();
        c.run();
        c.walk();
        c.run();
    }
}

3. Multilevel inheritance

When class A is extending a class B which itself is extending another class C, it is known as multilevel inheritance. In this, class A can extend the properties and behavior of both class B and class C.

multilevel-inheritance

Program

class A {
    void doStuff() {
        System.out.println("Doing Stuff.");
    }
}
class B extends A {
    void sleep() {
        System.out.println("Sleeping.");
    }
}
class C extends B {
    void walk() {
        System.out.println("Walking.");
    }
}
public class MultilevelInheritance {
    public static void main(String args[]) {
        C c = new C();
        c.doStuff();
        c.sleep();
        c.walk();
    }
}

4. Hierarchical inheritance

In this type of inheritance one superclass can have multiple direct sub-classes.

hierarchical-inheritance

Program

class A {
    void doStuff() {
        System.out.println("Doing Stuff.");
    }
}
class B extends A {
    void sleep() {
        System.out.println("Sleeping.");
    }
}
class C extends A {
    void walk() {
        System.out.println("Walking.");
    }
}
public class HierarchicalInheritance {
    public static void main(String args[]) {
        B b = new B();
        b.doStuff();
        b.sleep();

        C c = new C();
        c.doStuff();
        c.walk();
    }
}

5. Hybrid inheritance

Hybrid inheritance is a combination of more than one type of inheritance described above.

hybrid-inheritance

6. Cyclic inheritance

When a class is extending itself then it is known as cyclic inheritance. Cyclic inheritance is not supported in java.

cyclic-inheritance


Comments:


There are no comments.

Enter a new Comment:










Copyright Šprgrmmng. All rights reserved.