This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js and have Paul Jensen on-line!
See this thread for details.
Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Var scope when extending a base class  RSS feed

 
Willie Toma
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the reasoning behind the output? It uses the variable of the base class.

 
Kamil Dada
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • 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.
 
Ragu Sivaraman
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
  • 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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!