| Author |
Can sumone explain this to me? regarding attributes
|
Terry Huang
Greenhorn
Joined: May 16, 2006
Posts: 25
|
|
why is the output "easy"? thx
|
 |
aymane chetibi
Ranch Hand
Joined: Apr 12, 2006
Posts: 175
|
|
first you are missing a semi colon. try to find it. second if you put this semicolon back the output is not easy the output is : Cannot find symbol symbol : method printDifficulty() location: class SCJPExam myExam.printDifficulty(); regards,
|
 |
Keith Lynn
Ranch Hand
Joined: Feb 07, 2005
Posts: 2341
|
|
|
I think he meant to have SCJPExam extends Exam.
|
 |
aymane chetibi
Ranch Hand
Joined: Apr 12, 2006
Posts: 175
|
|
yes Lynn, with inheritance the output is easy. the method in protected so it can be inherited. what about the member variable ? does the subclass has 2 variables with the same name and 2 different values ??? or is it just that the method access the member in its class first ?? Can you explain this Lynn? regards,
|
 |
Keith Lynn
Ranch Hand
Joined: Feb 07, 2005
Posts: 2341
|
|
There is a thead about this. http://www.coderanch.com/t/251701/java-programmer-SCJP/certification/Inheritance
|
 |
NS Reddy
Greenhorn
Joined: May 11, 2006
Posts: 9
|
|
class Exam { protected String difficultyLevel="easy"; public void printDifficulty(){ System.out.println(difficultyLevel); } } class SCJPExam extends Exam { private String difficultyLevel="killing"; //public void printDifficulty(){ //System.out.println(difficultyLevel); //} public static void main(String args[]) { SCJPExam myExam=new SCJPExam(); myExam.printDifficulty(); } } Since the subclass SCJPExam did not override the printDifficulty() method super class's printDifficulty() method is invoked and hence prints easy. If the comments are removed in the above program it would print: killing Thanks
|
 |
 |
|
|
subject: Can sumone explain this to me? regarding attributes
|
|
|