Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java

Why must @Override have a "public" access modifier?

Tried reading from:http://stackoverflow.com/questions/9330001/change-the-access-modifier-of-an-overridden-method-in-java

I couldn;t fully grasp what they are, but I think I get the idea. Could someone dumb it down for me?

1 Answer

By using the @Override annotation you are telling the compiler you are overriding the matching method a parent class. This means if the parent's method is public, the subclass's method must also be public. This is because the new overridden method will always be called whenever it is used from an object of the subclass, even if upcast to the the parent class. (i.e. you can't hide a method the parent class is expected to expose for use) You do have some leeway with the access modifier to make it more permissive (like changing from protected in the parent to public in the child)