• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question

 
Hans
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1)Question
class Super
{ int index = 5;
public void printVal()
{ System.out.println( "Super" );
}
}
class Sub extends Super
{ int index = 2;
public void printVal()
{ System.out.println( "Sub" );
}
}
public class Runner
{ public static void main( String argv[] )
{ Super sup = new Sub();
System.out.print( sup.index + "," );
sup.printVal();
}
}
The result is (5,Sub).Why is not (2,Sub)?Who know reason.
Thank you.
2)Question
The finally statment execution finish .The rest of the method countinue to run.(True or False)?
 
chetan nain
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
two points:
1. overridden method called depends on the class denoted by the reference during runtime.
2. shadowed member variable called depends on the type of the reference .
retry your analysis
for your second answer, the data is insufficient to warrant an answer. for example, if an uncaught exception occured, rest of method would be skipped
hth, chetan
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic