:doodle { @grid: 10 / 100vmax; } background: @pick(#29B6F6, #FDD835, #5E35B1, #FFCCBC, #E8EAF6, #B2DFDB, #1B5E20, #2979FF); opacity: @r(.9); clip-path: polygon( @rand(50%) 0, 50% @rand(70%), 0 @rand(100%) ); animation: test infinite @r(40s, 70s) linear; @keyframes test { 0% { transform: translate3d(0, 0, 0); } 50% { transform: translate3d(@r(-500%, 1000%), @r(-500%, 1000%), 0); } 100% { transform: translate3d(0, 0, 0); } }

Interface

Introduction:

An interface in java is a blueprint of a class. It has 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. It is used to achieve abstraction and multiple inheritance in Java. In other words, you can say that interfaces can have abstract methods and variables. It cannot have a method body.

Why to use Java interface:

  • It is used to achieve abstraction.

  • By interface, we can support the functionality of multiple inheritance.

  • It can be used to achieve loose coupling.

How to declare an interface:

An interface is declared by using the interface keyword. It provides total abstraction; means all the methods in an interface are declared with the empty body, and all the fields are public, static, and final by default. A class that implements an interface must implement all the methods declared in the interface.

Code

Internal addition by the compiler:

The Java compiler adds public and abstract keywords before the interface method. Moreover, it adds public, static, and final keywords before data members.

Code

The relationship between classes and interfaces:

As shown in the figure given below, a class extends another class, an interface extends another interface, but a class implements an interface.

Code

Java Interface Examples:

Code Code

Multiple inheritance in Java by interface:

If a class implements multiple interfaces, or an interface extends multiple interfaces, it is known as multiple inheritance.

Example:

Code

Multiple inheritance is not supported through class in java, but it is possible by an interface:

Multiple inheritance is not supported in the case of class because of ambiguity. However, it is supported in the case of an interface because there is no ambiguity. It is because its implementation is provided by the implementation class.

Interface Inheritance:

Code

Default method in interface:

Code

Static method in interface:

Code

Marker/tagged interface:

An interface that has no member is known as a marker or tagged interface, for example, Serializable, Cloneable, Remote, etc. They are used to provide some essential information to the JVM so that JVM may perform some useful operations.

Nested interface in java:

Code