What is a Java Interface?

167
0
SHARE

Java contains Interfaces, Classes and Objects. Distinguishing between each can be slightly difficult at first, but I will explain exactly what a Java interface is in the post.

Firstly, I’d like to start with a short definition of the word “Interface”.

A device or program enabling a user to communicate with a computer.

Given this definition, we know that an Interface is a medium between a user and a computer.

Now, you might be asking, what does an interface look like in Java?

Interfaces look like Classes. However, on their own, their purpose is slightly different.

We would set the file name to the name of the class in this case. However, you may still be wondering WHAT IS HAPPENING.

Related:  How to Use Java's Scanner to Read Text Files

To put this in the most simple and sweet manner: An Interface forces the class that implements it to contain the methods laid out in the interface.

So, let’s say we want a Coffee to implement a Hot Drink interface. In the Hot drink interface, we can set some methods equating to:

  • Temperature
  • Water
  • Milk
  • Sugar
  • Ingredients[]

That way we can always be sure that the hot drinks are implementing the right methods!

You don’t want coffee to not have any water in do you? No.

I hope this explains interfaces for you in a beautiful manner!

Until next time, Josh.