Why Interface:
The general form of an interface is
interface InterfaceName { fields declarations; methods declarations; }
The java compiler adds public and abstract keywords before the interface method and public, static and final keywords before data members.
Interface Demo { int x=10; void show(); }
Here the keyword interface tells that Demo is an interface containing one final field x and one abstract method show().
NOTE:
Compiler automatically converts methods of Interface as public and abstract, and the data members as public, static and final by default.
What we declare
interface Demo { int x=100; void print(); }
What the Compiler Understand
interface Demo { public static final int x=100; public abstract void print(); }
Let's see a demo program for better understanding:
InterfaceExample1.java
package java8s;
interface Demo { void print(); }
class Example2 implements Demo { public void print() { System.out.println("JAVA means Silan Software"); } } public class InterfaceExample1 { public static void main(String[] args) { Example2 obj=new Example2(); obj.print(); } }
Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.
We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc