aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Can sumone explain this to me? regarding attributes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Can sumone explain this to me? regarding attributes" Watch "Can sumone explain this to me? regarding attributes" New topic
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: 2367
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: 2367
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can sumone explain this to me? regarding attributes