what are interfaces in java

what are interfaces in java

1 year ago 40
Nature

In Java, an interface is an abstract type that is used to declare a behavior that classes must implement). It is similar to a protocol in other programming languages. Interfaces are declared using the interface keyword and may only contain method signature and constant declarations). All methods of an interface do not contain implementation (method bodies) as of all versions below Java 8. Starting with Java 8, default and static methods may have implementation in the interface definition. Then, in Java 9, private and private static methods were added).

A class that implements an interface must implement all of the non-default methods described in the interface, or be an abstract class). Object references in Java may be specified to be of an interface type; in each case, they must either be null or be bound to an object that implements the interface).

One benefit of using interfaces is that they simulate multiple inheritance. All classes in Java must have exactly one base class, the only exception being java.lang.Object (the root class of the Java type system); multiple inheritance of classes is not allowed. However, an interface may inherit multiple interfaces, and a class may implement multiple interfaces).

Some reasons to use interfaces in Java include achieving abstraction and multiple inheritances, creating multiple is-a relationships between classes, and establishing a contract between the class and the outside world that is enforced at build time by the compiler).

Read Entire Article