4

I have this two classes, Dog and Beagle that extends Dog.

class Dog {
    protected String bark() {return "woof "; }
}
class Beagle extends Dog {
    private String bark() { return "arf "; }
}
class Test {
    public static void main(String args[]) {
        Dog[] dogs = {new Dog(), new Beagle()};
        for(Dog d : dogs)
            System.out.print(d.bark());
    }
}

When I'm using any other editor rather than Eclipse, the above code it does not even compile. I'm getting this error:

Attempting to assign weaker access privileges to bark() method in Beagle class.

You can also see this behavior here.

If I use Eclipse Indigo (Version: 3.7.2), this code compiles fine and the output is: woof woof.

Please make me understand which one is correct and why?

Thanks in advance!

Joan P.
  • 2,368
  • 6
  • 30
  • 63
  • 1
    This code shouldn't pass compilation in eclipse either. Perhaps you are running an earlier version of your code. – Eran Jun 21 '17 at 12:33
  • I get an error message in eclipse too: `Cannot reduce the visibility of the inherited method from Dog`. Clean your Project and rebuild it. I guess than the Problem is not longer reprduceable – Jens Jun 21 '17 at 12:35
  • @Eran I'm using `Version: 3.7.2`. Is this version so old? – Joan P. Jun 21 '17 at 12:36
  • Doesn't compile in Eclipse Neon.2 using Java 1.8.0_102 – JonK Jun 21 '17 at 12:36
  • Eclipse 3.7.2 is very old, 4.6.3 is the current release with 4.7.0 coming by the end of the month. – greg-449 Jun 21 '17 at 12:37
  • Thanks @greg-449 I will update with the newest version. – Joan P. Jun 21 '17 at 12:39

3 Answers3

5

If I use Eclipse, this code compiles fine and the output is: woof woof.

I dont think so, you are not allowed to reduce the visibility the method of bark

it must be

@Override
protected String bark() {
    return "arf ";
}

instead of

@Override
private String bark() {
    return "arf ";
}

after that modification then Begle is correctly overriden the bark method, so the output must be:

woof arf


side note:

Version: Neon.3 Release (4.6.3)

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
1

Other IDE's seeing it as parent method and giving you the correct error message.

Coming to eclipse (that's weird from eclipse side), since there is no @ovveride annotation it is completely treating that as a new method in child, hence no error. If you place @ovveride annotation in child, you see the same error as other IDE's.

It is the same reason that you getting woof woof twice, as it is calling the same method from parent twice as it is assuming you are not calling the new method created from child.

To avoid the confusion it is always recommend that to use the @ovveride annotation while overriding methods.

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
1

You have not written @Override annotation in child class. So compiler is considering it to be a different method rather than overridden method.

While overriding we can increase the visibility but we cannot decrease it. So make your overridden method to be protected or public.

gprathour
  • 14,813
  • 5
  • 66
  • 90