Inheritance in Java

Inheritance in Java is a way for one class (called the child class or subclass) to use features from another class (called the parent class or superclass). This allows the child class to use the variables and methods (functions) that already exist in the parent class, making it easier to reuse and organize code.

How Inheritance Works:

  • Code Reuse: The child class can use code from the parent class without having to write it again.
  • Adding New Features: The child class can add its own variables and methods on top of what it inherited from the parent class.
  • Overriding: If needed, the child class can change how some methods work by overriding them with its own version.

Types of Inheritance

Inheritance in Java is a way for one class to acquire the properties and behaviors of another class. Here are the main types of inheritance:

1. Single Inheritance

In single inheritance, a class inherits from only one superclass. This is the most common form of inheritance.

class Parent { ... }
                    class Child extends Parent { ... }

2. Multilevel Inheritance

In multilevel inheritance, a class is derived from another class, which is also derived from another class, forming a chain.

class Grandparent { ... }
                    class Parent extends Grandparent { ... }
                    class Child extends Parent { ... }

3. Hierarchical Inheritance

In hierarchical inheritance, multiple classes inherit from a single superclass.

class Parent { ... }
                    class Child1 extends Parent { ... }
                    class Child2 extends Parent { ... }

4. Multiple Inheritance

Java does not support multiple inheritance with classes (a class cannot inherit from more than one class) to avoid complexity and ambiguity. However, it can be achieved using interfaces.

interface InterfaceA { ... }
                    interface InterfaceB { ... }
                    class Child implements InterfaceA, InterfaceB { ... }

import java.util.Scanner;

class MathOperation 
{                            
    int a, b, c;
    Scanner s = new Scanner(System.in);
    
    public void math1()
    {
        System.out.print("Enter the value of A: ");
        a = s.nextInt();

        System.out.print("Enter the value of B: "); 
        b = s.nextInt();
    }
}                           

class Add extends MathOperation
{                           
    public void add1()
    {     
        c = a + b;
        System.out.println("Addition = " + c);
    }
}                            

class Sub extends Add
{                            
    public void sub1()
    {                        
        c = a - b;
        System.out.println("Subtraction = " + c);
    }
}                            

class Mult extends Sub
{
    public void mult1()
    {
        c = a * b;
        System.out.println("Multiplication = " + c);
    }
}                            

class Div extends Mult
{                            
    public void div1()
    {                        	    
        c = a / b;
        System.out.println("Division = " + c);   	  
    }
}

public class InheritanceExample
{                            
    public static void main(String[] args) 
    {                        
        Div d = new Div();  
        d.math1();           
        d.add1();            // Perform addition
        d.sub1();            // Perform subtraction
        d.mult1();           // Perform multiplication
        d.div1();            // Perform division
    }
}
                        

Output: