public enum Scale2 {
GOOD('C') {
public char getGrade() {
return grade;
}
},
BETTER('B') {
public char getGrade() {
return grade;
}
},
BEST('A') {
public char getGrade() {
return grade;
}
};
private char grade;
Scale2(char grade) {
this.grade = grade;
}
// (1) INSERT CODE HERE
public char getGrade() {
return grade;
}
public static void main (String[] args) {
System.out.println(GOOD.getGrade());
}
}
This is a program from khalid mughal scjp guid and following are the options and questions. When I tried to run this in eclipse, Its saying the non static grade cannot access from static context,I think according to concept its right,but I am confused wheather book is write or I am…please replay.
Which code, when inserted at (1), will make the program print C?
Select the two correct answers.
(a) public char getGrade() { return grade; }
(b) public int getGrade() { return grade; }
(c) abstract public int getGrade();
(d) abstract public char getGrade();
Problem with the sample code is that
gradeis declared asprivate. sogradeis not accessible from its subclasses.Either
gradeshould be accessible from its subclasses or subclasses ofScale2should accessgradethroughsuper.getGrade()method.You have inserted the possibly correct code in your post, and yes, the book is wrong.