Weird Eclipse behavior when tring overrides a method

I have two classes: Dogand Beaglethat extend 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 use some other editor, and not Eclipse, it does not even compile this code. I get this error:

An attempt to assign weaker permissions to the bark () method in the 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: woof woof.

Please let me know which one is correct and why?

Thanks in advance!

+4
source share
3 answers

IDE .

( ), @ovveride, , , . @ovveride , , IDE.

, woof woof, , , , .

, @ovveride .

+1

Eclipse, , : woof woof.

,

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

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

, Begle , :

woof arf


:

: Neon.3 Release (4.6.3)

+5

@Override . , .

, . protected public.

+1

Source: https://habr.com/ru/post/1679808/


All Articles