Interface in java 2
Why do we use interface ?
- It is used to achieve total abstraction.
- Since java does not support multiple inheritance in case of class, but by using interface it can achieve multiple inheritance .
- It is also used to achieve loose coupling.
- Interfaces are used to implement abstraction. So the question arises why use interfaces when we have abstract classes?The reason is, abstract classes may contain non-final variables, whereas variables in interface are final, public and static.
interface in1
{
// public, static and final
final int a = 10;
// public and abstract
void display();
}
// A class that implements interface.
class testClass implements in1
{
// Implementing the capabilities of
// interface.
public void display()
{
System.out.println("Java");
}
// Driver Code
public static void main (String[] args)
{
testClass t = new testClass();
t.display();
System.out.println(a);
}
}
Java 10
Comments
Post a Comment