Java OOP | Nested Class

Java OOP | Nested Class

Nested Class In Java

Pada pemograman java kita dapat membuat kelas didalam kelas. Kelas seperti ini disebut nested class (kelas bersarang). Tujuan dari membuat nested class adalah ketika kita membutuhkan untuk membuat beberapa kelas yang saling berhubungan, dimana kelas tidak bisa dibuat tanpa kelas lain.

public class OutherClass { // ... public class InnerClass { // ... } }

Kelas didalam kelas disebut inner class. Dan kelas diluar disebut outher class

Cara mengakses inner class, buat terlebih dahulu object outher class kemudian buat object inner class.

// membuat object kelas luar OutherClass outher = new OutherClass(); // membuat object kelas dalam OutherClass.InnerClass inner = outher.new InnerClass(); // atau langsung seperti ini OutherClass.InnerClass inner = new OutherClass().new InnerClass();

Keuntungan inner kelas adalah kita dapat mengakses informasi outher class, seperti attribute & method walaupun acces modifiernya private. Untuk mengaksesnya gunakan keyword nama outherclass keyword this lalu nama attribute / method.

// outher class public class OutherClass { private int x; // method constructor public OutherClass(int x) { this.x = x; } private int getX() { return x; } // inner class public class InnerClass { private int y; public InnerClass(int y) { this.y = y; } public int getY() { return y; } // mengakses method "getX" outher class public int getX() { return OutherClass.this.getX(); } // mengakses attribute "x" outher class public void calculation() { int result = OutherClass.this.x * this.y; System.out.println("Hasil Kalkulasi X & Y : "+result); } } } public class Main { public static void main(String[] args) { OutherClass outher = new OutherClass(10); OutherClass.InnerClass inner = outher.new InnerClass(5); System.out.println("Nilai Y : "+inner.getY()); System.out.println("Nilai X : "+inner.getX()); inner.calculation(); /*OUTPUT: Nilai Y : 5 Nilai X : 10 Hasil Kalkulasi X & Y : 50 */ } }

Private Inner Class

Inner class dapat diberi access modifier private atau protected. Jika anda tidak ingin inner class dapat diakses oleh object lain, definisikan dengan private.

public class OutherClass { // ... //inner class private private class InnerClass { // ... } } public class Main { public static void main(String[] args) { OutherClass outher = new OutherClass(10); // error waktu kompilasi: has private access OutherClass.InnerClass inner = outher.new InnerClass(5); } }

Static Inner Class

Di java kita bisa membuat inner class yang didefinisikan sebagai static. Artinya kita dapat membuat object inner class tanpa membuat object outher class, caranya.

OutherClass.InnerClass inner = new OutherClass.InnerClass(5);

Contoh diatas kita membuat object static inner class hanya menggunakan nama kelas dari inner class. Problemnya kita tidak dapat mengakses informasi attribute & method pada outher class nya, menggunakan outherclass.this.

Kita tidak dapat membuat attribute atau method inner class menjadi static. Jika inner class nya tidak didefinisikan sebagai static.

Contoh Program Nested Class

// outher class public class Car { private String carName; private String carType; public Car(String carName, String carType) { this.carName = carName; this.carType = carType; } private String getCarName() { return carName; } private String getCarType() { return carType; } // inner class public class Engine { private float speedLimit; private String engineType; public void setEngineType() { // mengakses method outher class switch(Car.this.getCarName()) { case "Toyota" : // mengakses method outher class if(Car.this.getCarType().equals("Fortuner")) { engineType = "4 Silinder"; speedLimit = 190.0F; } else { engineType = "2 Silinder"; speedLimit = 100.0F; } break; case "Bmw" : // mengakses method outher class if(Car.this.getCarType().equals("Bmw X1")) { engineType = "3 Silinder"; speedLimit = 201.0F; } else { engineType = "2 Silinder"; speedLimit = 100.0F; } break; default: break; } } public float getSpeedLimit() { return speedLimit; } public String getEngineType() { return engineType; } } } //File: CarMain.java public class CarMain { public static void main(String[] args) { // membuat object other class Car toyota = new Car("Toyota", "Fortuner"); // membuat object inner class Car.Engine engineCar = toyota.new Engine(); engineCar.setEngineType(); System.out.println("Toyota engine type : "+engineCar.getEngineType()); System.out.println("Toyota engine speed limit : "+engineCar.getSpeedLimit()+" Km"); /*OUTPUT: Toyota engine type : 4 Silinder Toyota engine speed limit : 190.0 Km */ } }

Contoh Program Static Nested Class

public class Company { public static class Developer { private float salary; private float bonus = 0.1F; public Developer(float salary) { this.salary = salary; } public long getSalary() { long totalSalary = (long) ((salary * bonus) + salary); return totalSalary; } } public static class Manager { private float salary; private float bonus = 0.15F; public Manager(float salary) { this.salary = salary; } public long getSalary() { long totalSalary = (long) (salary * bonus + salary); return totalSalary; } } } public class CompanyMain { public static void main(String[] args) { // membuat object static inner class Company.Developer developer = new Company.Developer(10000000F); System.out.println("Developer Salary : Rp."+developer.getSalary()); // membuat object static inner Manager Company.Manager manager = new Company.Manager(18500000); System.out.println("Manager Salary : Rp."+manager.getSalary()); /*OUTPUT: Developer Salary : Rp.11000000 Manager Salary : Rp.21275000 */ } }