• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How Interface is different from Abstract Class incase of Inheritence

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank a lot for your quick reply
 
I've been selected to go to the moon! All thanks to this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic