This tutorial explains some of the key points about interfaces which you should be aware about. It will help you to use interfaces correctly. To understand this tutorial easily, you must have knowledge about interfaces in java, if not, refer Interface tutorial first. Following are some key points about interfaces in java :
- Interfaces cannot be declared as
privateorprotected. Onlypublicand default modifiers are allowed. - Every interface in java is by default
abstract. - Every variable of an interface is by default
public staticandfinalwhile every method of an interface is by defaultpublicandabstract. - Interface variables must be initialized with some value at the time of declaration.
- Class implementing an interface cannot change the value of variable declared in the interface since they are declared as
finalby default. - Class implementing an interface must implement all the methods of that interface, otherwise the class must be declared as
abstract. - While implementing the method of an interface in a class, it's visibility(access modifier) can not be reduced. Reducing the visibility
will result in compilation error, so it must have
publicaccess modifier in class as well. - An interface can extend another interface but it cannot implement it. Only classes can implements interfaces.
- A class can implement any number of interfaces.
- If a class implements two interfaces which have same method name, then the implementation of same name method once is enough.
- A class cannot implement two interfaces that have methods with same name but different return type.
- Variable name conflicts can be resolved by interface name. Conflict is a situation in which a class implements two interfaces which have same variable name. Conflicted variable must be accessed by interface name otherwise compiler will throw error.
- The object of implementing class can also be assigned into interface type. Using such object you can call only the methods declared inside the interface. If you call a method defined inside class using such object, it will result in compilation error.
- A class can extend another class and can implement an interface as well.
- From java 8 onward an interface can have default and static methods with body. Refer New features in Interfaces tutorial for the same.
- An interface can have another interface inside it. Refer Nested Interface tutorial for the same.
private interfaceA { }// Compilation errorprotected interfaceA { }// Compilation errorpublic interfaceA { }// CorrectinterfaceA { }// Correct
interfaceA { }// is same asabstract interfaceA { }
interfaceA {intid = 20;voidprint(); }......... IS SAME AS ........interfaceA {public static final intid = 20;public abstract voidprint(); }
interfaceA {// int id;// Compilation errorintid = 20; }
interfaceA {intid = 20;voidtest() }classMyclassimplementsA {public voidtest() { id = 30;// Compilation error} }
interfaceA {intid = 20;voidtest() }abstract classMyclassimplementsA {// Define the class as abstract if not overriding the interface method;}
interfaceA {voidtest(); }classMyclassimplementsA {voidtest() { }// Compilation errorpublic voidtest() { }// Correct}
interfaceA { }interfaceBextendsA {// valid}interfaceBimplementsA {// Not valid}
classMyclassimplementsInterface1, Interface2, Interface3, .... {// Define methods of all implemented interface}
interfaceA {intcalulateArea(); }interfaceB {intcalulateArea(); }classMyClassimplementsA, B {public intcalulateArea() {// Defining once is enough.} }
interfaceA {intcalulateArea(); }interfaceB {voidcalulateArea(); }classMyClassimplementsA, B {// Not possible to define calculateArea() for both interfaces}
interfaceA {intid = 10; }interfaceB {intid = 20; }classMyClassimplementsA, B {public staticvoid main(String args []) {// System.out.println(id);//compilation error if uncommentedSystem.out.println(A.id);// Access interface A variable, prints 10System.out.println(B.id);// Access interface B variable, prints 20} }
interfaceMyInterface {voidprint(); }classMyClassimplementsMyInterface {public voidprint() { System.out.println("Inside print method"); }public voidmessage() { System.out.println("Inside message method"); }public staticvoid main(String args []) { MyInterface obj =newMyClass();// Assigning MyClass object into MyInterface type// obj.message();//Error if uncommented, message() is not interface methodobj.print();// prints "Inside print method"} }
classMyClassextendsAnotherClassNameimplementsInterfaceName { }


