• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question from Roundup game

 
Peixiao Lin
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

---------------------------------------------------
Thanks for reply.
[ October 07, 2004: Message edited by: Barry Gaunt ]
 
Anthony Villanueva
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic