i have tried the following code in net beans i am expecting error but i didn’t get any error
class B {
private void method() {
}
public static void main() {
B b = new B();
B c = new C();
b.method();
c.method();
}
}
class C extends B {
}
When c.method() tries to access the method it should show error but in NetBeans it is not showing. Please tell me what is the fault.
The way you have your method defined, you are calling
C.method()from insideB.main(). Since method is private toB, the method is visible inside of B.main() even though the object is of typeCwhich inherits fromB.