File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question from Roundup game 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 "Question from Roundup game" Watch "Question from Roundup game" New topic

Question from Roundup game

Peixiao Lin

Joined: Oct 04, 2004
Posts: 28

Thanks for reply.
[ October 07, 2004: Message edited by: Barry Gaunt ]
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
It helps to read your post if you enclose it in code tags.

Since an scjp object is also an instance of the exam class, each scjp object inherits the public print() method. But this inherited method can only "see" the member x of the parent class, namely x="easy". Try variants of this code by overriding print() in scjp, or changing access modifiers.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
When a.print() is executed, the context of the method is the base class part of the object. So the value of x is "easy".

If you define a print method in class scjp then the context of the print method will be the scjp class. Then "killing" will be printed.

Compile the classes, and play with the application by moving the print method from base class to derived class. See what happens when you define print in both classes. Change the type of the variable a to exam. What happens?

Fifteen minutes play will teach you a lot about this topic.

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
I agree. Here's the link:
subject: Question from Roundup game
It's not a secret anymore!