Q:

Java program to implement single inheritance

belongs to collection: Java Inheritance Programs

0

Java program to implement single inheritance

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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 BaseDerived, 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.

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Java program to implement multi-level inheritance... >>