This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Polymorphic method invocations

 
sura watthana
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
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.


Thanks

Sura
 
Michael Ku
Ranch Hand
Posts: 510
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your properties have been declared with no scope so they have default scope. Lession1 cannot access these properties.
 
ahmed yehia
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Variables 'gs' and 'pp' have default access. Only classes in the same package can access them.
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howdy,

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.


Yours,
Bu.
 
Michael Ku
Ranch Hand
Posts: 510
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let's ask the source.

sura, are the classes all in the same package? are they all in the default package?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic