3

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.

Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
hemanth
  • 109
  • 5

3 Answers3

6

The way you have your method defined, you are calling C.method() from inside B.main(). Since method is private to B, the method is visible inside of B.main() even though the object is of type C which inherits from B.

Justin Niessner
  • 242,243
  • 40
  • 408
  • 536
  • There is no C.method(). Private methods are not inherited. – atamanroman Jul 18 '11 at 15:20
  • 2
    @atamanroman private methods are inherited, they are just not visible. – MK. Jul 18 '11 at 15:22
  • 1
    @atamanroman - Of course private methods are inherited. They're just not visible from outside of the parent class. – Justin Niessner Jul 18 '11 at 15:24
  • 2
    **Private Members in a Superclass** A subclass does not inherit the private members of its parent class. However, if the superclass has public or protected methods for accessing its private fields, these can also be used by the subclass. A nested class has access to all the private members of its enclosing class—both fields and methods. Therefore, a public or protected nested class inherited by a subclass has indirect access to all of the private members of the superclass. (http://download.oracle.com/javase/tutorial/java/IandI/subclasses.html) – atamanroman Jul 18 '11 at 15:24
  • @atamanroman: I don't think that a java ***tutorial*** is a good source in this kind of terminology argument – Armen Tsirunyan Jul 18 '11 at 15:27
  • Well, that was the first reference I found. I had some of the java memory models in mind when I said that private members are not inherited (which were part of SCJP for example). I'd guess that there were eventual name clashes if private member *were* inherited, but you can shadow them without any problems. I'd appreciate any input which states the opposite, however. Also, it's all about scope here. Thats why Armens answer fits the question way better. – atamanroman Jul 18 '11 at 15:32
  • Here is a better source: *Members of a class that are declared private are not inherited by subclasses of that class. Only members of a class that are declared protected or public are inherited by subclasses declared in a package other than the one in which the class is declared.* (http://stackoverflow.com/questions/4716040/does-subclasses-inherit-private-fields, http://java.sun.com/docs/books/jls/third_edition/html/classes.html#8.2) – atamanroman Jul 18 '11 at 15:41
  • @atamanroman - That's certainly interesting. If that truly is the case, then this code shouldn't work at all. If the private member wasn't inherited then ((B)new C()).method(); should fail because it's not there...but obviously it is working and the method exists. – Justin Niessner Jul 18 '11 at 15:46
  • @Justin Niessner -I have understood the concept but when i try the code this following way i should get an error . but i am not getting class C { public static void main(String[] args) { B b = new B(); C c = new C(); b.method(); c.method(); } } class B { private void method() { } } class C extends B { } – hemanth Jul 18 '11 at 15:46
3

The access checking is not done at object/class level, but rather at scope level. You call the method in B's scope where it is accessible. It doesn't matter whether you call it on a C object or a B object.

Armen Tsirunyan
  • 130,161
  • 59
  • 324
  • 434
  • sorry, just a typo -- you had note instead of not, I fixed it on auto-pilot. – MK. Jul 18 '11 at 15:23
  • @MK: No problem, but I had already fixed that and the formatting. – Armen Tsirunyan Jul 18 '11 at 15:29
  • how about this code . even this code is getting compiled class C { public static void main(String[] args) { B b = new B(); C c = new C(); b.method(); c.method(); } } class B { private void method() { } } class C extends B { } – hemanth Jul 18 '11 at 15:31
2

That's because the main method is declared inside class B and has visibility to all B private methods.

When doing c.method(), the IDE knows that C extends B and it knows that main is inside B so it can see the private method (with referring to B).


That's the "register" you'll find on the compiled B class (from Eclipse).

public static void main(java.lang.String[] args);
    new com.example.B [1]
    dup
    invokespecial com.neurologic.example.B() [17]
    astore_1 [b]
    invokespecial com.example.C() [20]
    astore_2 [c]
    aload_1 [b]
    invokespecial com.example.B.method() : void [21]
    aload_2 [c]
    invokespecial com.example.B.method() : void [21]
    return
Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228