Menu
Coddy logo textTech

Yapıcı Metot Aşırı Yükleme

Coddy'nin Java Journey'sinin Nesne Yönelimli Programlama bölümünün bir parçası — ders 8 / 87.

Yapıcı metot aşırı yüklemesi (constructor overloading), farklı parametre listelerine sahip birden fazla yapıcı metoda sahip olmak anlamına gelir. Bir yapıcı metodu diğerinden çağırmak için this() kullanın.

Birden fazla yapıcı (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;
    }
}

this() ile yapıcı zincirleme (constructor chaining)

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);  // 3 parametreli yapıcıyı çağırır
    }
    
    public Book() {
        this("Unknown", "Unknown");  // 2 parametreli yapıcıyı çağırır
    }
}

Kullanım

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

this() yapıcı (constructor) içindeki ilk ifade olmalıdır. Zincirleme (chaining), yapıcılar arasında atama mantığının yinelenmesini önler.

challenge icon

Görev

Orta

Yapıcı zincirleme (constructor chaining) kullanarak Product sınıfı için üç adet aşırı yüklenmiş yapıcı metot oluşturun:

  • 3 parametreli yapıcı: name, price, stock
  • 2 parametreli yapıcı: name, price (stock varsayılan olarak 0)
  • Varsayılan yapıcı: parametresiz (name = "Unknown", price = 0, stock = 0)

Yapıcıları zincirlemek ve kod tekrarını önlemek için this() kullanın.

Kopya kağıdı

Yapıcı metot aşırı yüklemesi (constructor overloading), aynı sınıfta farklı parametre listelerine sahip birden fazla yapıcı metodun bulunmasına olanak tanır.

Birden fazla yapıcı metot örneği:

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;
    }
}

this() ile yapıcı metot zincirleme (constructor chaining), kod tekrarını önlemek için bir yapıcı metodun diğerini çağırmasını sağlar:

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);  // 3 parametreli yapıcıyı çağırır
    }
    
    public Book() {
        this("Unknown", "Unknown");  // 2 parametreli yapıcıyı çağırır
    }
}

Kullanım:

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

Önemli: this() yapıcı metot içindeki ilk ifade olmalıdır.

Kendin dene

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 iconKendini test et

Bu ders kısa bir quiz içerir. Soruları yanıtlamak ve ilerlemeni kaydetmek için derse başla.

Nesne Yönelimli Programlama bölümündeki tüm dersler