Explain the error: “constructor … cannot be applied: actual and formal differ in length”

前端 未结 1 1752
自闭症患者
自闭症患者 2021-01-26 02:37

Please help me fix this error:

constructor CollegeMember in class C10h1.CollegeMember cannot be applied to given types; required: java.lang.String,java.

相关标签:
1条回答
  • 2021-01-26 03:16

    The problem is your Student constructor. It needs to chain to the CollegeMember constructor, which it should do like this:

    public Student(String name, int year, String telNumber) {
        super(name, telNumber);
        this.year = year;
    }
    

    Likewise for Professor, just with rank instead of year

    Note that you don't need to set name and telNumber in those constructors - that's the job of the CollegeMember constructor you're chaining to. It would also be better if you made all the fields private - then you wouldn't even be able to try to set those fields in the subclasses... which makes sense, as they're not the responsibility of the subclasses.

    0 讨论(0)
提交回复
热议问题