Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

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: 75690
354
  • 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: 75690
354
  • 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
 
There were millions of the little blood suckers. But thanks to this tiny ad, I wasn't bitten once.
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic