Concept of Interface in Java

It has been a while since my previous Mediu update. It was super busy and I was re-thinking which direction should I focus on for the next coming 6 months. Maybe I will write another post later on (around August) to talk about my career pathway before I graduate from the University to my situation right after graduation. Anyway, Java will be my focus for the next 6 months due to some reason to be mention later on. Today, let us talk about the interface in Java.

Definition

An interface specifies methods for a particular class that implements the interface must implement.

The keyword interface in java is like a blueprint that standardizes the way a particular set of classes is used. The interface class itself is abstract and there is no actual code of any of the methods in the interface class but only supply the actual method names and parameters. We use an interface class to provide a common behavior of a particular classification of a thing (class) in the real-world so that it can be used by several classes by having methods implemented in the same interface class.

Example

Let us consider developing classes for a game that has 2 characters of a knight and a witch. These 2 classes may share some common behaviors (method) but on the other hand, different operations within the behavior. Now, the interface class acts like a blueprint for developers to stick with the rules that they must implements when they want to instantiate a specific class. Consider the following character interface class.

An interface class that defined a character can move and attack, as well as if they are allowed to use sword or magic when it is created.

Now, let us create a class of knight that refer to the rules in the interface class character (implements from the character).

If one of the methods is removed in the class that implements the interface, there will be an error to show that not all methods are being implemented from the interface. Even the implemented class does not need that function, we still have to implement it. In other words, the interface allows the class to override the function inside the interface and it is a must for that implemented class to implemented all methods declared in the interface class.

For the use of the interface, we cannot instantiate it as an object of the class of the interface and we have to use the actual class that has overridden that functionality. However, we are still able to use the interface as the data type to use the appropriate class that has implemented the interface.

When we use the interface as the data type to instantiate an actual class, we can use the variable class created to point towards another class that is implemented from the same interface but a different class. Let us consider the other class of witch that also implements from the character interface.

We can then use the data type of interface to point towards different classes is possible as they implement from the same interface.

It is the same when you are using generic collection classes to instantiate an ArrayList. It is always good to use a generic data type first, then to specify the actual type later in the program.

In C++, there is no interface for programmers to use where they have to write their own class as a blueprint and do all polymorphism (virtual function) themself and it supports multiple inheritances. However, in Java, can only inherit from one class but you can still implement from many interfaces. Multiple inheritances are only allowed by the implementation of several interfaces.

Conclusion

I have talked about the concept of the interface where it is just like a contract of blueprint to a thing in the rea-world as a class. Whenever users want to design a class that is part of the blueprint, it is a must for them to implement all methods that are implemented in the interface classes.

Although it has been a while for me to update on Medium, I still would like to thanks those who are still reading my post as a study review. I will talk about my current career-related things in the summer but I guess my post will mainly focus on Java instead of C++ at least for the next coming 6 months.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store