Home arrow static arrow Java Programming [Archive] - package private methods in package private Interface
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - package private methods in package private Interface
8 Duke Stars available
This topic has 19 replies on 2 pages.    « Previous | 1 | 2 |

Posts:36
Registered: 2/28/02
Re: package private methods in package private Interface  
Jun 20, 2004 11:54 PM (reply 15 of 19)



 
I then would like to differ the Objects
by lets say Persistable which means that these
objects
can physically be stored somewhere. I do not want to
expose this attribute and I want to add a method
save(). Even if I choose the interface to be
package-private the save() Method would have to be
public although I do not want to expose it.

Yep, that's one problem with implementing an interface
... check out the JavaDoc for
JComboBox.actionPerformed() to see an example of
undesired exposure (one that could've been hidden with
a little work).

An alternative work-around is to make your Persistable
interface a marker interface (it has no methods), and
use reflection to invoke the expected save() method.
It means you don't get compile-time type safety,
someone could implement Persistable but not provide
save(), but it's better IMO than throwing a class into
the hierarchy.

I haven't checked the JCP, but it might be worthwhile
throwing this request in if it's not already there, or
giving it a vote if it is.

I checked a nested package private interface. It defaults to public as well (as stated in the Java lang spec). In JCP, I did not find a request alike. Maybe I should try it. Re. being reflection coding in the wild: I personally define never interfaces with methods not declared being public. Many colleagues around thought that package private methods in interface would be possible because you can declare them without accesibility modifier. I believe it is good habit to declare interface methods explicitly to being public.

 

Posts:1,695
Registered: 1/13/04
Re: package private methods in package private Interface  
Jun 21, 2004 12:35 AM (reply 16 of 19)



 
Perhaps the use of the protected keyword would be of some benefit in your case ?
 

Posts:6,487
Registered: 5/5/04
Re: package private methods in package private Interface  
Jun 21, 2004 9:57 AM (reply 17 of 19)



 
An interface makesassumptions about modifers as you can only certain possiblities are allowed.
You cannot define package private methods because the compiler just assumes the remaining modifiers.
e.g.
interface Interface {    int VAR1 = 1;    static int VAR2 = 1;    final int VAR3 = 1;    public int VAR4 = 1;    public static final int VAR5 = 1;     void method1();    abstract void dmethod2();    public void method3();    public abstract void method4();}

These variables and methods have equivilent modifiers..

If you want a package private interface with package private member you need to use an abstract class
abstract class MyClass {    int VAR1 = 1;    static int VAR2 = 1;    final int VAR3 = 1;    public int VAR4 = 1;    public static final int VAR5 = 1;     void method1();    abstract void dmethod2();    public void method3();    public abstract void method4();}

In this case, only the variables declared static are static, only the final variables are final and only the public methods and variables are notionally 'public' (Actually the class is package private so the methods cannot be accessed publicly in this example)
 

Posts:6,750
Registered: 1/25/04
Re: package private methods in package private Interface  
Jun 21, 2004 10:09 AM (reply 18 of 19)



 
If you want a package private interface with package
private member you need to use an abstract class

But that isn't an interface, it's a class. They're not interchangeable.
 

Posts:3,258
Registered: 00-08-28
Re: package private methods in package private Interface  
Jun 21, 2004 12:32 PM (reply 19 of 19)



 
There are two complementary roles of inheritance in OOP application.

1. Specialisation: Extending the functionality of an existing class.

2. Generalisation: Sharing commonality between two or more classes.

So adding functionality by extending what exists at each level to create more specialized classes. This would create a hierarchy and would be helpful even later on in maintenance.

Now when to have an abstarct class and when an interface. An interface just defines the basic functionality to be implemented by all the subclasses, though the way they are implemented may be different. e.g an interface animal can have all those functionalities common to all animals in basics but different in implementation. e.g all animals walk however some walk on two legs and some on four. All animals can see but they might have a different colour spectrum visible. So all such methods general but common can be in an interface.

Abstract classes come into play at a more defined level e.g in an abstract class human though walk would be the same so implemented there and then but talking can be left abstract since there may be different languages involved.

So in short not interchangeable
 
This topic has 19 replies on 2 pages.    « Previous | 1 | 2 |