Read this to ace every interview question regarding interfaces.
What are interfaces?
An interface is a reference type similar to a class but different in a sense that it can only contain abstract methods, default methods, static methods, (only default and static methods in interfaces can have a method body) nested types and constants. Interfaces cannot be instantiated; hence they do not have constructors. Instead, interfaces are implemented by classes (or extended by other interfaces).
What is a reference type?
Reference data types in Java are those which contain references to the address of dynamically created objects. class types, arrays types, interface types.
Abstract methods in interfaces
An abstract method in Java is a method that is declared without an implementation (a method with an empty body and no curly braces). Interfaces, in their most basic form are a collection of abstract methods. All methods declared in an interface are always public. Here’s an example from Java Collections Framework:
The comparable interface only has one method which compares an object with another object of the same type.
Let’s implement the Comparable interface. We have created a basic Employee class that has a salary field. We will use the Comparable interface to compare employees’ salary:
Our Employee class can now implement the Comparable interface and will compare employees’ salaries.
Note that when we compare the salary of employees, we are comparing an integer value. Therefore, we can just reliably subtract one from the other and that will indicate whether one employee’s salary is larger than another's.
In order to implement an interface, two things have to be done:
- You have to declare that your class intends to implement an interface via the implements followed by the interface name. Example:
class Employee implements Comparable<Employee>
2. Provide the definition for all the methods in the interface.
Regarding the second point; although in our example, the Comparable interface only has one method, interfaces can have many methods and whenever a class implements an interface, it has to supply the method definitions for all the method that the interface has. Interface are analogous to contracts in real life, because they bind the class to an agreement that it will provide a definition for all the methods in the interface. Therefore, if your class implements an interface you have to provide an implementation for all the abstract methods declared in the interface.
Also, the @Override notation is used to simply indicate to the compiler that the method must override an existing method (either from the superclass/parent class via Inheritance, from an Interface or an abstract base class).
Constants in interfaces
Interfaces can also contain constants and classes that implement an interface containing constants will inherit those constants. Therefore, any class that implements an interface with constants has access to those constants. Constants are always labelled public, static and final.
Using interface as a type
Although you cannot use the new keyword to instantiate an interface (like you do with classes), you can still declare variables with the interface as the data type. An interface variable can only refer to an object of a class that implements that interface. Here is a simple example:
In the example above, we declare variables personInfo and machineInfo with the reference type Info and assign an instance of Person and Machine to them respectively. Doing so, we only have access to the methods declared in the interface but do not have access to methods that are declared in the class. As you can see, personInfo.greet() will result in an error since personInfo is declared with the type Info. Therefore, assigning an object to an interface reference data type limits that object to the methods in the interface.
Defining a new interface, defines a new reference type.
This means that you can use interface names anywhere you would use any other data type name. If you define a reference variable whose type is an interface, any object you assign to it must be an instance of a class that implements that interface. Consider Figure 2.1; the only reason we are allowed to initialize personInfo and machineInfo with an instance of Person and Machine is because both Person and Machine classes implement the interface Info.
Interfaces can be passed as a parameter in a method; any class instances that implement that interface can now be passed into that method as arguments. In the method body itself, you will only have access to class methods that are implemented in the interface (methods defined in the class are not accessible). Interfaces can also be used to specify the return type.
Here’s an example showcasing the usage of an interface to specify the return type and the parameter type:
Why use interfaces?
There are several benefits that developers get when using interfaces.
- Multiple inheritance
Java only permits singular inheritance; a class can only have one parent class. However, a class can implement many interfaces so utilizing interfaces creates a way to bypass singular inheritance in Java.
- Organizational benefits
There are several organization advantages to using interfaces. A class that implements an interface is organized about the behaviour it promises to provide since all the methods listed in an interface must be implemented in the class for the program to successfully compile.
- Loose coupling potential
Using interfaces gives developers the ability to design loosely coupled systems. Coupling refers to the dependency of one object on another. In order to write long-term, scaling software it is important to create systems composed of many independent parts which can be changed without having to change the entire system.
Common interview questions on Interfaces
If you are unsure of the answer, scan through the article for it (or Google it :) )
Q: Can interfaces have constructors?
Q: Can we override an interface method with visibility other than public?
Q: Can we re-assign a value to a field of interfaces?
Hope you learned something! :)