Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Interface and Implementin class

 
meng zhou
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, there,
A legal nonabstract immplementing class must has implemented the methods defined in the interface it implements.
According to the override rule, all methods implemented must be public as well. So could it be safe to say that the methods defined in an interface are at least be accessed by any class of the same package?
 
Vicken Karaoghlanian
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All the methods in an interface are implicitly public abstract, thus the overriding method can't be more restrictive than public.
In other words: Any method that overrides an interface must have the access modifier of public.
Since all methods are declared public in an interface, then any class can access these methods regardless of the package it is located in.

So could it be safe to say that the methods defined in an interface are at least be accessed by any class of the same package?

I would say: "Methods defined in an interface are accessible to any class disregarding the package they are located in."
 
meng zhou
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
THnaks for your anwser, but not fully agree.
Quoted as you said : "Methods defined in an interface are accessible to any class disregarding the package they are located in."
Quoted as K&B (p128): "if a class can not be accessed, its members can not be accessed".
My thought:
1. first of all, methods are declared in the interface, but only defined in the implementing class.
2. Second, if we restrict the access level of the implementing class to default, then all these implemented methods (public though ) can only be accessed by classes of the same package.
 
meng zhou
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
K&B Pabge Page 115 "The public modifier is required if you want the interface to have the public rather than default access."
 
Vicken Karaoghlanian
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hmmm, interesting... I had this funny idea in my head that top level interfaces are implicitly public, apparently i was wrong :roll: and it turns out that top level interfaces can indeed be default. Thanks for making me realize that.
Now back to your thoughts...

first of all, methods are declared in the interface, but only defined in the implementing class.

Your statement is correct, however you should note that a class that implements an interface have the option not to implement all the methods in that interface, provided it is declared abstract.

Second, if we restrict the access level of the implementing class to default, then all these implemented methods (public though) can only be accessed by classes of the same package.

As you adequately put: "if a class can not be accessed, its members can not be accessed"

Sorry for the confusion I caused.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic