Konstruktor w klasie dziedziczącej

0

Witam,
mam klasę Wood, która dziedziczy po klasie Material.

package com.tomasztopolewski;

public class Material implements Ore {
    private int quantity;
    private int producedQuantity;

    public Material(int quantity, int producedQuantity) {
        this.quantity = quantity;
        this.producedQuantity = producedQuantity;
    }

    public Material() {
        this.quantity = 0;
        this.producedQuantity = 0;
    }

    public int setQuantity(int quantity) {
        return this.quantity = quantity;
    }

    public int getQuantity() {
        return this.quantity;
    }

    public void setProducedQuantity(int producedQuantity) {
        this.producedQuantity = producedQuantity;
    }

    public int getProducedQuantity() {
        return this.producedQuantity;
    }

    public int getSumQuantity() {
        return this.quantity + this.producedQuantity;
    }
}
 

Po utworzeniu obiektu poprzez konstruktor z podanymi wartościami:

 Wood oreWood = new Wood(10, 50);

wyskakuje następujący błąd:

Error:(5, 24) java: constructor Wood in class com.tomasztopolewski.Wood cannot be applied to given types;
required: no arguments
found: int,int
reason: actual and formal argument lists differ in length

Program działa poprawnie po stworzeniu obiektu z użyciem konstruktora domyślnego już z przypisanymi wartościami.

1

W Javie konstruktora się nie dziedziczy, więc tak jak masz w błędzie. Wywołałes domyślny konstruktor z parametrami int, int.

Ale mozesz go wywołać poprzez super(int, int) ;

1 użytkowników online, w tym zalogowanych: 0, gości: 1