• Post Reply Bookmark Topic Watch Topic
  • New Topic

protected data members in an interface  RSS feed

 
Ranch Hand
Posts: 30
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why can't we have protected data members in an interface?
 
author & internet detective
Marshal
Posts: 37518
554
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • 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
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
Marshal
Posts: 37518
554
Eclipse IDE Java VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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: 999
5
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!