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
private
orprotected
. Onlypublic
and default modifiers are allowed. - Every interface in java is by default
abstract
. - Every variable of an interface is by default
public static
andfinal
while every method of an interface is by defaultpublic
andabstract
. - 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
final
by 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
public
access 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 interface
A { }// Compilation error
protected interface
A { }// Compilation error
public interface
A { }// Correct
interface
A { }// Correct
interface
A { }// is same as
abstract interface
A { }
interface
A {int
id = 20;void
print(); }......... IS SAME AS ........
interface
A {public static final int
id = 20;public abstract void
print(); }
interface
A {// int id;
// Compilation error
int
id = 20; }
interface
A {int
id = 20;void
test() }class
Myclassimplements
A {public void
test() { id = 30;// Compilation error
} }
interface
A {int
id = 20;void
test() }abstract class
Myclassimplements
A {// Define the class as abstract if not overriding the interface method;
}
interface
A {void
test(); }class
Myclassimplements
A {void
test() { }// Compilation error
public void
test() { }// Correct
}
interface
A { }interface
Bextends
A {// valid
}interface
Bimplements
A {// Not valid
}
class
Myclassimplements
Interface1, Interface2, Interface3, .... {// Define methods of all implemented interface
}
interface
A {int
calulateArea(); }interface
B {int
calulateArea(); }class
MyClassimplements
A, B {public int
calulateArea() {// Defining once is enough.
} }
interface
A {int
calulateArea(); }interface
B {void
calulateArea(); }class
MyClassimplements
A, B {// Not possible to define calculateArea() for both interfaces
}
interface
A {int
id = 10; }interface
B {int
id = 20; }class
MyClassimplements
A, B {public static
void main(String args []) {// System.out.println(id);
//compilation error if uncommented
System.out.println(A.id);// Access interface A variable, prints 10
System.out.println(B.id);// Access interface B variable, prints 20
} }
interface
MyInterface {void
print(); }class
MyClassimplements
MyInterface {public void
print() { System.out.println("Inside print method"
); }public void
message() { System.out.println("Inside message method"
); }public static
void main(String args []) { MyInterface obj =new
MyClass();// Assigning MyClass object into MyInterface type
// obj.message();
//Error if uncommented, message() is not interface method
obj.print();// prints "Inside print method"
} }
class
MyClassextends
AnotherClassNameimplements
InterfaceName { }