Java Interface | How to Use Interface in Java
We are gonna learn what Java Interface means and how to use interface in Java in this chapter. I dedicate this whole leaflet to learning everything about interfaces.
Interfaces are more or less based on the concept of Abstract Classes and are introduced to get rid of the multiple-inheritance deficiency. Meaning that multiple inheritances even though not possible in Java are taken care of by the use of Interfaces.
I know the obvious question running in your head is:
What is an Interface?
An interface is nothing but a reference type in Java. It is abounding with static constants and abstract methods. However, if you find a method body in an interface, it must be a static method or a default method.
NOTE: Default method is nothing but a method whose implementation is provided in an interface itself to avoid rework. It is preceded with a keyword default. The method is public implicitly so you don’t have to mention public on it.
There are a lot of similarities it shares with a class. Like a Java interface is written in a separate .java file too with the name of the file resembling that of the interface name. There can be any number of methods you can put inside an interface. Also, it cannot be instantiated (just like abstract class).
However, there are tons of dissimilarities too.
- There are no constructors here.
- All the methods inside a Java interface are abstract.
- Fields in an interface are static and final.
- A Java interface is implemented and not extended.
- One interface can extend multiple interfaces.
An Example for Interface in Java
If I try to explain interfaces with the aid of an example that would help you to understand what you are dealing with here.
I have created an interface called Alien:
You don’t need to put an access modifier public or a keyword abstract here because, like I said before, they are public and abstract by default.
Now time to create a class that implements this Java interface. Remember this class should define and declare all the methods from the interface. In case it doesn’t it has to be declared as abstract and let another class extend this class for declaration.
NOTE: If we don’t make methods here public, it would be counted as default which would be a step down from public. Stepping down in access modifiers is unacceptable to Java when you are extending or implementing. The superclass or interface has to be greater in terms of access.
So naturally if you run the above program, you will get the following result:
Growl! Chomp! Chomp! Zzzzzzz!
which basically sounds like my life right now.
I could also create a reference variable of Alien Interface directly like:
Alien a
But remember I cannot instantiate it, so to instantiate I have to once again make use of Prometheus.
Alien a = new Prometheus();
Evolving Interfaces
It is hard to be a pro when you are learning new things as they come. Chances of missing out on methods from interfaces are high. You could miss the inclusion of a method, and then at a later stage come to realize without adding that method continuing is futile.
For instance, let’s take our above interface example into account. Let’s say we need another method called moaning() that we realized at a later stage.
Now if you have already set up everything, it would be a programming nightmare to include it in the interface Java and then make changes in the implementing classes.
Why, you ask? Remember the rule that every method inside the interface needs to be declared first? If you don’t declare this method in your class, you get a compile time error.
On keeping your mouse on the error it says:
The type must implement the inherited abstract method moaning()
To tackle such a scenario, you have two options.
- Use Interface Inheritance
- Use Default Method
Interface Inheritance
So here I have created another interface called Aliens (quite befitting since it was the sequel) and asked it to extend Alien interface. Here we are free to define the method in our new interface and it will not affect our class either:
Go ahead and run your program, it will work just fine.
Default Method
We can alternatively use the default method which I had explained about earlier. Meaning you have to simply put a keyword default against and declare it then and there:
Add pr.moaning(); in your main method in Prometheus class and run it. You will get the result as:
Growl! Chomp! Chomp! Zzzzzzz! Weird Off-Putting Sounds
Achieving Multiple Inheritance with Interfaces
Interfaces came into the picture since multiple inheritance wasn’t possible in Java with Classes. So, with the help of interfaces, multiple inheritance can be achieved. A class can “implement” multiple interfaces. Even an interface can extend multiple interfaces.
To use multiple interfaces you just have to use commas to separate each interface like this:
That is all. Also, make sure all of your interfaces’ methods have been declared in your class. Since the method has been created in a separate interface we need to use a variable of its own interface to call the method, since Alien’s reference type wouldn’t recognize it.
So, creating a separate reference type of the interface and instantiating Prometheus class (since interfaces can’t be instantiated)
Running the program will, of course, give you the same result:
Growl! Chomp! Chomp! Zzzzzzz! Weird Off-Putting Sounds
In the same manner, an interface can extend more than one interfaces. Just use the comma sign to separate them.
Static Method in Interface
Just as an interface allows us to declare default methods, there is another set of methods that you can use herein that lets you get away with implementation. Yes, you guessed right. Static method. The heading gave away?
Here’s a static method that I have declared in the Alien interface:
Now in order to call static methods, you generally used to make use of Class Name. Here since you have an interface just use it to call the method.
Hope you remember your Static basics, if not go back to the chapter about static class and methods.
Once again when you try to run this program you are going to get the same result:
Growl! Chomp! Chomp! Zzzzzzz! Weird Off-Putting Sounds
Marker or Tagging Interface
Those interfaces that do not have any method inside it is known as a marker or a tagging interface. So, the syntax for such an interface is going to be something like this:
interface InterfaceName { }
The reason we often resort to tagging interfaces is because it lets us:
- Have a common parent
- Add a data type to a class
The latter reason is the one that lets you get rid of the tension of implementing methods inside your class, primarily because there aren’t any. By doing so, your class becomes an Interface Type.
Nested Java Interface
Interfaces can also be nested amongst each other. For example:
interface Alien { void hunting(); interface Aliens { void moaning(); } }
And yes they can be declared within a class too. Also if you are planning on defining a class inside an interface that can be done too.
I think we have covered everything there is to learn about interfaces. If there’s more we are going to learn our way through it.
Till then go celebrate for a while. Clink those glasses. You deserve it.
2 Responses
[…] in order to tackle that situation we have the concept of Interfaces in […]
[…] Next story Java Interface | How to Use Interface in Java […]