Win a copy of Rust Web Development this week in the Other Languages forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

protected data members in an interface

 
Ranch Hand
Posts: 30
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
why can't we have protected data members in an interface?
 
author & internet detective
Posts: 40913
840
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because interfaces automatically define anything to be public. For data, they are also static and final. For methods, just public.
 
Gautam Bhalla
Ranch Hand
Posts: 30
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeanne Boyarsky wrote:Because interfaces automatically define anything to be public. For data, they are also static and final. For methods, just public.




Hi jeanne,


That was something I wanted to understand,why just public and why cannot protected?

As per context and as per technical feasibilities as well , interfaces are meant to be implemented by sub classes or extended by other interfaces;Any ways, If my members are protected I should still be able to access them in my sub-classes as intended.
 
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Interfaces do not have the same purpose as classes. Interfaces are public contracts. So they've got to have public methods - how else will everyone know what the contract requires..
 
Chan Ag
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

As per context and as per technical feasibilities as well , interfaces are meant to be implemented by sub classes or extended by other interfaces;Any ways, If my members are protected I should still be able to access them in my sub-classes as intended.



Isn't that something that even classes could provide us? Why would Java designers choose to introduce another functionally equivalent construct and make things complex for us.
 
Jeanne Boyarsky
author & internet detective
Posts: 40913
840
Eclipse IDE VI Editor Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's not functionally equivalent. I can have a class implement multiple interfaces and extend only one class. More importantly, interfaces specify the contract as noted above. They are also useful because you can define an interface before any implementing classes exist.
 
Rancher
Posts: 1043
6
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeanne Boyarsky wrote:They are also useful because you can define an interface before any implementing classes exist.



An abstract class can also be defined before any extending non-abstract class exists.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic