• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Interfaces that access members of a class

 
Tiberius Marius
Ranch Hand
Posts: 115
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found this phrase in a book and it makes me think that i did not understand everything important related to interfaces :

The members of a class defined using the protected access modifier are accessible to
■Classes and interfaces defined in the same package
■All derived classes, even if they’re defined in separate packages


A member of a class (defined using protected modifier) can be accessible to an interface for what ? Does that happen ? (i mean if the user defines a variables with the same name ) in both the class and the interface ?!
Interfaces contain only abstract methods and constants .How can they be affected by members of other classes apart from identifier conflicts ?
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35279
384
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think they are referencing a scenario like this:



The interface in the same package and refers to a variable with protected access.
 
Sergej Smoljanov
Ranch Hand
Posts: 468
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

it was interesting for me that you can initialize b even by creation object. And was not surprize that interface once initialize not change its constant variable value.
There is no problem with name of variable because in place where you use it you make full name.
and if you declare constant in interface you will hide it in class that implement this interface if you declare same name variable in class

there is no restriction you can change type or value - it will be new variable that hide declared in interface.
and you can use protected metod for intitialization interface variable

 
Sergej Smoljanov
Ranch Hand
Posts: 468
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and one more for name in interface
It is possible for an interface to inherit more than one field with the same name.
Such a situation does not in itself cause a compile-time error. However, any attempt
within the body of the interface to refer to any such field by its simple name will
result in a compile-time error, because such a reference is ambiguous.

this was not your question but was near.

_______
you may interest when initialize will happen (example in next msg):
A class or interface type T will be initialized immediately before the first occurrence
of any one of the following:

• T is a class and an instance of T is created.
• T is a class and a static method declared by T is invoked.
• A static field declared by T is assigned.
• A static field declared by T is used and the field is not a constant variable
(§4.12.4).

• T is a top level class (§7.6), and an assert statement (§14.10) lexically nested
within T (§8.1.3) is executed.
A reference to a static field (§8.3.1.1) causes initialization of only the class or
interface that actually declares it, even though it might be referred to through the
name of a subclass, a subinterface, or a class that implements an interface.

 
Sergej Smoljanov
Ranch Hand
Posts: 468
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
example for last

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic