*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Var scope when extending a base class 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 "Var scope when extending a base class" Watch "Var scope when extending a base class" New topic
Author

Var scope when extending a base class

Willie Toma
Ranch Hand

Joined: May 11, 2001
Posts: 78
What is the reasoning behind the output? It uses the variable of the base class.

Kamil Dada
Ranch Hand

Joined: Aug 07, 2001
Posts: 43
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

Joined: Jul 20, 2001
Posts: 464
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Var scope when extending a base class