Java Interface

Interface is like a container class, Which are contain method and properties. By default all method of interface are abstract method and they are implemented by other class that are use this interface. The main goal of interface is providing a availability to implement multiple inheritance capability. Because java are not support multiple inheritance. They are declare by using following syntax.

interface interfaceName {
    // public,static,and final fields
    // declare abstract methods (by default abstract)
}

interface is an keyword followed by name of interface. The name of interface should be valid identifier in Java language. Within the curly braces define its properties and method. And abstract method are declare by empty body. Interface declaration are implicitly an abstract interface. Whether it's declared abstract or not.

interface Test{
}
//or
abstract interface Test{
}

Declaring Interface Members

There is three type of members are possible in interface. First is constant members (fields) . Second is to define the abstract, static and default method. And third is static member.

interface DemoOne {
   
   float PI = 3.145f;
}

//Or equilevent to 
interface DemoTwo {
   
   public static final float PI = 3.145f;
}

In this example both PI variable of DemoOne and DemoTwo interface are equivalent. Because by default an implicitly variable of interface is constant public static. We can normally use interface variable of using following syntax.

InterfaceName.variableName

let's see an example how to use interface variable

interface Demo{
   //By default that is public static final
   float PI = 3.145f;
}


public class Working
{ 

    public static void main(String[] args){ 
      //Access interface variable
      System.out.println("PI : "+(Demo.PI));

      //Cannot modify because constant
      //Demo.PI =3.14f; 
    } 
}
PI : 3.145

Let's see an example how can be used interface in you'll source code.

//Define interface
interface TestInterface
{
   public int data=10; //interface properties variables
   public void message(String about); //abstract method
}
class DemoOne implements TestInterface{


    //implements interface method
    public void message(String about){
        System.out.println(about);
        System.out.println("data : "+ data); //use interface field value
    }
}

public class Operates{

    public static void main(String[] args){
        DemoOne test = new DemoOne();
        test.message("Test Data");
    }

}

Output

Test Data
data : 10

Interface are very important in Java programming. You'll cannot create an object of interface, But there are possible to create reference variable to interface. This reference variable is capable to hold the instance of class which are implement interface. See this example.

//Make a interface
interface Employee{

  public void about();

}
//Class implements interface
class Salesman implements Employee{

  private String name;
  Salesman(String name){
    this.name=name;
  }
  //implement interface method
  public void about(){
    System.out.println(name + " is an Salesman ");
  }
}
class Driver implements Employee {
  private String name;

    Driver(String name){
    this.name=name;
  }
  //implement interface method
  public void about(){
    System.out.println(name + " is an Driver ");
  }

}

class Working
{ 

  public static void roles(Employee [] list){

    for(Employee employee : list){
      //Execute class methods
      employee.about();
    }
  }
    public static void main(String[] args){ 

      //Make a array of Employee interface
      Employee[] employee = new Employee[4];

      employee[0]= new Driver("Smith");
      employee[1]= new Salesman("Lee");
      employee[2]= new Salesman("Jhon");
      employee[3]= new Driver("peter");

    roles(employee);

    } 
}
Create interface reference variable
Smith is an Driver 
Lee is an Salesman 
Jhon is an Salesman 
peter is an Driver 

JDK 8 are including few new feature to interface. We can implement static and default method inside a interface.

interface info 
{ 
    //Define default method of info interface
    default void about() 
    { 
        System.out.println("default method of interface"); 
    }
    //Define static method
    static void detail() 
    { 
        System.out.println("static method of interface"); 
    }  
} 
//implements interface
class Test implements info 
{ 
   
    public static void main (String[] args) 
    { 
        Test test = new Test();
        
        test.about(); //execute default method of interface 
        info.detail(); //execute static method
    } 
}

Output

default method of interface
static method of interface

Why not allow multiple inheritance in java programming

That is an programming strategy, multiple inheritance is complex structure that are allows to use more than one base class can be inherited by derived class. C++ are providing this feature.

But programmer point of view, multiple inheritance are creates very complex structure. like that Use more than one base class can be inherited by derived class. Then that can create following problems.

1) When Both base class are exist same signature method (name and parameter list are same). Then how to identify that which base class method will execute at run time.

2) We are know about when derived class create constructor then internally are execute base class constructor. In this situation how to compiler are verifying which class constructor would first execute?. That the reason java is avoid the multiple inheritance.



Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved