Consider the following code
class B {
int i=100;
void amethod1() {
System.out.println("I am in B's amethod");
}
class C extends B {
int i=200;
void amethod1() {
System.out.println("I am in C's amethod");
}
public static void main(
String args[]) {
B b=new C();
System.out.println(b.i);
b.amethod();
}
}
when the programmed is compiled and run it gives me an o/p of
100 and i am in C's amethod
further if i modify the code a bit and include another method called amethod2() in class B and donot override it in C
and call it from class C it shows that I am in B;s amethod 2 fine
but when i do override it then it starts showing I am in C's amethod2
my question when we refer to a variable present in both theclass it will pick the value thats in parent class as b.i=100
but when we call a method it picks the method from the current class
but when we add another method and do not override it executes the code of the method thats in parent class why is this happening
can anybody expalin it to me
regards
amit
------------------
coffe drinker