• 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:

Var scope when extending a base class

 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the reasoning behind the output? It uses the variable of the base class.

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

Originally posted by Willie Toma:
[B]What is the reasoning behind the output? It uses the variable of the base class.
[/B]


Hello,
This seems to be a very common question that is asked.
As far as I know, the output should be:
99
RType.amethod()
This actually happens because methods are determined at runtime while variables are determined at compile time.
Which means that while the program is compiling, it sets the data of any/all the variables which are defined in the program.
The data of methods however, is determined at runtime, so the compiler sees that RType extends Base, so it uses the data of amethod from the RType class, and not the Base class.
P.S: Be careful of these sort of questions as they are often tricky and misleading, and also you can only define a object of a class in this way if the class is extended; inner classes are different you need to use: outerclass.innerclass obj = new outer().new inner()
Someone please correct me if im wrong.
Kamil.
 
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Static binding (b.i)
Dynamic binding (b.method())
Always remember this
static binding. (Associates to Class of objref)
Dynamic binding (Associates to Class of object referred by objref)
HTH
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic