| Author |
How Interface is different from Abstract Class incase of Inheritence
|
jayaprakash kaluva
Greenhorn
Joined: Aug 01, 2012
Posts: 4
|
|
Sub class of a Parent class will inheret all the public and protected members .will an implementing class of an interface inheret the members of Interface ?
My doubt is why can't we declare protected members in interface ?
if a class which is implementing an interface inherets the fields ,we can declare the protected fields and we can acess them.
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32694
|
|
Welcome to the Ranch (again)
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.
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32694
|
|
|
I have told you already why you don’t have protected members in interfaces.
|
 |
jayaprakash kaluva
Greenhorn
Joined: Aug 01, 2012
Posts: 4
|
|
|
i did not understand what you explained regarding "why cant we have protected fields in interface ".If possibe can you please explain more eloberately .
|
 |
jayaprakash kaluva
Greenhorn
Joined: Aug 01, 2012
Posts: 4
|
|
|
Thank a lot for your quick reply
|
 |
 |
|
|
subject: How Interface is different from Abstract Class incase of Inheritence
|
|
|