ADVERTISEMENT
ADVERTISEMENT

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 »


ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.