Pages

Interface

Super And This
Interface Definition

An Interface is a collection of methods definitions without implementations and can also include constant declarations. An interface in Java is a blueprint of a class. A Java interface contains static constants and abstract methods. The interface in Java is a mechanism to achieve abstraction. There can be only abstract methods in the Java interface, not the method body. Interface is used to achieve abstraction and multiple inheritance in Java. We can say that interfaces can have abstract methods and variables. It cannot have a method body. Java Interface also represents the IS-A relationship.

Why do we use an Interface ?
  1. Interfaces are used to implement abstraction.
  2. Interface is used to achieve total abstraction.
  3. Interface is also used to achieve loose coupling.
  4. abstract classes may contain non-final variables, whereas variables in the interface are final, public and static.
  5. Java does not support multiple inheritances in the case of class, by using an interface it can achieve multiple inheritances.
Difference Between Class and Interface
Class
  1. We can instantiate variables and create an object.
  2. Class can contain concrete(with implementation) methods.
  3. The access specifiers used with classes are private, protected, and public.
Interface
  1. We can not instantiate variables and create an object.
  2. The interface can not contain concrete(with implementation) methods
  3. Interface only one specifier is used Public.

Important Points About Interface
  1. Interface is used to achieve loose coupling.
  2. A class can implement more than one interface.
  3. Interface is used to achieve multiple inheritances.
  4. An interface can extend to another interface or interface.
  5. A class that implements the interface must implement all the methods in the interface.
  6. All the methods are public and abstract and all the fields are public, static, and final.
What is Marker or Tagged Interface ?

An Interface that have no data member and method is known as a Marker or Tagged Interface. They are used to provide some essential information to the JVM so that JVM may perform some useful operation.

Tell me Marker Interface in Java ?
  1. java.rmi.Remote.
  2. java.lang.Cloneable.
  3. java.io.Serialization.
  4. java.util.RandomAccess.
  5. java.util.EventListener.
  6. javax.ejb.EnterpriseBean.
  7. javax.servlet.SingleThreadModel.
Default Method in Interface ?

Java 8 introduced Default Method in Interface. Methods which are defined inside the interface and tagged with default are known as default methods. These methods are non-abstract methods.