Stevie
Stevie

Reputation: 1

Initializing Variable in Subclass

public class Fiction extends Book{
    String Author;
    public Fiction(String name, String refNum, int copiesOwned, String Author) {
        super(name, refNum, copiesOwned, Author);

    }
}

Basically I am trying to give one of my subclasses a variable (for only this subclass). I am not allowed to put it in my abstract class code, and it says there is a problem in my constructor.

How do I initialize this variable?

Upvotes: 0

Views: 34

Answers (1)

DoctorMick
DoctorMick

Reputation: 6793

You shouldn't be passing the extra variable (Author) to the super class constructor as it won't exist as a parameter in that class. This should work:

public class Fiction extends Book{
    String Author;
    public Fiction(String name, String refNum, int copiesOwned, String Author) {
        super(name, refNum, copiesOwned);

    }
}

Upvotes: 1

Related Questions