0

In the following program, if msg() in A is declared as public, b.display() in main() calls B's version of msg(). But, if msg() in A is declared as private, b.display() in main() calls A's version of msg(). Could you please tell me why this behaviour?

Thanks in advance.

class A {
    private String msg() {
        return "Message from class A";
    }

    void display() {
        System.out.println(msg());
    }
}

class B extends A {
    public String msg() {
        return "Message from class B's msg()";
    }
}

class Tests {
    public static void main(String args[]) {
        B b = new B();

        // private version of msg defined in A will be called
        // if msg is defined as public in A, the version of msg defined in B is called
        b.display();

        // the version defined in B is called
        System.out.println(b.msg());
    }
}
abgd1712
  • 60
  • 1
  • 4
  • Thank you logan for the reference. So at bottomline, a private method of a class cannot be overridden in a subclass because the subclass has no idea about the private method of the superclass. Instead a new method is created in the subclass. In the case of this program, msg() defined in B is entirely different from msg() defined in A. There is no overriding in this case. – abgd1712 Jun 02 '18 at 07:58
  • Yes! That is exactly what happens. – Logan Jun 02 '18 at 08:02

3 Answers3

1

Because the method of A

private String msg() {
        return "Message from class A";
    }

is private. To override it, it has to be public and on B there should be the @Override notation like this

class B extends A {
    @Override
    public String msg() {
        return "Message from class B's msg()";
    }
}
modmoto
  • 2,901
  • 7
  • 29
  • 54
  • There does not have to be an `@Override` annotation, but including one ensures that the method actually overrides the one in its superclass at compile-time. – Logan Jun 02 '18 at 07:57
  • thanks, i was actually not aware of that, as Intellij always does that for me ;) – modmoto Jun 02 '18 at 07:59
  • No problem! Just remember that method overriding was available when Java was first released. Annotations were not added until Java 5. – Logan Jun 02 '18 at 08:01
1

This is, in short, because you cannot override a private method - Java doesn't allow it.

You can think of all private methods as effectively being marked with the final modifier.

If you want to ensure a method has definitely overridden another one, use the @Override annotation. This will let you know if it hasn't, by throwing an error at compile time.

Michael Berry
  • 70,193
  • 21
  • 157
  • 216
0

Because in java , extended class only have possibility upconversion modifier, not else.For expamle in your code Class A have method private msg() , that extended class make private msg() to public msg , msg() , and protected msg(); In your case you create B b = new B() and call b.display() naturally you call super method (because you have no possibility override private method's) , not extended. And b.msg() called from b because you overridedd it.

Saaanty
  • 195
  • 8