Programs on Inheritance

1. Single Inheritance

This program demonstrates single inheritance where one class inherits the properties of another class. The child class accesses the methods of the parent class using inheritance.

class Employee {

    void employeeInfo() {
        System.out.println("Employee ID: 101");
        System.out.println("Employee Name: Rahul");
    }
}

class Developer extends Employee {

    void department() {
        System.out.println("Department: Software Development");
    }
}

public class SingleInheritance {

    public static void main(String[] args) {

        Developer obj = new Developer();

        obj.employeeInfo();
        obj.department();
    }
}
Output:
Employee ID: 101
Employee Name: Rahul
Department: Software Development

2. Multilevel Inheritance

This program demonstrates multilevel inheritance where a class inherits another derived class. The child class can access properties from both parent and grandparent classes.

class Person {

    void personInfo() {
        System.out.println("Name: Priya");
    }
}

class Student extends Person {

    void studentInfo() {
        System.out.println("Roll No: 105");
    }
}

class Result extends Student {

    void marks() {
        System.out.println("Marks: 92");
    }
}

public class MultilevelInheritance {

    public static void main(String[] args) {

        Result obj = new Result();

        obj.personInfo();
        obj.studentInfo();
        obj.marks();
    }
}
Output:
Name: Priya
Roll No: 105
Marks: 92

3. Hierarchical Inheritance

This program demonstrates hierarchical inheritance where multiple child classes inherit the same parent class. Each child class has its own specialized method.

class Vehicle {

    void fuel() {
        System.out.println("Uses Fuel");
    }
}

class Car extends Vehicle {

    void wheels() {
        System.out.println("Car has 4 Wheels");
    }
}

class Bike extends Vehicle {

    void helmet() {
        System.out.println("Helmet is Mandatory");
    }
}

public class HierarchicalInheritance {

    public static void main(String[] args) {

        Car c = new Car();
        Bike b = new Bike();

        c.fuel();
        c.wheels();

        b.fuel();
        b.helmet();
    }
}
Output:
Uses Fuel
Car has 4 Wheels
Uses Fuel
Helmet is Mandatory

4. Multiple Inheritance using Interfaces

This program demonstrates multiple inheritance using interfaces. A class implements multiple interfaces and provides implementations for their methods.

interface Father {

    void property1();
}

interface Mother {

    void property2();
}

class Child implements Father, Mother {

    public void property1() {
        System.out.println("Father's Property");
    }

    public void property2() {
        System.out.println("Mother's Property");
    }
}

public class MultipleInheritance {

    public static void main(String[] args) {

        Child obj = new Child();

        obj.property1();
        obj.property2();
    }
}
Output:
Father's Property
Mother's PropertyCode language: PHP (php)

5. Hybrid Inheritance using Interfaces

This program demonstrates hybrid inheritance using interfaces. Java supports hybrid inheritance through interfaces instead of classes.

interface A {
    void displayA();
}

interface B extends A {
    void displayB();
}

class C implements B {

    public void displayA() {
        System.out.println("Method A");
    }

    public void displayB() {
        System.out.println("Method B");
    }
}

public class HybridInheritance {

    public static void main(String[] args) {

        C obj = new C();

        obj.displayA();
        obj.displayB();
    }
}
Output:
Method A
Method B

6. Super Keyword practice program-1

This program demonstrates the use of the super keyword to invoke the parent class constructor. The child constructor calls the parent constructor before executing its own statements.

class Animal {

    Animal() {
        System.out.println("Animal Constructor");
    }
}

class Dog extends Animal {

    Dog() {
        super();
        System.out.println("Dog Constructor");
    }
}

public class SuperConstructor {

    public static void main(String[] args) {

        Dog obj = new Dog();
    }
}
Output:
Animal Constructor
Dog Constructor

7. Super Keyword practice program-2

This program uses the super keyword to access the parent class variable. It differentiates between parent and child variables having the same name.

class Parent {

    int number = 100;
}

class Child extends Parent {

    int number = 200;

    void display() {

        System.out.println("Child Variable = " + number);
        System.out.println("Parent Variable = " + super.number);
    }
}

public class SuperVariable {

    public static void main(String[] args) {

        Child obj = new Child();

        obj.display();
    }
}
Output:
Child Variable = 200
Parent Variable = 100Code language: PHP (php)

8. Method Overriding

This program demonstrates method overriding. The child class provides its own implementation of the parent class method.

class Bank {

    void interest() {
        System.out.println("Interest Rate: 5%");
    }
}

class SBI extends Bank {

    @Override
    void interest() {
        System.out.println("Interest Rate: 7%");
    }
}

public class MethodOverriding {

    public static void main(String[] args) {

        SBI obj = new SBI();

        obj.interest();
    }
}
Output:
Interest Rate: 7%

9. Dynamic Method Dispatch

This program demonstrates dynamic method dispatch. The parent class reference calls the overridden method of the child class at runtime.

class Animal {

    void sound() {
        System.out.println("Animal makes sound");
    }
}

class Cow extends Animal {

    @Override
    void sound() {
        System.out.println("Cow says moo");
    }
}

public class DynamicDispatch {

    public static void main(String[] args) {

        Animal obj = new Cow();

        obj.sound();
    }
}
Output:
Cow says moo

10. Abstract Class

This program demonstrates the use of an abstract class. The abstract method is implemented by the child class.

abstract class Employee {

    abstract void salary();
}

class Manager extends Employee {

    void salary() {
        System.out.println("Manager Salary = 60000");
    }
}

public class AbstractClassExample {

    public static void main(String[] args) {

        Manager obj = new Manager();

        obj.salary();
    }
}
Output:
Manager Salary = 60000

11. Abstract Method

This program demonstrates an abstract method. Different child classes provide their own implementation of the abstract method.

abstract class Shape {

    abstract void area();
}

class Square extends Shape {

    void area() {

        int side = 5;

        System.out.println("Area = " + (side * side));
    }
}

public class AbstractMethodExample {

    public static void main(String[] args) {

        Square obj = new Square();

        obj.area();
    }
}
Output:
Area = 25

12. Interface

This program demonstrates the use of an interface. The implementing class provides the definition for the interface method.

interface Calculator {

    void add(int a, int b);
}

class Demo implements Calculator {

    public void add(int a, int b) {

        System.out.println("Sum = " + (a + b));
    }
}

public class InterfaceExample {

    public static void main(String[] args) {

        Demo obj = new Demo();

        obj.add(15, 25);
    }
}
Output:
Sum = 40
Scroll to Top