Interface tutorial Series:
- What is an Interface in Java
- Default Methods in Java
- Static method in Interface
An interface is like a class[Link] which has fields and methods but with following difference:
1)All fields in an interface are constants i.e. they are static and final and must be initialized at the time of declaration.
2)Methods do no have bodies in an interface. i.e. methods are abstract except the default and static methods.
Interface is declared using the keyword interface before the interface name as:
interface interface_name {
...
}
For example:
interface OurInterface {
public static final int i=10; //a constant
public void method1(); //abstract method
public void method2(); //abstract method
//default method
default void method3() {
System.out.println("This is default method");
}
//static method
static void method4() {
System.out.println("This is static method");
}
}
In above interface there is a constant i and it is initialized at the time of declaration. As said earlier fields in an interface are constants and must be initialized, if you do not initialize it it will give error.
All constants in an interface are implicitly public, static and final. So in above interface constant i is
implicitly public, static and final.
All methods in an interface must be abstract except the default and static methods i.e. they do not have have their bodies defined in interface. In above interface method1() and method2() are abstract methods. We do not need to provide keyword abstract they are implicitly abstract. We just do not provide their bodies.
An interface can have default methods and static methods. Default and static methods can have their bodies defined in an interface. They are not abstract.
All constants and methods in an interface are implicitly public. If you provide any other access modifier to them then it will give error. For example
interface OurInterface {
//this line will give error
protected int i=10;
//this line will give error
protected void method1();
//this line will give error
private void method2();
//this line will give error
private default void method3() {
System.out.println("This is default method");
}
//this line will give error
protected static void method4() {
System.out.println("This is static method");
}
}
An interface cannot be instantiated i.e. we cannot make objects of an interface by using new operator. For example
OurInterface demo=new OurInterface();
Above line will give error:
Cannot instantiate the type OurInterface
Implementing the interface
Now we have defined an interface but how to use it?
For using an interface we use the concept of inheritance i.e. we implement an interface by making a subclass of it.
For Example
interface OurInterface {
int i=10;
void method1();
void method2();
}
public class OurClass implements OurInterface{
//Implementing an abstract method1()
public void method1(){
System.out.println("This is method1()");
}
//Implementing an abstract method2()
public void method2(){
System.out.println("This is method2()");
}
public static void main(String s[]) {
OurClass ourObject=new OurClass();
ourObject.method1();
ourObject.method2();
System.out.println(ourObject.i);
}
}
Output:
This is method1()
This is method2()
10
In above code OurClass inherits OurInterface by implementing it using keyword implements. Since OurClass inherits OurInterface all the fields and methods of OurInterface are available in OurClass.
No comments:
Post a Comment