Java Interface Example Tutorial (original) (raw)
Hello guys, if you are wondering what Java interface is and how to do your user interface in Java, you have come to the right place. Earlier, I have written about the actual use of the interface in Java, and in this article, I will give you an example of an interface in Java. I will also explain what an interface is and how and where you should use it. An interface is nothing but a name. As Joshua Bloch advised in the Effective Java book, the interface is great for declaring type. So, if you want to declare your own type like Employee, Order, Listener, etc., you can use the interface.
The key advantage of using an interface in Java is the flexibility it provides. Once you declare an interface, you can use the interface for declaring variables, defining method arguments, and return types, and so on. This adds flexibility to your code because you can consume any implementation of the interface at runtime.
For example, you can write code for starting and stopping a Machine, as shown in our example, and this code can be used to start/stop any kind of machine, like a TV, Car, Aircon, Computer, Smartphone, etc. This is great flexibility you can have, and it comes from the abstract nature of the interface.
Here are some important points about the interface in Java.
- An Interface can extend another interface.
- A class can be abstract if it doesn't implement all methods of the interface.
- From Java 8, the interface can contain default methods.
- An interface can not contain static methods.
If you have any questions concerning the interface or this example, feel free to ask in the comments, I would be happy to clear your doubts.
How to use an interface in Java Program
And, here is our complete example of using an interface in Java. You can see we have an interface called Machine to represent any kind of machine, and then we have written some code using Machine. Later we create two implementations of a Machine, Car, and a WaterPump, and then the same piece of code works fine for both machines.
By the way, if you are new to the Java programming world, then I also suggest you join these comprehensive Java courses to learn to master essential Java and object-oriented concepts and learn core development step-by-step and make your first unique, advanced program in 30 days.
And, now let's see the full code example of how to use an interface in a Java program.
public class Testing { public static void main(String args[]) {
Machine machine = new Car();
machine.start();
machine.stop();
machine = new WaterPump();
machine.start();
machine.stop();
paint(new Car());
paint(new WaterPump());
}
public static void paint(Machine m) {
m.stop();
System.out.println("Painting ...");
m.start();
}
}
interface Machine { public void start(); public void stop(); }
class WaterPump implements Machine { @Override public void start() { System.out.println("Starting WaterPump .."); } @Override public void stop() { System.out.println("Stopping WaterPump .."); } }
class Car implements Machine { @Override public void start() { System.out.println("Starting Car"); } @Override public void stop() { System.out.println("Stopping Car"); } }
Output: Starting Car Stopping Car Starting WaterPump .. Stopping WaterPump .. Stopping Car Painting ... Starting Car Stopping WaterPump .. Painting ... Starting WaterPump ..
That's all about how to use an interface in the Java program. This is a straightforward example of the interface, but it shows how powerful a concept is. When you are a beginner, it is hard to understand the value provided by the interface.
Still, when you start writing code, particularly object-oriented code, you will realize how an interactive helps in writing clean code, which is both flexible and robust.
And, now one question for you, what is the difference between an interface and an abstract class in Java? This is actually a popular question from Java interview, see if you can answer it correctly.