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 »






Comments and Discussions!

Load comments ↻






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