Skip to content

Latest commit

 

History

History
136 lines (101 loc) · 4.69 KB

14 Interface.md

File metadata and controls

136 lines (101 loc) · 4.69 KB

Interface

  • An interface in java is like an abstract class with the following:
    1. no instance variables ( other than variables declared final ).
    2. declared with interface keyword.
    3. interface can extend/inherit multiple interfaces.
    4. class implementing interface should either implement the methods or declare it as abstract.
    5. declared either public or default package level access.

interface vs concrete class vs abstract class

identity concrete class abstract class interface
constant ( public, final, static ) yes yes yes
constructor yes yes no
instatiation yes no no
instance members yes yes (Pre / Post ) java 8 depends

interface Comparison pre and post java 8.

Pre Java 8:
• no private or protected members.
• no instance variables, all variables are public static final implicitly (essentially constants).
• no instance methods.
• all methods are public abstract implicitly.
• no static methods.

Java 8 & 8+:
• no private or protected members.
• no instance variables, all variables are public static final implicitly (essentially constants).
• can have default methods ( instance methods or extension method ), inherited by implementing class.
• all unimplemented methods are public abstract implicitly.
• can have implemented static methods.
• unlike static methods in classes, static methods are not inherited by the extending interface or the implementing class

Beginning with JDK 9 we can have private implemented methods in the interface, which is accessible within the interface only. private methods can be called only by the default method or another private method of the same interface.

So, when referring to methods within an interface:
Abstract methods are methods without a body that must be implemented by classes.
Default methods are methods with a body that can be optionally overridden.
Static methods are methods that belong to the interface itself and cannot be overridden or accessed via class instances.
Private methods are methods that belong to the interface itself, and only accessible via the default interface methods.

variables defined inside the interface are automatically imported as constants in the implementing class.

interface Test1{
    public static final int hello = 0;
    public abstract void implementMe();
    
    private void say(String msg){
      System.out.println(msg);
    }
  
    default public void sayHello(){
      this.say("Hello");
    }
    
    public static void  hello(){
        System.out.println("hello");
    }

}

// OR 

interface Test1{
  int hello = 0;
  void implementMe();

  private void say(String msg){
    System.out.println(msg);
  }

  default void sayHello(){
    this.say("Hello");
  }
  
  static void  hello(){
    System.out.println("hello");
  }
}

Handling Method Clash

  1. Interface vs Interface: when two interfaces have a method with same signature and one of them is a default method,
    • any implementer of both interfaces must override the method or declare the method abstract.
    • any sub interface of both interfaces must provide a default method or declare the method abstract.
  2. SuperClass vs Interface: when a class extends a superclass and implements an interface, and both the super class and the interface have a method with same signature, the superclass implementation wins! this is the version inherited in the subclass.
  3. Same Static methods: No clash

Type of interfaces

  • functional Interface : interface with just one abstract methods, methods matching the signature from Object class does not count.
@FunctionalInterface
public interface Test1{
    void implementMe();
    boolean equals(Object object);
}

@FunctionalInterface
public interface Test2{
    void implementMe();
}
  • Member or Nested interface: An interface can be declared a member of a class or another interface. A nested interface can be declared as public, protected or private. this differs from the top level interface, which must be declared either as public or default package level access.
class A {
    public interface INested {
        boolean isEven(int x);
    }
}

class B implements A.INested{
    public boolean isEven(int x){
        return x % 2 == 0;
    }
}
  • Marker interface
  • Tag Interface ( cloneable, serializable, RandomAccess )