ADVERTISEMENT
ADVERTISEMENT

Java program to implement multiple-inheritance using the interface

Learn how to implement multiple-inheritance using the interface in Java?
Submitted by Nidhi, on March 29, 2022

Problem Solution:

In this program, we will create 2 Interfaces. Each interface contains an abstract method. Then we will implement created interface in a class.

Program/Source Code:

The source code to implement multiple-inheritance using the interface is given below. The given program is compiled and executed successfully.

// Java program to implement multiple-inheritance 
// using the interface

interface Inf1 {
  void sayHello1();
}

interface Inf2 {
  void sayHello2();
}

class Sample implements Inf1, Inf2 {
  public void sayHello1() {
    System.out.println("Hello World1");
  }

  public void sayHello2() {
    System.out.println("Hello World2");
  }
}

public class Main {
  public static void main(String[] args) {
    Sample S = new Sample();

    S.sayHello1();
    S.sayHello2();
  }
}

Output:

Hello World1
Hello World2

Explanation:

In the above program, we created 2 interfaces Inf1, Inf2. Then we implemented the created interfaces in the Sample class to implement multiple-inheritance.

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

Java Interface 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.