Confusing calling method in Java

Posted by vBx on Stack Overflow See other posts from Stack Overflow or by vBx
Published on 2011-03-14T00:06:18Z Indexed on 2011/03/14 0:10 UTC
Read the original article Hit count: 101

Filed under:
class Parent
{
    private void method1()
    {
        System.out.println("Parent's method1()");
    }
    public void method2()
    {
        System.out.println("Parent's method2()");
        method1();
    }
}

class Child extends Parent
{
    public void method1()
    {
        System.out.println("Child's method1()");        
    }
}
class test {
    public static void main(String args[])
    {
        Parent p = new Child();
        p.method2();
    }
}

I'm confuse why does in Parent::method2() when invoking method1() it will cal Parents method1() and not Childs method1 ? I see that this happens only when method1() is private? Can someone explain me why ?
Thanks you.

© Stack Overflow or respective owner

Related posts about java