interface temp
{
public int add(int a,int b)
{
return a+b;
}
}
can we implement method like above in interface, or we have to just define methods in interface.
interface temp
{
public int add(int a,int b)
{
return a+b;
}
}
can we implement method like above in interface, or we have to just define methods in interface.
Yes you can in Java 8, using default methods
interface temp
{
default public int add(int a,int b)
{
return a+b;
}
}
As mentioned by Thilo in the comments, Java 8 also added the possibility to have static methods in interfaces:
interface temp
{
public static int add(int a,int b)
{
return a+b;
}
}
If you are not using java 8
then you can only define methods in interface.
public interface temp {
int add(int a,int b);
}
This is a new feature "default method in interface" introduced in java 8
.