0
class parent
{
    public void disp(int i)
    {
        System.out.println("int");
    }
}

class child extends parent
{
    private void disp(long l)
    {
        System.out.println("long");
    }
}

class impl
{
    public static void main(String... args)
    {
        child c = new child();
        c.disp(0l); //Line 1
    }
}

Compiler complains as below

inh6.java:27: error: disp(long) has private access in child
c.disp(0l);

The input given is 0L and I am trying to overload the disp() method in child class.

4

1 に答える 1

4

The method disp() is declared as private

private void disp(long l){System.out.println("long");}

As such, it is only visible within the child class, not from the impl class where you are trying to call it. Either change its visibility to public or rethink your design.

If your question is why is it seeing the disp(long) instead of disp(int), this is because you provided a long primitive value to the method call.

 c.disp(0l); // the 'l' at the end means 'long'

The official tutorial for access modifiers is here.

于 2013-08-31T16:28:30.423 に答える