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.

1 Answer 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.

Sign up to request clarification or add additional context in comments.

1 Comment

"not from the impl class where you are trying to call "...Thanks for pointing this :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.