In this program, we will create an interface with an abstract method and a data member. Then we will implement created interface into a class using the "implements" keyword.
Program/Source Code:
The source code to create an interface with a data member is given below. The given program is compiled and executed successfully.
// Java program to create an interface
// with a data member
interface Inf {
int num = 10;
//public and abstract
void printNum();
}
public class Main implements Inf {
public void printNum() {
System.out.println("Num: " + num);
}
public static void main(String[] args) {
Main mObj = new Main();
mObj.printNum();
}
}
Output:
Num: 10
Explanation:
In the above program, we created an interface Inf with an abstract method printNum(). Then we defined the printNum() method in the Main class by implementing the Inf interface using the "implements" keyword.
The Main class contains a method main() and defined printNum() method. The main() method is the entry point for the program, here we created the object of the Main class and called the printNum() method, and printed the result.
In this program, we will create an interface with an abstract method and a data member. Then we will implement created interface into a class using the "implements" keyword.
Program/Source Code:
The source code to create an interface with a data member is given below. The given program is compiled and executed successfully.
Output:
Explanation:
In the above program, we created an interface Inf with an abstract method printNum(). Then we defined the printNum() method in the Main class by implementing the Inf interface using the "implements" keyword.
The Main class contains a method main() and defined printNum() method. The main() method is the entry point for the program, here we created the object of the Main class and called the printNum() method, and printed the result.