Interface in java is declared using keyword interface and it represent a Type like any Class in Java.
Interface just like a class but it is not a class. An interface can have methods and variables just like the class but the methods declared in interface are by default abstract.
java programming language does not support multiple inheritance, using interfaces we can achieve this as a class can implement more than one interfaces, however it cannot extend more than one classes.
Note: Class implements interface but an interface extends another interface.
Interface variables must be initialized at the time of declaration otherwise compiler will through an error.
There are some key features of Interface
Example :-
Another use of interface
Interfaces are not there to achieve multiple inheritance in Java rather they are made to make any class behave in some specific manner. It is somewhat different concept than inheritance. For example,
we have one interface Animal. So any class that will implement this interface will become an Animal and also can have its own code of methods
Interface just like a class but it is not a class. An interface can have methods and variables just like the class but the methods declared in interface are by default abstract.
java programming language does not support multiple inheritance, using interfaces we can achieve this as a class can implement more than one interfaces, however it cannot extend more than one classes.
Note: Class implements interface but an interface extends another interface.
Interface variables must be initialized at the time of declaration otherwise compiler will through an error.
There are some key features of Interface
- A class can implement any number of interfaces.
- Any interface can extend any interface but cannot implement it. Class implements interface and interface extends interface.
- Class implementing any interface must implement all the methods, otherwise the class should be declared as “abstract”.
- A interface can not be declare private and protected.
- All method of interface by default abstract and public.
- all variable of interface by default public static final.
Example :-
interface MyInterface
{
public void method1();
public void method2();
}
class Test implements MyInterface
{
public void method1()
{
System.out.println("implementation of method1");
}
public void method2()
{
System.out.println("implementation of method2");
}
public static void main(String arg[])
{
MyInterface obj = new Test();
obj. method1();
}
}
{
public void method1();
public void method2();
}
class Test implements MyInterface
{
public void method1()
{
System.out.println("implementation of method1");
}
public void method2()
{
System.out.println("implementation of method2");
}
public static void main(String arg[])
{
MyInterface obj = new Test();
obj. method1();
}
}
Another use of interface
Interfaces are not there to achieve multiple inheritance in Java rather they are made to make any class behave in some specific manner. It is somewhat different concept than inheritance. For example,
interface Animal {
public void eat();
public void sleep();
}
class Lion implements Animal {
public void eat() {
}
public void sleep(){
}
}
class Monkey implements Animal {
public void eat() {
. }
public void sleep() {
}
}
public void eat();
public void sleep();
}
class Lion implements Animal {
public void eat() {
}
public void sleep(){
}
}
class Monkey implements Animal {
public void eat() {
. }
public void sleep() {
}
}
we have one interface Animal. So any class that will implement this interface will become an Animal and also can have its own code of methods