ADVERTISEMENT
ADVERTISEMENT

Java program to create a simple interface

Learn how to create a simple interface in Java?
Submitted by Nidhi, on March 28, 2022

Problem Solution:

In this program, we will create an interface with an abstract method. Then we will implement created interface into a class using the "implements" keyword.

Program/Source Code:

The source code to create a simple interface is given below. The given program is compiled and executed successfully.

// Java program to create a 
// simple interface

interface Inf {
  // public and abstract
  void sayHello();
}

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

  public static void main(String[] args) {
    Main mObj = new Main();

    mObj.sayHello();
  }
}

Output:

Hello World

Explanation:

In the above program, we created an interface Inf with an abstract method sayHello(). Then we defined the sayHello() method in the Main class by implementing the Inf interface using the "implements" keyword.

The Main class contains a method main() and defined sayHello() method. The main() method is the entry point for the program, here we created the object of the Main class and called sayHello() method, 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.