Interface in Java

This post explains Interface in java.

What is Interface?
Interface is a blue print of class. Unlike class it contains only public & abstract methods and public, static & final fields.

Interface Syntax:


Difference between Interface and Class
1. Interface: Have only abstract methods
Class: Cannot have abstract method

2. Interface: Multiple Inheritance possible
Class: Multiple Inheritance not possible

3. Interface: Can not be instantiated
Class: Can be instantiated

4. Interface: Do not have constructors
Class: Have constructor

Some facts about Interface
1. Interface is use to achieve loose coupling
See Example

2. Multiple inheritance can be achieve by using interface
See Example

3. Interface provides full abstraction

4. Interface can extends multiple Interfaces

5. An interface does not contain any constructors, that is why Interface can not be instantiated

—InterfaceClass —

—MainClass —

6. JVM itself writes the public static final before the data members and public abstract before methods in Interface if programmer do not write it

For Example:

After Compilation

  • Very interesting details you have observed, thank you for putting up.

%d bloggers like this: