gucci
gucci

Reputation: 57

Overriding error Java, program does not read both methods only one

I have this code

public class Student extends Person {
    //id represents the student's ID
    private int id;
    //grade represents the student's grade in the course
    private Grade grade;

    //constructor allows user to define first and last names, id, and grade of student in demo
    public Student(String fName, String lName, int id, Grade grade) {
        super(fName, lName);
        this.id=id;
        this.grade=grade;
    }
    //get methods for fields
    public int getId() {
        return id;
    }
    public Grade getGrade() {
        return grade;
    }
    //toString prints out the string from person class along with id and grade fields in formatted string
    public String toString() {
        return super.toString()+"'s id is " + id + "." +getGrade();
    }
}

And this code. The issue is where toString method uses the passFailGrade getGrade() return value instead of the method located within the class

public class Grade {

    private double score;

    public Grade(double score) {
        this.score=score;
    }
    public void setScore(double score) {
        this.score=score;
    }
    public double getScore() {
        return score;
    }
    public char getGrade() {
        if (getScore()>=90)
            return 'A';
        else if (getScore()>=80)
            return 'B';
        else if (getScore()>=70)
            return 'C';
        else if (getScore()>=60)
            return 'D';
        else
            return 'F';
    }
    public String toString() {
        return "\nThe student recieved a " + getGrade() + 
                " and had a mark of " + getScore() + ".";
    }
}

Not sure if there's a problem in PassFailGrade:

public class PassFailGrade extends Grade {

    public PassFailGrade(double score) {
        super(score);
    }
    public char getGrade() {
        if (getScore()>=50)
            return 'Y';
        else
            return 'N';
    }
    public String toString() {
        return "(Y for yes/N for no) The student passed their course (" 
                + getGrade()+ ")." + super.toString();
    }
}

Then demo class just defining in constructors and printing

public class StudentDemo {
    public static void main(String[] args) {
        PassFailGrade bo= new PassFailGrade(98);
        Student s1 = new Student("bob", "blake", 123, bo);
        System.out.println(s1); 
    } 
}

Output:

bob blake's id is 123.(Y for yes/N for no) The student passed their course (Y). The student recieved a Y and had a mark of 98.0.

Upvotes: 1

Views: 267

Answers (1)

Makoto
Makoto

Reputation: 106508

You're actually overriding the getGrade() method. It's something you intended to do, but now you've got a problem when you call super.toString() - it still uses your overriden methods.

You can fix this issue by changing your toString in PassFailGrade in this way:

@Override
public String toString() {
    return "(Y for yes/N for no) The student passed their course ("
            + getGrade()+ ")." + "\nThe student recieved a " + super.getGrade() +
            " and had a mark of " + getScore() + ".";
}

Note that I only call super.getGrade(). This will produce the correct results.

Upvotes: 3

Related Questions