It's not a secret anymore!
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Polymorphic method invocations 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 "Polymorphic method invocations" Watch "Polymorphic method invocations" New topic

Polymorphic method invocations

sura watthana
Ranch Hand

Joined: Sep 13, 2004
Posts: 77
I need an explanation.
Let's look at the code below before the question is to be asked.

i know that
Polymorphic method invocations apply only to instance methods. You can always refer to an object with a more general reference variable type (a superclass or interface), but at runtime, the ONLY things that are dynamically selected based on the actual object (rather than the reference type) are instance methods. Not static methods. Not variables. Only overridden instance methods are dynamically invoked based on the real object's type.

problem is I don't get it.
question is why can't variable be called when it comes to

I thought it'd make sense to do so.
Could anyone give me an explanation to this.


Michael Ku
Ranch Hand

Joined: Apr 20, 2002
Posts: 510
Your properties have been declared with no scope so they have default scope. Lession1 cannot access these properties.
ahmed yehia
Ranch Hand

Joined: Apr 22, 2006
Posts: 424
Variables 'gs' and 'pp' have default access. Only classes in the same package can access them.
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274

I don't see your problem. Since you didn't have a package statement, all this classes go into the default package. Hence all fields are visible.

Your classes compile without an error and print out the values as expected.


all events occur in real time
Michael Ku
Ranch Hand

Joined: Apr 20, 2002
Posts: 510
Let's ask the source.

sura, are the classes all in the same package? are they all in the default package?
I agree. Here's the link:
subject: Polymorphic method invocations
It's not a secret anymore!