Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Weird Eclipse behaviour when tring to override a method [duplicate]

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!

like image 276
Joan P. Avatar asked Mar 09 '23 19:03

Joan P.


1 Answers

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)

like image 54
ΦXocę 웃 Пepeúpa ツ Avatar answered Apr 25 '23 17:04

ΦXocę 웃 Пepeúpa ツ