Home > DeveloperSection > Blogs > Interfaces in Java

Interfaces in Java

Java Java  Interface 
1 Comment(s)
 309  View(s)
Rate this:

Interfaces in Java 

When we are learning Inheritance, we go through a point that Java does not support multiple inheritance—in other words, a class cannot have two or more super classes. Multiple inheritance has its own advantages and disadvantages. Java achieves some of the benefits of multiple inheritance through interfaces. So what is an interface? An interface is a device or system that unrelated entities use to interact with each other.

When we drive a car, we interact with a machine—two totally unrelated entities. These two entities interact through a well-defined interface for steering, throttling, and braking. The English and French languages may be considered an interface for communication between two people— not totally unrelated entities in this case. A remote control is an interface between a viewer and a television set. In the military, the interface between officers of different rank is the enforced protocol of behaviour. Java interfaces are analogous to such protocols; they provide an agreed upon behaviour between objects of different types or of the same type.

Interfaces may be considered a standard framework for accessing classes. They provide a separation between behaviour and implementation.

How Interfaces look like?                   

So what does an interface look like? An interface has a structure similar to a class. It contains methods and fields. However,

·         none of the methods of an interface can contain implementation code,

·         And all fields must be declared final.

 Therefore, the methods defined in an interface provide only the signatures, and all the fields are program constants. We need to define a class in our program that provides the implementations of the methods declared in an interface. A class can implement multiple interfaces, and an interface can have multiple super-interfaces

All methods in an interface are public and abstract by default. A method is said to be “abstract” when no implementation is provided for it.

Difference between Interface and Multiple Inheritance:

Although there seems to be a lot of similarity between interfaces and multiple inheritance, there are some subtle important differences:

·     A class can inherit the fields of a superclass, but it inherits only the constants from an interface (note that an interface does not allow field declarations).

·   A class can inherit method implementations from a superclass, but it cannot inherit method implementations from an interface because there are none in an interface.

·      In the case of multiple inheritance, all involved classes in the hierarchy are somehow related to each other; in the case of interfaces, the classes that implement them may or may not be related through the class hierarchy.

Where to use it?

Now that you know what an interface is, the next question is where to use it. Therefore, let’s discuss the various uses of an interface:

Interfaces are useful in capturing similarities between unrelated classes without the need to force a relationship between them in the class hierarchy. Think of an Employee class and a Stock class—both require a print functionality whereby the user can print its description. Thus, we could create an interface called Printable that has a Print method (besides other methods for the printer, page settings, and so on). Both the Employee and Stock classes will implement this Printable interface to provide a common functionality, but otherwise the classes are unrelated.

·     Interfaces allow us to define behaviour that one or more classes are expected to implement. In the Printable interface example, Print, PageSetting, PrinterSetting, and so on, would be the methods of the Printable interface that define the behaviour for various classes.

·    Interfaces allow us to hide the implementation details of an object. For example, as discussed earlier, to drive a car, we need not be concerned with how the fuel is ignited in the engine’s cylinders. Many times, we can provide anonymous objects to the user by revealing only the object’s programming interface.

Wonderful blog

By Simond Gear on   one month ago
This is a wonderful blog. It gives all the details in short and simple. Thank you very much. Well done guys.

Don't want to miss updates? Please click the below button!

Follow MindStick