Menu
CoddyTech

Overloading Konstruktor

Termasuk bagian Pemrograman Berorientasi Objek dalam perjalanan Java di Coddy. Pelajaran 8 dari 87.

Overloading constructor berarti memiliki beberapa constructor dengan daftar parameter yang berbeda. Gunakan this() untuk memanggil satu constructor dari constructor lainnya.

Beberapa constructor

public class Book {
    private String title;
    private String author;
    private int pages;
    
    public Book(String title, String author, int pages) {
        this.title = title;
        this.author = author;
        this.pages = pages;
    }
    
    public Book(String title, String author) {
        this.title = title;
        this.author = author;
        this.pages = 0;
    }
}

Perantaian konstruktor dengan this()

public class Book {
    private String title;
    private String author;
    private int pages;
    
    public Book(String title, String author, int pages) {
        this.title = title;
        this.author = author;
        this.pages = pages;
    }
    
    public Book(String title, String author) {
        this(title, author, 0);  // Memanggil konstruktor dengan 3 parameter
    }
    
    public Book() {
        this("Unknown", "Unknown");  // Memanggil konstruktor dengan 2 parameter
    }
}

Penggunaan

Book b1 = new Book("1984", "Orwell", 328);
Book b2 = new Book("Dune", "Herbert");
Book b3 = new Book();

this() harus menjadi pernyataan pertama di dalam konstruktor. Chaining menghindari duplikasi logika penetapan nilai di berbagai konstruktor.

challenge icon

Tantangan

Sedang

Buat tiga constructor overloaded untuk class Product dengan menggunakan constructor chaining:

  • Constructor 3 parameter: name, price, stock
  • Constructor 2 parameter: name, price (stock secara default bernilai 0)
  • Constructor default: tanpa parameter (name = "Unknown", price = 0, stock = 0)

Gunakan this() untuk menghubungkan constructor dan menghindari duplikasi kode.

Coba sendiri

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        double price = Double.parseDouble(scanner.nextLine());
        int stock = Integer.parseInt(scanner.nextLine());
        
        Product product1 = new Product(name, price, stock);
        Product product2 = new Product("Keyboard", 79.99);
        Product product3 = new Product();
        
        System.out.println("Product 1: " + product1.getName() + " - $" + product1.getPrice() + " (Stock: " + product1.getStock() + ")");
        System.out.println("Product 2: " + product2.getName() + " - $" + product2.getPrice() + " (Stock: " + product2.getStock() + ")");
        System.out.println("Product 3: " + product3.getName() + " - $" + product3.getPrice() + " (Stock: " + product3.getStock() + ")");
    }
}
quiz iconUji dirimu

Pelajaran ini berisi kuis singkat. Mulai pelajarannya untuk menjawab kuis dan melacak progresmu.

Semua pelajaran di Pemrograman Berorientasi Objek

Latihan sendiri: Compiler Java online