Infinite_Loop
Infinite_Loop

Reputation: 380

Accessing methods and variables through super in Java

I am trying to implement the below code, but I am getting an error in class HistoryStudent extends Student, sGrade. How do I go about adding the student grade through super? This is an assignment and we are not supposed to change anything in the abstract class or any part of the below code.

abstract class Student
{
    private String studentID    =   "" ;
    private int studentGrade    =   0 ;

    public Student (String sID, int sGrade)
    {
        studentID       = sID  ;
        studentGrade    = sGrade ;

        return ;
    }


    public String getStudentID      () { return studentID       ;}
    public double getStudentGrade   () { return studentGrade    ;}


    boolean updateGrade (int grade)
    {
        boolean check = false ;

        studentGrade += grade;

        return check ;
    }

}

class HistoryStudent extends Student
{
    public HistoryStudent (String sID, int sGrade)
    {
        super(sID,sGrade) ;


        return ;
    }


    boolean updateGrade (int grade)
    {
        boolean checking = false ;

            //Problem here
        **sGrade** += grade;

        return checking ;
    }
}

Any help appreciated.

Upvotes: 0

Views: 54

Answers (3)

Devavrata
Devavrata

Reputation: 1795

change update method in HistoryStudent class.

boolean updateGrade(int grade)

{
    boolean checking = false ;
    super.updateGrade(grade);
    return checking ;
}

Upvotes: 1

Kick
Kick

Reputation: 4923

//Problem here
**sGrade** += grade;   //Line mention in code

sGrade is local variable declare in the HistoryStudent constructor so you cant access it in the updateGrade method.

What you can do is declare the sGrade as global variable and while calling the super constructor in the method set the global variable value and access it in the updateGrade method.

class HistoryStudent extends Student
{
    int sGrade;

    public HistoryStudent (String sID, int sGrade)
    {
        super(sID,sGrade) ;
        this.sGrade = sGrade;
    }


    boolean updateGrade (int grade)
    {
        boolean checking = false ;
        sGrade += grade;    // Dnt know usage bcoz everytime method will return false
        return checking ;
    }
}

Upvotes: 1

Smutje
Smutje

Reputation: 18163

Check the super class for updateGrade (int grade) which does exactly what you want to do.

Upvotes: 0

Related Questions