PBO(10): Learning Interface with Java
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
publicandabstractby default. - All variables in an interface are
public,static, andfinalby 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
extendsfirst, thenimplements. - An interface can inherit from another interface using
extends.
Interface vs Abstract Class
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 OperasiHitungdefines an interface namedOperasiHitung.int hitung(int a, int b);declares a method without a body. Classes that implement this interface must write the code for it.intis the return type, andaandbare 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 OperasiHitungmeans this class follows the contract from the interface.@Overridetells Java that this method comes from the interface.return a + b;returns the sum ofaandb.
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 OperasiHitungmeans 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 ofaminusb.
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.
OperasiHitung penjumlahan = new Penjumlahan();creates aPenjumlahanobject and stores it in an interface-type variable.penjumlahan.hitung(10, 5)calls the addition logic, so the result is15.OperasiHitung pengurangan = new Pengurangan();creates aPenguranganobject in the same way.pengurangan.hitung(10, 5)calls the subtraction logic, so the result is5.System.out.println()displays the result in the output.
3. Program Output
After running Main.java, the program displays the following output.
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.
4.1. Creating the Pembayaran Interface
First, we create an interface called Pembayaran. It contains a method named hitungPajak().
public interface Pembayarandefines an interface namedPembayaran.double hitungPajak(double harga);declares a method without a body. It receives a price and returns the tax.doubleis 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 Pembayaranmeans this class must write thehitungPajak()method.@Overridemarks the method that comes from the interface.harga * 0.10calculates 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 Pembayaranmeans this class follows the same contract asElektronik.harga * 0.05calculates 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.
public class MainBayaris the main class for the assignment. It has a different name fromMainso both can live in the same package.Pembayaran elektronik = new Elektronik();creates anElektronikobject using the interface type.elektronik.hitungPajak(100000)calculates the tax for a price of 100000. 10% of it is10000.0.Pembayaran makanan = new Makanan();creates aMakananobject using the same interface type.makanan.hitungPajak(50000)calculates the tax for a price of 50000. 5% of it is2500.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.”
Pembayaranonly 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
implementsbut 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.










