Interface

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

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.

Why we use Interface?

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.

Properties of Interface

  • An interface is implicitly abstract. So we no need to use the abstract keyword when declaring an interface.

  • Each method in an interface is also implicitly abstract, so the abstract keyword is not needed.

  • Methods in an interface are implicitly public. All the data members of interface are implicitly public static final.

How interface is similar to class?

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

How interface is different from class?

You cannot instantiate an interface.

Interface does not contain any constructors.

All methods in an interface are abstract.

Interface cannot contain instance fields.

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

Interface can extend multiple interfaces.

It means interface support multiple inheritance

Declaring Interfaces:

The interface keyword is used to declare an interface.

Explanations

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.

Example'

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

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 declaration.

When we use abstract and when Interface

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

Rules for implementation interface

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.

Relationship between class and Interface

Any class can extends another class.

Any Interface can extends another Interface.

Any class can Implements another Interface.

Any Interface cannot extend or Implements any class.

Why interface have no constructor ?

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.

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.

JDK 8 Updates:

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.

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.

Output :

Hello

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.

Last updated