Scope

Read the [Java program]. Pay particular attention to show(), its formal parameters, the calls of show(), and their actual parameters. Predict what the program does.

stack objects
by new( ) class (objects)
 
^
|
show
superObj
subObj
---> ?
--->#4
main
superObj
subObj
--->#3
--->#4
 
 
#0:
Test
main( )
show( )
 
--->code0
--->code1

#3:
superObj
class=SuperClass
memberVar=...
 
--->#1
 
 
#1:
SuperClass
Super
staticVar=...
method( )
method2( )
 
--->Object
 
--->code2
--->code3

#4:
subObj
class=SubClass
memberVar=...
(shadowed!)
memberVar=...
 
--->#2
 
 
 
#2:
SubClass
Super
staticVar=...
method( )
(overrides)
method2( )
--->#1
 
--->code4
 
--->code3

After making your predictions, run the program and look at the output. Were your predictions correct?

main -- static
code0:...
  SuperClass.new() -- #1.new()
  SubClass.new()   -- #2.new()
  ...
  show(#3, #4) -- i.e. code1
  show(#4, #4) -- i.e. code1
  ...
  return
show(p0, p1) -- static
code1:...
  p0.method(p0)
  -- i.e. p0.class.method(p0)
  p0.method2(p0)
  -- i.e. p0.class.method2(p0)
  ...
  p1.method(p1)
  -- i.e. p1.class.method(p1)
  p1.method2(p1)
  -- i.e. p1.class.method2(p1)
  ...
  return

method[p](this) -- in SuperClass
code2:...
  ...
  return
method2(this) -- in SuperClass
code3:...
  ...memberVar...
  -- this.memberVar[p]
  ...
  method(this)
  -- ie this.class.method(this)
  ...
  return

method[b](this) -- in SubClass
code4:...
  ...
  return
(method2(this) -- ^above^, inherited by SubClass)
-- LA 26/10/2004