This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Interface and Implementin class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Interface and Implementin class " Watch "Interface and Implementin class " New topic
Author

Interface and Implementin class

meng zhou
Greenhorn

Joined: Jan 16, 2004
Posts: 12
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

Joined: Jul 21, 2003
Posts: 522
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."


- Do not try and bend the spoon. That's impossible. Instead, only try to realize the truth. - What truth? - That there is no spoon!!!
meng zhou
Greenhorn

Joined: Jan 16, 2004
Posts: 12
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

Joined: Jan 16, 2004
Posts: 12
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

Joined: Jul 21, 2003
Posts: 522
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.
 
GeeCON Prague 2014
 
subject: Interface and Implementin class