Java OOP | Anonymous Class

Java OOP | Anonymous Class

Java Anonymous Class

Di java kita bisa membuat sebuah kelas didalam kelas. Itu dikenal sebagai Nested Class. Kita juga bisa membuat nested class tanpa memiliki nama. Itu dikenal sebagai anonymous class.

Anonymous class sebenarnya termasuk anonymous inner class (kelas dalam anonim). Oleh karena itu anonymous class harus didefinisikan didalam outher class (kelas luar).

Sintaks dasar membuat anonymous class

public class OutherClass { // mendefinisikan anonymous inner class TipeData variable = new Type() { // body anonymous class }; }

Anonymous class adalah kelas yang didefinisikan didalam kelas lain tanpa memiliki nama. Anonymous class biasanya berguna untuk memperluas subkelas atau mengimplementasikan interface/abstract secara langsung dalam bentuk sederhana.

Java OOP | Anonymous Class

Anonymous Class Extends Class

Ini adalah beberapa contoh untuk memperluas kelas dengan anonymous inner class . Anonymous class dapat diterapkan dibeberapa tempat seperti:

  1. didalam body method
  2. didalam argumen pemanggilan method
  3. instance initializers
  4. fields (attribute)
  5. local class

Contoh 1: Memperluas kelas dengan anonymous class didalam body method

public class Company { public void seyHello() { System.out.println("Hello Company.. This is inside in Company class"); } } public class Employe { public void seyHelloEmploye() { // membuat anonymous class didalam method untuk memperluas kelas Company Company company = new Company() { @Override public void seyHello() { System.out.println("Hello Employe.. This is inside in Employe class"); } }; company.seyHello(); } } public class Main { public static void main(String[] args) { Employe developer = new Employe(); developer.seyHelloEmploye(); } }

Output:

Hello Employe.. This is inside in Employe class

Contoh 2: Di dalam argumen pemanggilan method

public class Button { public void onClick() {} } public class Email { public void sendButton(Button button) { button.onClick(); } } public class Main { public static void main(String[] args) { // membuat object Email Email email = new Email(); /* memanggil method sendButton dari object Email. dengan mengirimkan data argumen berupa anonymous class */ email.sendButton(new Button() { @Override public void onClick() { System.out.println("Tombol kirim berhasil di Click... 'Pesan Terkirim' "); } }); } }

Output:

Tombol kirim berhasil di Click... 'Pesan Terkirim'

Contoh 3: Instance initializers

public class Engine { private String engineType; private float speedLimit; public String getEngineType() { return engineType; } public float getSpeedLimit() { return speedLimit; } } public class Car { private String carName; private String carType; private Engine engine; public Car(String carName, String carType) { this.carName = carName; this.carType = carType; // anonymous class this.engine = new Engine() { @Override public String getEngineType() { if(carType.equals("Fortuner")) return "4 Silinder"; return "2 Silinder"; } @Override public float getSpeedLimit() { if(carType.equals("Fortuner")) return 190.0F; return 0; } }; } public String getCarName() { return carName; } public String getCarType() { return carType; } public String getCarEngineType() { return engine.getEngineType(); } public float getCarSpeedLimit() { return engine.getSpeedLimit(); } } public class CarMainApp { public static void main(String[] args) { Car car = new Car("TOYOTA", "Fortuner"); System.out.println("Car Name :"+car.getCarName()); System.out.println("Car Type :"+car.getCarType()); System.out.println("Car Engine Type :"+car.getCarEngineType()); System.out.println("Car Speed Limit :"+car.getCarSpeedLimit()+" Km"); } }

Output:

Car Name : TOYOTA Car Type : Fortuner Car Engine Type : 4 Silinder Car Speed Limit : 190.0 Km

Contoh 4: Fields

public class OutherClass { private int var1; private String var2; Anonymous var3 = new Anonymous() { // body implementasi } }

Anonymous Class Implements Interface/Abstract

implementasi interface/abstract dengan anonymous inner class sama seperti memperluas class yang dapat diterapkan dibeberapa tempat.

Contoh 1: mengimplementasikan interface dengan anonymous class didalam body method

public interface Employe { void seyHello(); } public class Developer { public void getDeveloperSeyHello() { // implementasi interface dengan anonymous class Employe employe = new Employe() { @Override public void seyHello() { System.out.println("Hello Developer...!"); } } employe seyHello(); } } public class CarMainApp { public static void main(String[] args) { Developer developer = new Developer(); developer.getDeveloperSeyHello(); } }

Output:

Hello Developer...!

Contoh 2: Di dalam argumen pemangilan method

public interface MobilePhone { void turnOnPhone(); void turnOfPhone(); } public class SetOnClickListener { public static void onClick(MobilePhone mobilePhone) { mobilePhone.turnOnPhone(); } } public class Main { public static void main(String[] args) { SetOnClickListener.onClick(new MobilePhone() { @Override public void turnOnPhone() { System.out.println("Turn on the Phone..!"); } @Override public void turnOfPhone() { System.out.println("Turn of the Phone...!"); } }); } }

Output:

Turn on the Phone..!