Java program to implement single inheritance

Learn how to implement single inheritance in Java?
Submitted by Nidhi, on March 23, 2022

Problem Solution:

In this program, we will implement single inheritance, here we will create a base class with a method and then inherit the created base class into the derived class using the "extends" keyword. Then we can access the inherited method from the derived class object.

Program/Source Code:

The source code to implement single inheritance is given below. The given program is compiled and executed successfully.

// Java program to implement 
// single inheritance

class Base {
  void BaseMethod() {
    System.out.println("Base method called.");
  }
}

class Derived extends Base {
  void DerivedMethod() {
    System.out.println("Derived method called.");
  }
}

public class Main {
  public static void main(String[] args) {
    Derived obj = new Derived();
    obj.BaseMethod();
    obj.DerivedMethod();
  }
}

Output:

Base method called.
Derived method called.

Explanation:

In the above program, we created 3 classes Base, Derived, and Main. The Base class contains a method BaseMethod(), The Derived class contains a method DerivedMethod(). Here, we inherited the Base class into the Derived class.

The Main class contains a method main(). The main() method is the entry point for the program, here we created the object of Derived class and called the methods of Base and Derived class, and printed the result.

Java Inheritance Programs »





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.