Java Interface Tutorial
An interface is like a blue print of Java Class, Interface has static constants and abstract method. Interface is implemented in multiple inheritance.
Interface is similar to Class.It dose not contain construction.Implement keyword is used to implement interface in class and extend keyword is used to extend an interface.
Java Interfaces Declaration
import java.lang.*; public interface Interface_Name { //Abstract method declarations\ }
Java Interface Example
interface Pet{ public void test(); } class Dog implements Pet{ public void test(){ System.out.println("Interface Method Implemented"); } public static void main(String args[]){ Pet p = new Dog(); p.test(); } }
Class vs Interface
Class | Interface |
---|---|
In class, you can instantiate variable and create an object. | In an interface, you can't instantiate variable and create an object. |
Class can contain concrete(with implementation) methods | The interface cannot contain concrete(with implementation) methods |
The access specifiers used with classes are private, protected and public. | In Interface only one specifier is used- Public. |