Menu
Coddy logo textTech

Surcharge de constructeur

Fait partie de la section Programmation Orientée Objet du Journey Java de Coddy — leçon 8 sur 87.

La surcharge de constructeurs signifie avoir plusieurs constructeurs avec des listes de paramètres différentes. Utilisez this() pour appeler un constructeur depuis un autre.

Constructeurs multiples

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

Chaînage de constructeurs avec 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);  // Appelle le constructeur à 3 paramètres
    }
    
    public Book() {
        this("Unknown", "Unknown");  // Appelle le constructeur à 2 paramètres
    }
}

Utilisation

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

this() doit être la première instruction du constructeur. Le chaînage permet d'éviter de dupliquer la logique d'affectation entre les constructeurs.

challenge icon

Défi

Moyen

Créez trois constructeurs surchargés pour la classe Product en utilisant le chaînage de constructeurs :

  • Constructeur à 3 paramètres : name, price, stock
  • Constructeur à 2 paramètres : name, price (stock par défaut à 0)
  • Constructeur par défaut : aucun paramètre (name = "Unknown", price = 0, stock = 0)

Utilisez this() pour chaîner les constructeurs et éviter la duplication de code.

Aide-mémoire

La surcharge de constructeur permet d'avoir plusieurs constructeurs avec des listes de paramètres différentes dans la même classe.

Exemple de constructeurs multiples :

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

Le chaînage de constructeurs avec this() appelle un constructeur à partir d'un autre pour éviter la duplication de code :

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);  // Appelle le constructeur à 3 paramètres
    }
    
    public Book() {
        this("Unknown", "Unknown");  // Appelle le constructeur à 2 paramètres
    }
}

Utilisation :

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

Important : this() doit être la première instruction du constructeur.

Essayez vous-même

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 iconTestez-vous

Cette leçon comprend un petit quiz. Commencez la leçon pour y répondre et suivre votre progression.

Toutes les leçons de Programmation Orientée Objet