Default Interfaces

  • JDK8 allows us to use Default interfaces which provides a default implementation for an Interface methods means we can define a body for a method rather than abstract methods. So these are called as Default methods or Extension methods.
  • The prior motivation of these methods to avoid breakage of existing code where if the implementation of the methods are not exist then it will breaks the execution.
  • Thus this Default methods will solve this problem by supplying an implementation when there is no implementation for methods.
  • Another motivation is to specify methods in an interface that are, essentially, optional, depending on how the interface is used.
  • Example remove() method where we have to remove the data when it is modified only so if not modified then there is no need of this method so we can use this as default method which tells the implemented class as remove() is optional.
  • This will not change the property of Interface as its inability to maintain state information means it can not have instance variables.

Fundamentals of Default Method

The default method is defined by using default keyword where the defining the method is same as methods in class but declared as default.

//example for default method in interfaces
interface MyIF 
{
 // This is a "normal" interface method declaration.It does NOT define a default implementation.
 int getNumber();

  // This is a default method.it provides a default implementation.
 default String getString() 
 {
 return "Default String";
 }
}

// Implement MyIF.
class MyIFImp implements MyIF 
{
 // Only getNumber() defined by MyIF needs to be implemented.
 // getString() can be allowed to default.

  public int getNumber() 
 {
 return 100;
 }
}

// Use the default method.
public class DefaultMethodDemo 
{
  public static void main(String args[]) 
  {
 MyIFImp obj = new MyIFImp();
 // Can call getNumber(), because it is explicitly
 // implemented by MyIFImp:
 System.out.println(obj.getNumber());
 // Can also call getString(), because of default
 // implementation:
 System.out.println(obj.getString());
 }
}

Output:

100
Default String

Practical Example

interface IntStack 
{
 void push(int item); // store an item
 int pop(); // retrieve an item
 // Because clear( ) has a default, it need not be
 // implemented by a preexisting class that uses IntStack.
 default void clear() 
 {
 System.out.println("clear() not implemented.");
 }
}

As it is not mandatory to implement clear() method so we can declare it as default method.

Multiple Inheritance Issues

  • In Java we It is not possible to multiple inheritance of classes but Interfaces essentially can not provide the way for this restriction as classes can maintain the states information but interfaces can not.

  • As a class can implements two or more interfaces by implementing the methods which if there is a default methods in both Interfaces with same definition thus default methods limit the extent of multiple inheritance.

  • This process can be categorized in to a set of cases as follows.

Consider A and B are interfaces and C is the class implementing A and B and A and B have a default method as display() with same definition.

  1. If C implements the default method display() means override the method then it will execute.
  2. If C does not override the display() method then it shows an error.
  3. If A and B are inherited interfaces means B extends A then C will execute B's default method.
  4. If B want's to refer to the A's default method then we can use InterfaceName.super.defaultMethod();

A.super.display();

//example for case 1
interface A
{
  default void display()
  {
    System.out.println("This is A's default method");
  }
}

interface B
{
  default void display()
  {
    System.out.println("This is B's default method");
  }
}

class C implements A,B
{
  public void display()
  {
    System.out.println("This is c's default method");
  }
}

public class Demo
{
  public static void main(String args[])
  {
    C c=new C();
    c.display();
  }

}

Output:

This is c's default method

//example for case 2
interface A
{
  default void display()
  {
    System.out.println("This is A's default method");
  }
}

interface B
{
  default void display()
  {
    System.out.println("This is B's default method");
  }
}

class C implements A,B
{
  //public void display()
  //{
    System.out.println("This is c's default method");
  //}
}

public class Demo
{
  public static void main(String args[])
  {
    C c=new C();
    c.display();
  }

}

Output:

Unable to execute program; could not compile!

Using Static methods in an Interface

  • JDK8 provides to use static methods in interfaces, So there is no need of Object to call that method. Thus no implementation of the interface is necessary, and no instance of the interface is required, in order to call a static method.
  • General form to call static method InterfaceName.staticMethodName;
//example for static methods in an interface
interface MyIF 
{
 // This is a "normal" interface method declaration.
 // It does NOT define a default implementation.
 int getNumber();

 // This is a default method. Notice that it provides
 // a default implementation.
 default String getString() 
 {
 return "Default String";
 }

  // This is a static interface method.
 static int getDefaultNumber() 
 {
 return 11;
 }
}

public class Demo 
{
  public static void main(String args[])
  {
   System.out.println("default number from static method is::"+MyIF.getDefaultNumber()); 
  }
}

Output::

default number from static method is::11

results matching ""

    No results matching ""