How to create Interface in java Program

interface Info {
  static final String language = "Java";
  public void display();
}
class Simple implements Info {
  public static void main(String[] args) {
    Simple obj = new Simple();
    obj.display();
  }
  // Defining method declared in interface
  public void display() {
    System.out.println(language + " is awesome");
  }
}

Program Explanation in Steps:

This is a Java program that demonstrates how interfaces work. It includes the declaration of an interface called Info, the declaration of a class Simple that implements this interface, and it displays how methods of an interface are overridden in a class.

1. The Info Interface

interface Info {

   static final String language = “Java”;  // A constant field in the interface}

public void display();  // An abstract method that needs to be implemented

}

  • Constant Field: In the interface Info, the variable language is declared as static final.It has a constant value that cannot be changed once declared. It’s also static-this means it refers to the interface itself, and not to an instance of some class that may implement it. The value of language is “Java”.
  • Abstract Method: In this interface, the method is declared as display(). This makes the implementing class provide an implementation for this method.

2. The Simple Class

public class Simple implements Info {

   public static void main(String []args) {

       Simple obj = new Simple();  // Declaring and instantiating the object of Simple class

obj.display();  // Calling the display() method on the newly declared object

   }

   // Provide an implementation for the display() method declared in Info interface

public void display() {

       System.out.println(language + ” is awesome”);  // Print out the value of language

   }

  • Class Implementation: The Simple class implements the Info interface. That implies Simple has to implement all the abstract methods declared in Info. In this case, the display() method.
  • Main Method: Within the main() method, an object of the Simple class is declared, named obj. Then the obj.display() method is invoked. This will invoke the implementation of the display() method inside the Simple class.
  • display() Method : The method display() displays a message. It concatenates the value of the language constant, which is “Java”, with the string ” is awesome”. Hence, the message “Java is awesome” gets printed to the console.

Key Points:

1.  Interface Declaration : Info declares a constant language and an abstract method display().

2. Class Implementation : Simple implements the Info interface and provides the implementation of the display() method.

3. Direct Access: The language constant from the interface Info is accessed directly within the display() method.

4. Method Invocation: The display() method is invoked within the main() method of the class Simple.

Output

The program will print

Java is awesome

Output Explanation

Summary

The program demonstrates how to declare an interface that has constant fields and abstract methods, how it can be implemented by a class, and finally how to use these in that class. Also, the code uses the interface to access a constant and implements the demanded method to give output.

Leave a Comment

Your email address will not be published. Required fields are marked *