Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why is the result?

 
david_chen
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
code:
class Base {
int i=99;
public void amethod(){
System.out.println("Base.amethod()");
}
}
public class RType extends Base{
int i=-1;
public static void main(String argv[]){
Base b = new RType();//<= Note the type
System.out.println(b.i);
b.amethod();
}
public void amethod(){
System.out.println("RType.amethod()");
}
}

the irght answer is :
99
RType.amethod()
why not is :
-1
RType.amethod()
or
99
Base.amethod()
please help me!!
 
anand raman
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
david this question has been discussed endlessly even in a post around 2 days back. Please check the archives for "HOW VARIABLES ARE DECIDED" posted by ZEESHAN AZIZ on June 24.
Hope this helps
Anand
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic