# Interface

&#x20;Interface is similar to class which is collection of public static final variables (constants) and abstract methods.

&#x20;The interface is a mechanism to achieve fully abstraction in java. There can be only abstract methods in the interface. It is used to achieve fully abstraction and multiple inheritance in Java.

#### &#x20;Why we use Interface?&#x20;

It is used to achieve fully abstraction. By using Interface, you can achieve multiple inheritance in java. It can be used to achieve loose coupling.

#### &#x20;Properties of Interface

* &#x20; An interface is implicitly abstract. So we no need to use the abstract keyword when declaring an interface.&#x20;
* Each method in an interface is also implicitly abstract, so the abstract keyword is not needed.&#x20;
* &#x20;Methods in an interface are implicitly public. All the data members of interface are implicitly public static final.

#### &#x20;How interface is similar to class?

&#x20;Whenever we compile any Interface program it generate .class file. That means the byte code of an interface appears in a .class file.&#x20;

#### How interface is different from class?

&#x20;You cannot instantiate an interface.

&#x20;Interface does not contain any constructors.&#x20;

All methods in an interface are abstract.

&#x20;Interface cannot contain instance fields.&#x20;

Interface only contains public static final variables. Interface is cannot extended by a class; it is implemented by a class.

&#x20;Interface can extend multiple interfaces.

It means interface support multiple inheritance&#x20;

#### Declaring Interfaces:

&#x20;The interface keyword is used to declare an interface.

![](https://106491783-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LTW0eWU2biq082hmKEn%2F-LTffK6QO4zya-16v28m%2F-LTfgYf0zya-6vsDMahI%2Fimage.png?alt=media\&token=91d147c3-2536-4ebe-aac1-41af03dd3670)

#### &#x20;Explanations

&#x20;In the above syntax Interface is a keyword. interface name can be user defined name the default signature of variable is public static final and for method is public abstract. JVM will be added implicitly public static final before data members and public abstract before method.

#### &#x20;Exampl&#x65;**'**

&#x20;public static final datatype variable name=value; ----> for data member public abstract returntype methodname(parameters)---> for method

#### &#x20;Implementing Interfaces

: A class uses the implements keyword to implement an interface. The implements keyword appears in the class declaration following the extends portion of the declaratio&#x6E;**.**

When we use abstract and when Interface

&#x20;If we do not know about any things about implementation just we have requirement specification then we should be going for Interface.\
&#x20;If we are talking about implementation but not completely (partially implemented) then we should be go for abstract.

#### &#x20;Rules for implementation interface

&#x20;A class can implement more than one interface at a time. A class can extend only one class, but implement many interfaces. An interface can extend another interface, similarly to the way that a class can extend another class.&#x20;

#### Relationship between class and Interface

&#x20;Any class can extends another class.&#x20;

Any Interface can extends another Interface.&#x20;

Any class can Implements another Interface.&#x20;

Any Interface cannot extend or Implements any class.

![](https://106491783-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LTW0eWU2biq082hmKEn%2F-LTffK6QO4zya-16v28m%2F-LTfhQQOyghK0hWmczHv%2Fimage.png?alt=media\&token=e9b9b04f-82b0-491b-89d6-843fbd074277)

#### &#x20;Why interface have no constructor ?

&#x20;Because, constructor are used for eliminate the default values by user defined values, but in case of interface all the data members are public static final that means all are constant so no need to eliminate these values.

&#x20;Other reason because constructor is like a method and it is concrete method and interface does not have concrete method it have only abstract methods that's why interface have no constructor.&#x20;

#### JDK 8 Updates:&#x20;

1\. Prior to JDK 8, interface could not define implementation. We can now add default implementation for interface methods. This default implementation has special use and does not affect the intention behind interfaces.&#x20;

Suppose we need to add a new function in an existing interface. Obviously the old code will not work as the classes have not implemented those new functions. So with the help of default implementation, we will give a default body for the newly added functions. Then the old codes will still work.

![](https://106491783-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LTW0eWU2biq082hmKEn%2F-LTffK6QO4zya-16v28m%2F-LTfhh9BD7tFbOu0Topo%2Fimage.png?alt=media\&token=d755cdb2-3877-4617-bb79-11ef12bd3626)

#### &#x20;Output :

#### &#x20;Hello

&#x20;2\. Another feature that was added in JDK 8 is that we can now define static methods in interfaces which can be called independently without an object. Note: these methods are not inherited.

![](https://106491783-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LTW0eWU2biq082hmKEn%2F-LTffK6QO4zya-16v28m%2F-LTfi2hlNRw052g77TLr%2Fimage.png?alt=media\&token=f1c5bbca-c26a-437a-8cd6-973cbbaa6b0a)

