1

I have given this question :

public class A {

    private void a() {
        System.out.println("a");
    }

    public static void main(String[] args) {
        A t = new B();
        t.a();
    }
}


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

This prints output: a

I am not very sure of the answer. I know that if you create an child object and pass it to parent reference. It runs functions overridden via A only. But here the function is private, how does this work ???

I know this is not overriding. But how come output is printing "a" ?

  • 3
    You can't override a `private` method. – Andy Turner May 06 '17 at 19:45
  • `a()` is a private function on A and is not inherited. – SomeDude May 06 '17 at 19:46
  • private methods are not inherited – rjsperes May 06 '17 at 19:46
  • you are not really overriding `a()` here, and since its private your `B` object won't be able to call it. – MeknessiHamida May 06 '17 at 19:48
  • I am not saying its overriding at all. I am saying because its not overriding,Why does it print "a" – gaurav sharma May 06 '17 at 19:53
  • I think this is a good question. `t` is an instance of `B`, so why doesn't `B.a()` get called? It probably has something to do with the method dispatch being locked during compile time in this case where the method of the parent class is private (and no overriding on that method can happen). – Mick Mnemonic May 06 '17 at 19:54
  • @MickMnemonic then why i am getting negative votes on it :( – gaurav sharma May 06 '17 at 19:57
  • This smacks of a Java bug – jamesc May 06 '17 at 20:09
  • As explained in the duplicate question, the reason is apparently that the `main` method is within the super class `A`, making the `private` method callable even through the derived class instance. – Mick Mnemonic May 06 '17 at 20:14
  • @MickMnemonic thanks for that link. – gaurav sharma May 06 '17 at 20:15
  • As you have clearly been able to fool Java into accessing a private method I would argue that this is a clearly a bug/unwanted feature with potential security issues. A private method should never be callable outside of the class – jamesc May 06 '17 at 20:20
  • The method is not overriden so the question is not a duplicate of the one linked to by the community – jamesc May 06 '17 at 20:23
  • @jamesc, I agree that this is quite confusing, but the code example in the dupe is equivalent with the one in this question (base class has a private method and deriving class a public method with the same signature). Apparently the behaviour is as defined in the [JLS](http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.12). – Mick Mnemonic May 06 '17 at 20:27
  • @jamesmc It's not a Java bug. It's all working to spec. The code that's calling the method is actually _inside_ the class, not outside, so nothing has been fooled. – Dawood ibn Kareem May 06 '17 at 21:07

1 Answers1

-1

Though both parent and child class has method a(), it is not method overriding as a() in parent class A is private.

private method is not visible outside class, not even in it's child class. Here class B doesn't know that there is a method with the same name exist in parent class.

So when you call a() with reference of class A, it will call the method of class A and not of it's child class B.

If you will change the modifier of method a() in class A to public, protected or default, then you can see the method overriding behaviour. And it will print 'b'

Abhi Andhariya
  • 550
  • 5
  • 15
  • 1
    but the object you have created is of b not A. this means private members of A is accessible via B's object. That does not sound right . – gaurav sharma May 06 '17 at 20:03
  • No. You can not access private members of class A using class B object. In your case, compiler is seeing a() as a method of class A and not of class B. – Abhi Andhariya May 06 '17 at 20:10
  • If you will remove method a() from class B, and then write code : B b = new B(); b.a(); it will not compile. – Abhi Andhariya May 06 '17 at 20:12