ADVERTISEMENT
ADVERTISEMENT

Java program to implement the interface with multi-level inheritance

Learn how to implement the interface with multi-level inheritance in Java?
Submitted by Nidhi, on March 28, 2022

Problem Solution:

In this program, we will implement the interface with multi-level inheritance. Then we will create the object of the low-level label class and call all methods.

Program/Source Code:

The source code to implement the interface with multi-level inheritance is given below. The given program is compiled and executed successfully.

// Java program to implement the interface 
// with multi-level inheritance

interface Inf {
  void sayHello();
}

class Sample1 implements Inf {
  public void sayHello() {
    System.out.println("Hello World");
  }
}

class Sample2 extends Sample1 {
  public void sayHello1() {
    System.out.println("Hello World1");
  }
}

class Sample3 extends Sample2 {
  public void sayHello2() {
    System.out.println("Hello World2");
  }
}

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

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

Output:

Hello World
Hello World1
Hello World2

Explanation:

In the above program, we created an interface Inf. Then we implemented the Inf interface in the Sample1 class. Then we implemented Sample1 into Sample2 and Sample2 into Sample3.

The Main class contains a method main(). The main() method is the entry point for the program, here we created the object of the Sample3 class. After that, we called all abstracted 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.