MediumPBO(10): Learning Interface with Java | by Kristian Agung Nugroho | Nov, 2026 | Medium
Sitemap
Kristian Agung Nugroho

Kristian Agung Nugroho

Still learning

PBO(10): Learning Interface with Java

6 min readNov 4, 2026

--

1. Introduction

What Is an Interface?

An interface is a concept in OOP that works like a contract. It tells a class what it must be able to do, but not how to do it. This makes the code more consistent and easier to extend.

For example, every TV remote has a power button and a volume button. The buttons are the same, but the inside of each remote is different for each brand. The buttons are the contract. The inside is the implementation.

What Does an Interface Contain?

An interface only contains method declarations (without a body) and constants.

  • All methods in an interface are public and abstract by default.
  • All variables in an interface are public, static, and final by default.
  • An interface cannot be used to create an object directly.

How Do We Use an Interface?

A class uses the keyword implements to follow an interface. The class must write the code for all methods from the interface. If not, the class must be declared abstract.

public class Student extends Person implements Learnable, Payable {
// ...
}
  • A class can implement more than one interface, separated by a comma.
  • If a class uses both, write extends first, then implements.
  • An interface can inherit from another interface using extends.

Interface vs Abstract Class

Press enter or click to view image in full size

2. Hands-On

2.1. Creating the OperasiHitung Interface

First, we create an interface called OperasiHitung. This interface contains one method named hitung().

  • public interface OperasiHitung defines an interface named OperasiHitung.
  • int hitung(int a, int b); declares a method without a body. Classes that implement this interface must write the code for it.
  • int is the return type, and a and b are the two numbers we want to calculate.

The OperasiHitung interface acts as a contract for all calculation classes.

2.2. Creating the Penjumlahan Class

Next, we create a class called Penjumlahan. This class implements the OperasiHitung interface and does an addition.

  • implements OperasiHitung means this class follows the contract from the interface.
  • @Override tells Java that this method comes from the interface.
  • return a + b; returns the sum of a and b.

The Penjumlahan class fills the contract with addition logic.

2.3. Creating the Pengurangan Class

Then, we create a class called Pengurangan. It also implements OperasiHitung, but it does a subtraction.

  • implements OperasiHitung means this class follows the same contract.
  • public int hitung(int a, int b) has the same method name and parameters as the interface.
  • return a - b; returns the result of a minus b.

The Pengurangan class uses the same method name as Penjumlahan, but the logic is different.

2.4. Creating the Main Class

Finally, we create the Main class to test both classes.

Press enter or click to view image in full size
  • OperasiHitung penjumlahan = new Penjumlahan(); creates a Penjumlahan object and stores it in an interface-type variable.
  • penjumlahan.hitung(10, 5) calls the addition logic, so the result is 15.
  • OperasiHitung pengurangan = new Pengurangan(); creates a Pengurangan object in the same way.
  • pengurangan.hitung(10, 5) calls the subtraction logic, so the result is 5.
  • System.out.println() displays the result in the output.

3. Program Output

After running Main.java, the program displays the following output.

Press enter or click to view image in full size

The output shows that Penjumlahan and Pengurangan have different implementations of hitung(). However, both classes are used in the same way through the OperasiHitung interface.

4. Assignment

Task: Build a payment program for a shop that sells Electronics and Food. Electronics have 10% tax, and Food has 5% tax.

Get Kristian Agung Nugroho’s stories in your inbox

Join Medium for free to get updates from this writer.

All classes are in the same package as the hands-on, praktikum10. Because Main already exists from section 2, the main class for this assignment is named MainBayar.

Press enter or click to view image in full size

4.1. Creating the Pembayaran Interface

First, we create an interface called Pembayaran. It contains a method named hitungPajak().

  • public interface Pembayaran defines an interface named Pembayaran.
  • double hitungPajak(double harga); declares a method without a body. It receives a price and returns the tax.
  • double is used because prices and taxes can have decimal values.

The Pembayaran interface acts as a basic contract for all product classes.

4.2. Creating the Elektronik Class

Next, we create the Elektronik class. It implements Pembayaran and calculates a 10% tax.

  • implements Pembayaran means this class must write the hitungPajak() method.
  • @Override marks the method that comes from the interface.
  • harga * 0.10 calculates 10% of the price.

The Elektronik class defines the tax rule for electronic products.

4.3. Creating the Makanan Class

Then, we create the Makanan class. It implements Pembayaran and calculates a 5% tax.

  • implements Pembayaran means this class follows the same contract as Elektronik.
  • harga * 0.05 calculates 5% of the price.

The Makanan class defines the tax rule for food products.

4.4. Creating the MainBayar Class

Finally, we create the MainBayar class to test both product classes.

Press enter or click to view image in full size
  • public class MainBayar is the main class for the assignment. It has a different name from Main so both can live in the same package.
  • Pembayaran elektronik = new Elektronik(); creates an Elektronik object using the interface type.
  • elektronik.hitungPajak(100000) calculates the tax for a price of 100000. 10% of it is 10000.0.
  • Pembayaran makanan = new Makanan(); creates a Makanan object using the same interface type.
  • makanan.hitungPajak(50000) calculates the tax for a price of 50000. 5% of it is 2500.0.
  • System.out.println() displays each result in the output.

The MainBayar class shows that two different products can be used through the same Pembayaran interface.

4.5. Assignment Output

After running MainBayar.java, the program displays the following output.

The output shows that Elektronik and Makanan follow the same contract but calculate tax in different ways: 10% for electronics and 5% for food.

5. Insights

  • An interface separates “what” from “how.” Pembayaran only says that a product must be able to calculate tax. Each class decides the rate by itself.
  • An interface type can hold any class that implements it. That is why we can write Pembayaran makanan = new Makanan();. The same code can work with many different classes.
  • It is easy to add new features. If the shop sells clothes later, we only create a new class that implements Pembayaran. The existing classes do not need to change.
  • The compiler protects the contract. If a class says implements but forgets one method, Java gives an error. This helps us avoid missing features.
  • Interface or abstract class? Use an interface when different classes need the same ability. Use an abstract class when the classes are closely related and share some code or data.

6. Conclusion

In this module, we learned that an interface is a contract that tells a class what to do, while the class decides how to do it. With implements, different classes can share the same method name but have different logic, like Penjumlahan and Pengurangan, or Elektronik and Makanan. A class can also implement more than one interface, which normal class inheritance cannot do. This makes our programs more consistent, flexible, and easy to extend.

7. Reference

Modul 10: Interface. Magelang: Program Studi Teknologi Informasi, Universitas Tidar.

8. GitHub Link