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.
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.
/**
* A simple example of using an interface in Java program.
* @author Javin Paul
*/
public class Testing {
public static void main(String args[]) {
Machine machine = new Car();
machine.start(); // starting car via Machine's start() method
machine.stop();
machine = new WaterPump();
machine.start(); // starting stopping WaterPump via interface
machine.stop();
// painting Machine
paint(new Car());
paint(new WaterPump());
}
/*
* You can write a method which depends on interface than
* implementation.
*/
public static void paint(Machine m) {
m.stop(); //stop machine before cleaning
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.
Other Java programming and OOP tutorials You may like
- My favorite courses to learn object-oriented programming
- 5 differences between Hashtable and HashMap in Java
- Difference between HashMap and ConcurrentHashMap in Java
- 7 best OOP design pattern courses for Java developers
- What is a factory method design pattern in Java?
- Difference between TreeSet and HashSet in Java
- Top 5 Courses to Learn Design Patterns in Java
- 5 Free Data Structure and Algorithms Courses
- 10 Java Coding Interview Questions and Answers for Java beginners.
- 5 Free Spring Framework Courses for Java Developers
- 10 Free Courses to learn Java for Beginners and Experienced Programmers
- Difference between an interface and an abstract class in Java.
P. S. - If you are serious about learning object-oriented programming and looking for a free online course to start with, then you can also check this list of free OOP courses for beginners. It's completely free, and you just need a free Udemy account to join this course.
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.
From Java 8 onwards interface can be used much more than just declaring type hierarchy as what Joshua bloch told in Effective Java. You can now declare static and default method inside interface which is quite powerful as it allows extension of popular interface like Map, or Collection which wasn't possible earlier without breaking all existing implementation.
ReplyDelete