implicit super constructor Person() is undefined. Must explicitly invoke another constructor?

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-30 16:27:44

问题


I am working on a project and i am getting the error "implicit super constructor Person() is undefined. Must explicitly invoke another constructor" and i don't quite understand it.

Here is my person class:

public class Person {
    public Person(String name, double DOB){

    }
}

And my student class when trying to implement the person class, and giving it an instructor variable.

public class Student extends Person {

    public Student(String Instructor) {

    }

}

回答1:


If a constructor does not explicitly invoke a superclass constructor, the Java compiler automatically inserts a call to the no-argument constructor of the superclass.

If the super class does not have a no-argument constructor, you will get a compile-time error. Object does have such a constructor, so if Object is the only superclass, there is no problem.

Reference: http://docs.oracle.com/javase/tutorial/java/IandI/super.html : (See under section 'SubClass Constructors')

So whenever dealing with parameterized constructors make a super(parameter1, parameter2 ..) call to the parent constructor. Also this super() call should be the FIRST line in your constructor block.




回答2:


You need to make a super call to your defined constructor:

public Student(String instructor) {
    super(/* name */, /* date of birth */);
}

You can't just call super() because that constructor is not defined.




回答3:


This is how I achieved it (in my case the superclass was Team and the subclass was Scorer):

// Team.java
public class Team {
    String team;
    int won;
    int drawn;
    int lost;
    int goalsFor;
    int goalsAgainst;
    Team(String team, int won, int drawn, int lost, int goalsFor, int goalsAgainst){
        this.team = team;
        this.won = won;
        this.drawn = drawn;
        this.lost = lost;
        this.goalsFor = goalsFor;
        this.goalsAgainst = goalsAgainst;
    }
    int matchesPlayed(){
        return won + drawn + lost;
    }
    int goalDifference(){
        return goalsFor - goalsAgainst;
    }
    int points(){
        return (won * 3) + (drawn * 1);
    }
}
// Scorer.java
public class Scorer extends Team{
    String player;
    int goalsScored;
    Scorer(String player, int goalsScored, String team, int won, int drawn, int lost, int goalsFor, int goalsAgainst){
        super(team, won, drawn, lost, goalsFor, goalsAgainst);
        this.player = player;
        this.goalsScored = goalsScored;
    }
    float contribution(){
        return (float)this.goalsScored / (float)this.goalsFor;
    }
    float goalsPerMatch(){
        return (float)this.goalsScored/(float)(this.won + this.drawn + this.lost);
    }
}



回答4:


When creating a subclass constructor, if you don't explicitly call a superclass constructor with super, then Java will insert an implicit call to the no-arg "default" superclass constructor, i.e. super();.

However, your superclass Person doesn't have a no-arg constructor. Either supply an explicit no-arg constructor in Person or explicitly call the existing superclass constructor in the Student constructor.




回答5:


you can't create an instance without calling a constructor of its super class. And the jvm doesn't know how to call Person(String, double) from your Student(String) constructor.



来源:https://stackoverflow.com/questions/23395513/implicit-super-constructor-person-is-undefined-must-explicitly-invoke-another

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!