• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Question from Roundup game

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

---------------------------------------------------
Thanks for reply.
[ October 07, 2004: Message edited by: Barry Gaunt ]
 
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic