Saturday, 13 January 2018

4.14 Interface

Interface - To implement multiple inheritance in java we use interface

interface came to solve the diamond problem





  • only definition of methods no implementations
  • by default method will be public and abstract.
  • Implement keyword will be use to inherit an interface
  • it is must that the class which is implementing the interface have implementation of interface methods.

Interface types
1. Marker Interface - interface without any method
2. SAM - Single abstract method - in java 8 (Functional interface)
3. Normal interface

In java 8 
you can define and declare methods by using default or static keywords.
diamond problem again came when it is possible to implements methods in interface



interface I
{
default void show()
{
  System.out.println("Hello");
}
static void display()
 {
    System.out.println("Hi");
 }
 void work();
}

class interfaceTest implements I
{
public void work()
{
System.out.println("Hello world");
}
  public static void main(String args[])
 {
  interfaceTest obj=new interfaceTest();
  obj.work();
  I.display();
}
}






No comments:

Post a Comment