Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have two classes, Staff and AdvancedStaff, which extends the former.

Staff has this constructor:

public Staff (String number, String title, String name, String role, char level) {
        staffNumber = number;
        staffTitle = title;
        staffName = name;
        staffRole = role;
        payScaleLevel = level;
    }

I'll note that all instance variables have been set to private.

While, Advanced Staff has this constructor:

public AdvancedStaff (String number, String title, String name) {
        super(number, title, name);
        role = "Entry level Advanced Staff"; 
        level = 'A';
    }

However, this throws a "symbol not found" error for my Staff constructor.

I've tried using super.staffRole = "Entry level Advanced Staff"; but the private scope of my superclass prevents this.

I've found that adding the fields String role and char level to my AdvancedStaff constructor allows me to call the super constructor, but I'm wondering if there's a way to call the super constructor without passing all of its arguments in my subclass constructor?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
224 views
Welcome To Ask or Share your Answers For Others

1 Answer

You have to provide all arguments to the constructor.

In your case, you still can call the constructor of Staff, but you must provide some default values, like so:

super(number, title, name, "Entry level Advanced Staff", 'A');

This does the same work as what you're already doing in the constructor for AdvancedStaff, only now it's the Staff class setting the values of the private variables, since you're passing it via the constructor.

On a side note, if you plan on accessing these private variables from a subclass, you should really make them protected instead.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...