Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

question

take the following code :

public class A {
public void foo(){System.out.println("A");}
//change here

public void goo(){foo();}
}

public class B extends A {
public void foo(){System.out.println("B");}
}

public class Test {
public static void main(String[] args) {
B b = new B();
b.goo();
}
}

the output is :
B

but , if we change in class A the word public to private (where it says "change here) to
private void foo(){System.out.println("A");}

then the output is :
A

Why is that ??
[627 byte] By [yyatuv] at [2007-11-11 6:49:58]
# 1 Re: question
The B class' inherited goo method will then use the A foo method since this method is private
and isn't overridden by the B class implementation.
sjalle at 2007-11-11 22:40:06 >