• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How Interface is different from Abstract Class incase of Inheritence

 
jayaprakash kaluva
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49796
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49796
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have told you already why you don’t have protected members in interfaces.
 
jayaprakash kaluva
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank a lot for your quick reply
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic