Interface in Java
Interface is a similar kind of java class. It can be called as collection of abstract methods or another way of achieving abstract method as it contains empty bodies. It can declare as: interface <any-name>{}.
Example
/* interface */
interface Box
{
/*interface method does not have a body */
public void boxHieght();
/*interface method does not have a body */
public void run();
}
Interface Feature:
- Class can contains variable, method etc. Similarly for interface.
- Like class interface have method, variable but these method does not contain any body part. This is main difference between interface and class.
- When we use any class, this can extends only one class. But a class can use multiple interface and all the interface method will be used as override. These override method can’t be contained any ambiguous problem.
Class which are used interface need to use implements such as
Class Example implements Shadow {} package interfaccepackage;
public interface Shadow{
/*implicitly public abstract method*/
void manshadow();
/*implicitly public static final*/
int x = 100;
}
1. Here we show that the method is contain void but implicitly here the method actually used as public abstract if we do not write down in manshadow() method.
2. Variable x is not only int but also public static final either we write it or not.
When we declare any interface and any class implements this interface then compiler show that the interface has implementations and method has override. Below we use a class named as Example, where we implements Shadow interface.
When any class implements any interface must be override all the interface methods in the class otherwise will compilation error.
package interfaccepackage;
public Class Example implements Shadow {
int y;
public int noOflegs();
return 10;
}
public void printmn(){
System.out.println(“Class method println statements : “);
}
public static void main(String args[]){
Example ex = new Example ();
System.out.println(ex.y);
System.out.println(x);
System.out.println(ex.noOflegs());
ex.manshadow();
ex.printmn();
}
@Override
public void manshadow(){
System.out.println(“Interface method manshadow statements prints: “);
}
}
noOflegs(), printmn() are Example class method. Manshadow () method is override and this is a public method due to interface this method is default access method. When we use method of interface we need to override the method less restricted in the class. In Example class public is less restricted than default so we override manshadow () method as public.
Like Example class more class can be implements Shadow() interface and can override all the methods as user requirements
Output of the program
run:
0
100
10
Interface method manshadow statements prints:
Class method println statements :
0 is for the glabal variable y’s default initialization value.
100 interface variable x’s value
10 is noOflegs() method of Example class value.
First println is Example class
Second println is Override method
Interface is mainly used for polymorphism. Multiple classes can implements multiple interface. Besides multiple interface can extends multiple interfaces. But a java class cannot extends multiple interface because java support single inheritance.
These interface feature are explain with example below:
Interface TextA:
public interface TextA{
public abstract void print();
public static final int var=0;
}
Interface TextB:
public interface TextB{
void print();
}
There is two interface TextA and TextB both contains same method but there will be no ambiguity problem. But if we use it in C++ then there will show ambiguity problem. Now we check ambiguity when we implements these interfaces in a class named Neon {}
public Class Noon implements TextA, TextB{
void method(){
System.out.println(“Outside method in java : “);
}
public static void main(String args[]){
Neon n = new Neon();
n.print();
n.method();
}
@Override
public void print(){
System.out.println(“Outside method in java: “);
}
}
Here is the override method print(). If we click print() method ‘I’ key this will show implements from both interface TextA and TextB and work properly what we use in this method. That means interface has no interface ambiguity problem. Another uses of interface is class Neon can be abstract so print() method need not mandatory override. But if we wish we can use override. There is a problem of using abstract class because In abstract class no object can be created. Similarly
- Interface no object can be created.
- An interface can extends another interface
public interface TextA extends TextB
{
public abstract void bprint();
public static final int var=0;
}
That is an interface can used another interface using extends.
Another feature of interface is marker interface. In this interface no method or variable included for example Cloneable(). This interface send special information to JVM. Again @Override in one kind of interface.
Cloneable() interface
package java.lang;
public interface Cloneable{
}
Cloneable() is default interface. We can implement user defined empty interface too.
package java.lang;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(value = (ElementType.METHOD))
@Retention(value =RetentionPolicy.SOURCE )
public @interface Override{
}
More other tutorials on JAVA in Draftsbook Article series on :
- JAVA : Dart is similar feature with JAVA language. Java Classes and objects are the two main aspects of object-oriented programming. The Principles of OOP are: