Default interface method
A default method lets us define a default implementation for an instance method. In other words, by use of a default method, it is possible for an interface method to provide a body rather than being abstract.A primary motivation for the default method is to provide a mean which interfaces could be expanded without breaking existing code.
Another motivation for the default method was the desire to specify methods in an interface that are essentially optional depending on how the interface is used.
Default method doesn’t change a key aspect of interface that is its inability to maintain state information. An interface still can’t have instance variables. It’s still not possible to create an instance of an interface by itself. It must be implemented by a class. Default method constitute a special purpose feature although it doesn’t change the fundamental idea of interface but it adds some flexibility to it.
Static interfaces method
Java provides the ability to define one or more static method like static method in a class. A static method defined by an interface can be called independently of any object. Thus no implementation of the interface is necessary and no instance of the interface is required in order to call a static method. Instead, the static method can be called by the following statement:
interfaceName.staticMethodName;
public
interface A
{
static
String show()
{
return
“This is a static method.”;
}
}
class B
implements A
{
public
static void main(String []s)
{
System.out.println(A.show());
}
}
No comments:
Post a Comment