Why We Need Java Interface rather than just Creating Class and How to Implement it (with Demo)

tanut aran
3 min readApr 7, 2024

--

Why we need interface

To ensure the structure.

Java compose of classes.

Some of these classes are ‘related’ and we need some mechanism to enforce some degree of strucutre over it.

With this, your code into the giant bunch of mess over time.

Example and Demo

Let’s think about the memory device with the connector of different type.

In this case, thumb drive has USB-A and USB-C.

In Java, this will be ThumbDrive class implementing UsbA and UsbC interfaces.

When you have this interface your must ‘do something’ to make it work correctly.

That enforce you to ‘implement the method’.

Demo Step 1 : Declaring the interface

Our memory device need to readData and writeData into it no matter how big it or how it appears.

We define two interface USB-A and USB-C here.

Note that some method can be duplicated.

public interface UsbA {
String memory = "";

void writeData(String data);

String readData();
}
public interface UsbC {
String memory = "";

void writeData(String data);

String readData();

boolean isEnableFastCharge = true;

void setEnableFastCharge();
}

Demo Step 2 : Implement the interface

Now we have our devices SimpleMemoryStick and ThumbDrive

These two class need to be ‘structured’.

These two class implemented Usb interface.

So it is forced to implement the readData and writeDate method.

You can see in the IDE it is forced to do so.

public class SimpleMemoryStick implements UsbA {

private String memory = "";

@Override
public void writeData(String data) {
this.memory += data;
}

@Override
public String readData() {
return this.memory;
}

}
public class ThumbDrive implements UsbA, UsbC {

private String memory = "";

@Override
public String readData() {
return this.memory;
}

@Override
public void setEnableFastCharge() {
}

@Override
public void writeData(String data) {
this.memory += data;
}

}

Demo Step 3 : Create and run the program

Now we go to Hello.java to make the main script.

We input the data into two type of device and read from them.

public class Hello {
public static void main (String agrs[]) {

// Stick
System.out.println("- Stick");
SimpleMemoryStick stick = new SimpleMemoryStick();
stick.writeData("foo");
stick.writeData("bar");
System.out.println(stick.readData());

// ThumbDrive
System.out.println("- Drive");
ThumbDrive drive = new ThumbDrive();
drive.writeData("foo");
drive.writeData("bar");
drive.writeData("baz");
System.out.println(drive.readData());
}
}

In this simple demo, we just put the file all in one folder.

Hello.java
SimpleMemoryStick.java
ThumbDrive.java
UsbA.java
UsbC.java

Now we need to compile all of file javac *.javato produce *.class

Then we run the main class java Hello.java

Here we go with the result.

Bonus : Interface VS Abstract Class

Multiple inheritance is allowed while abstract class does not.

public class A extends B,C {} // NOT POSSIBLE

public class A implements D,E {} // POSSIBLE

--

--

tanut aran
tanut aran

Written by tanut aran

Co-founder and Coder at work !

Responses (2)