This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
I don’t like the term parent class; say superclass. I know people will disagree with me, however.
You cannot override fields, only instance methods. If you try creating fields in a class which have the same name as fields in superclasses or interfaces, you end up with two fields. I think it is called hiding; look at this FAQ.
Rule of thumb: make all fields private except those used as constants; those should be public static final.
Rule of thumb: never try to use a field in a subclass. Use public (or protected) methods which use those fields.
Rule of thumb: you can override instance methods. But read Joshua Bloch’s Effective Java or another good book before you try it.
This is what such a public method might look like:-By using the superclass field and its public method you get any access to i that you need.
Joined: Oct 13, 2005
I have told you already why you don’t have protected members in interfaces.
Joined: Aug 01, 2012
i did not understand what you explained regarding "why cant we have protected fields in interface ".If possibe can you please explain more eloberately .