Lesson

Inheritance

Opps/Java

Inheritance

Inheritance is a mechanism in Java that allows one class to acquire the properties and behavior of another class. It is mainly used for:

  • Code reusability

  • Establishing relationships between classes

  • Reducing code duplication

A class can inherit another class using the extends keyword.

Key Terminology

  • Superclass / Parent Class / Base Class
    The class whose properties are inherited.

  • Subclass / Child Class / Derived Class
    The class that inherits from another class.

  • A subclass can access all members of the superclass except private members.

java
class A {
    int a = 10;
}

class B extends A {
    void display() {
        System.out.println(a);
    }
}

In this example, class B inherits the property a from class A.

Types of Inheritance

1. Single Inheritance

In single inheritance, one subclass inherits from one superclass.

java
class A {
    int a = 10;
}

class B extends A {
    void show() {
        System.out.println(a);
    }
}

2. Multiple Inheritance (Not Supported Java)

In multiple inheritance, a class tries to inherit from more than one class.

java
class A{
	int a = 10;
}
class B{
	int a = 20;
}
/*class C extends A,B{   // Not allowed
	void m1 (){
		System.out.println("a :- "+a);	
}
}*/

Reason:

Java does not support multiple inheritance using classes because of the ambiguity problem.

If two parent classes contain the same variable or method, the JVM cannot determine which one to use.

Note: Multiple inheritance can be achieved using interfaces.

3. Multilevel Inheritance

In this type, a class inherits from another class, which is already inheriting from another class.

java
class A {
    int a = 10;
}

class B extends A {
    int b = 20;
}

class C extends B {
    int c = 30;
}

Here, class C can access properties of both A and B.

4. Hierarchical Inheritance

In hierarchical inheritance, multiple classes inherit from the same superclass.

javascript
class A {
    int a = 10;
}

class B extends A {
    int b = 20;
}

class C extends A {
    int c = 30;
}

Both B and C can access properties of class A.

5. Hybrid Inheritance

Hybrid inheritance is a combination of multiple types of inheritance.

Java does not support hybrid inheritance using classes because it involves multiple inheritance.

6. Cyclic Inheritance (Not Allowed)

Cyclic inheritance occurs when a class tries to inherit from itself or forms a loop.

plaintext
class A extends A // Not allowed

class B extends C
class C extends B

This results in a compile-time error.

super Keyword and super()

super()

  • Used to call the constructor of the parent class

  • It must be the first statement inside the constructor

  • If not written, the compiler adds it automatically

java
class A {
    A() {
        System.out.println("A Constructor");
    }
}

class B extends A {
    B() {
        super();
        System.out.println("B Constructor");
    }
}

super Keyword

  • Used to access parent class variables and methods

  • Cannot be used in a static context

javascript
class A {
    int a = 10;
}

class B extends A {
    int a = 20;

    void show() {
        System.out.println(a);        // 20
        System.out.println(super.a);  // 10
    }
}

Important Points

  • Java supports:

    • Single inheritance

    • Multilevel inheritance

    • Hierarchical inheritance

  • Java does not support:

    • Multiple inheritance (using classes)

    • Hybrid inheritance

    • Cyclic inheritance

  • Interfaces are used to achieve multiple inheritance in Java

Inheritance | Java | Softcrayons Tech Solutions