Java Interface Example for Beginners

Hello guys, if you are wondering what is Java interface and how to do your user interface in Java then 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 is an interface 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 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. 
  1. An Interface can extend another interface
  2. A class can be abstract if it doesn't implement all methods of the interface.
  3. From Java 8, the interface can contain default methods.
  4. An interface can not contain static methods.
If you have any questions with respect to interface or this example, feel free to ask in 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 a comprehensive Java course like The Complete Java Masterclass 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.


Java Interface Example for Beginners




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 Java program. This is a very very simple example of the interface but it shows how powerful a concept it is. When you are a beginner it is hard to understand the value provided by interface but 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

Thanks for reading this article so far. If you like an object-oriented programming tutorial then please share it with your friends and colleagues. If you have any questions or feedback then please drop a note.

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.

No comments:

Post a Comment

Feel free to comment, ask questions if you have any doubt.