Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > hierarchy of interface/implementations.

Reply
Thread Tools

hierarchy of interface/implementations.

 
 
horos11@gmail.com
Guest
Posts: n/a
 
      03-24-2009
I had a quick question about implementing interfaces and storing those
implementations..

suppose I have a implementation that I defined:

package mycom;

public interface MyInterface
{
public void mymethod();
}

and I put it in file:

mycom/MyInterface.java


and I want to have several different implementations defined for it,
that I want to stick under:

mycom/MyInterface/Implentation1.java
mycom/MyInterface/Implentation2.java
mycom/MyInterface/Implentation3.java

Can you do this? when I go to make my Implementation1, I say:

package mycom.MyInterface;

I get a conflict, apparently with the interface. But this seems the
logical way to structure things. My workaround is to make a directory:

mycom/MyInterfaceImpl

and stuff all implementations there, but this IMO is a hack and I'm
hoping that there are other ways around this (that make sense, camel
case promotes spelling errors, isn't compiler checkable, etc.)

Any ideas?

Ed
 
Reply With Quote
 
 
 
 
Lew
Guest
Posts: n/a
 
      03-24-2009
On Mar 24, 5:29*pm, horo...@gmail.com wrote:
> I had a quick question about implementing interfaces and storing those
> implementations..
>
> suppose I have a implementation that I defined:
>
> package mycom;
>
> public interface MyInterface
> {
> * * *public void mymethod();
>
> }
>
> and I put it in file:
>
> mycom/MyInterface.java
>
> and I want to have several different implementations defined for it,
> that I want to stick under:
>
> mycom/MyInterface/Implentation1.java
> mycom/MyInterface/Implentation2.java
> mycom/MyInterface/Implentation3.java
>
> Can you do this? when I go to make my Implementation1, I say:
>
> package mycom.MyInterface;
>
> I get a conflict, apparently with the interface. But this seems the
> logical way to structure things. My workaround is to make a directory:
>
> mycom/MyInterfaceImpl
>
> and stuff all implementations there, but this IMO is a hack and I'm
> hoping that there are other ways around this (that make sense, camel
> case promotes spelling errors, isn't compiler checkable, etc.)


Drop the upper-case letters to lower-case in the package name. That's
more conventional, and will avoid a conflict with type names, which by
convention must have upper-case letters in them.

Forget directories - think in terms of packages and types, then work
out the directories after the fact.

You have more flexibility with names if you have a two-level domain
followed by functional package names.

One-level domain (per your example):

mycom.MyInterface
mycom.impl.Implentation1
mycom.impl.Implentation2
etc.

"MyInterface", "impl" and "ImplementationN" are far too general and
semantically useless names to really show the power, though.

Two-level domain, e.g., lewscanon.com, for a product known as "Killer
App":

com.lewscanon.killerapp.Potable;
com.lewscanon.killerapp.potable.Water;
com.lewscanon.killerapp.potable.Juice;
com.lewscanon.killerapp.potable.Whiskey;

Another way, more structurally bound to the code:

com.lewscanon.killerapp.potable.Potable;
com.lewscanon.killerapp.potable.impl.Water;
com.lewscanon.killerapp.potable.impl.Juice;
com.lewscanon.killerapp.potable.impl.Whiskey;

And the way I usually prefer:

com.lewscanon.killerapp.potable.Potable;
com.lewscanon.killerapp.potable.Water;
com.lewscanon.killerapp.potable.Juice;
com.lewscanon.killerapp.potable.Whiskey;

--
Lew
 
Reply With Quote
 
 
 
 
Mark Space
Guest
Posts: n/a
 
      03-24-2009
wrote:
>
> mycom/MyInterface.java

...

> mycom/MyInterface/Implentation1.java
> mycom/MyInterface/Implentation2.java
> mycom/MyInterface/Implentation3.java


This SHOULD work. As Eric said, we need to see the actual error message
(and the steps leading up to the error, if you're doing this on the
command line).

Your naming conventions are off, but I can't think of a filesystem off
hand that won't let you name a directory the same as a filename, minus
the extension. They're just two different strings to the filesystem.

You are likely having classpath troubles... post the screen output (or
Ant config, or whatever...) because it's too hard to guess what you are
actually doing incorrectly.
 
Reply With Quote
 
horos11@gmail.com
Guest
Posts: n/a
 
      03-25-2009
On Mar 24, 3:00 pm, "Matt Humphrey" <ma...@iviz.com> wrote:
> <horo...@gmail.com> wrote in message
>
> news:684f647a-c9e1-419c-8299-...>I had a quick question about implementing interfaces and storing those
> > implementations..

>
> > suppose I have a implementation that I defined:

>
> I think you mean interface.
>


yeah, you are right, I am tired..
>
>
> > package mycom;

>
> > public interface MyInterface
> > {
> > public void mymethod();
> > }

>
> > and I put it in file:

>
> > mycom/MyInterface.java

>
> > and I want to have several different implementations defined for it,
> > that I want to stick under:

>
> > mycom/MyInterface/Implentation1.java
> > mycom/MyInterface/Implentation2.java
> > mycom/MyInterface/Implentation3.java

>
> > Can you do this? when I go to make my Implementation1, I say

> You appear to be mixing up the package and class names. The class is not
> itself a package that you can put things under. (You can nest classes
> within another class, but that's an entirely different issue.)



yeah, I sort of gathered that you couldn't do this, but I think that
is more a limitation of java than anything else. You may argue not so
for interfaces, because you can have a class implement many of them
(and hence the 'inheritance' tree is really a bush), but for an
abstract class - or any arbitrary class for that matter - I think it
is a lot clearer to have an inherited class tied to its parent in a
very clear way.

Consider the whole reader design decision in java. I personally think
that

Reader.InputStream

is a lot clearer than

InputStreamReader

because it implicitly shows the relationship between the two classes,
and lends itself to a hierarchy
(like files inside a directory belong to that directory). Likewise,
I'd like to be able to name my class 'File', and hnece say:

Semaphore.File sf = new Semaphore.File()

because I'm using a *type* of Semaphore. I do admit that an abstract
class works better in this case than an interface, but as it is I need
to manually mangle the name to avoid conflict with java's File class.

Anyways, I don't want to make this a big deal, I'll work with it. It's
not like I have any choice.

ed
 
Reply With Quote
 
Joshua Cranmer
Guest
Posts: n/a
 
      03-25-2009
wrote:
> yeah, I sort of gathered that you couldn't do this, but I think that
> is more a limitation of java than anything else. You may argue not so
> for interfaces, because you can have a class implement many of them
> (and hence the 'inheritance' tree is really a bush), but for an
> abstract class - or any arbitrary class for that matter - I think it
> is a lot clearer to have an inherited class tied to its parent in a
> very clear way.


The package structure of Java, namespace structure of C++, and module
structure of Python (among other analogues in other programming
languages) would tend to disagree. It is often desirable to have anyone
extend an interface: for example, java.awt.LayoutManager is a good
example of this. It is implemented more frequently in other packages
than its own java.awt.

Your idea would also work poorly if you have deeper trees. If I want a
JTable, I'd like to be able to say JTable and not
Component.Container.JTable (I'm even skipping JComponent here!). Or how
about Component.Container.Window.Dialog? Most names are already
explanatory of their inheritance: a dialog is obviously a window, a
container, and a component.
>
> Consider the whole reader design decision in java. I personally think
> that
>
> Reader.InputStream
>
> is a lot clearer than
>
> InputStreamReader
>
> because it implicitly shows the relationship between the two classes,
> and lends itself to a hierarchy
> (like files inside a directory belong to that directory).


I disagree. An InputStreamReader is, well, an input stream reader. A
Reader.InputStream is the input stream of a reader (as I read it,
anyways). The use of `.' tends to imply a composition element; for
example, Map.Entry is the entry of a map.

--
Beware of bugs in the above code; I have only proved it correct, not
tried it. -- Donald E. Knuth
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      03-25-2009
On Tue, 24 Mar 2009 14:29:41 -0700 (PDT), wrote,
quoted or indirectly quoted someone who said :

>package mycom.MyInterface;


I would do it like this:

------------------

// E:\com\mycom\horses\Horse.java
package com.mycom.horses;

interface Horse { .... }

------------

// E:\com\mycom\horses\Palomino.java
package com.mycom.horses;

public class Palomino implements Horse { ... }

------------

// E:\com\mycom\horses\Arabian.java
package com.mycom.horses;

public class Arabian implements Horse { ... }


Note how package names are all lower case, and begin com.

see http://mindprod.com/jgloss/package.html
--
Roedy Green Canadian Mind Products
http://mindprod.com

"Don’t worry about the world coming to an end today.
It is already tomorrow in Australia."
~ Charles Schulz
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      03-25-2009
On Tue, 24 Mar 2009 14:29:41 -0700 (PDT), wrote,
quoted or indirectly quoted someone who said :


>and I want to have several different implementations defined for it,
>that I want to stick under:
>
>mycom/MyInterface/Implentation1.java
>mycom/MyInterface/Implentation2.java
>mycom/MyInterface/Implentation3.java
>
>Can you do this?


see http://mindprod.com/jgloss/package.html#EXAMPLE
--
Roedy Green Canadian Mind Products
http://mindprod.com

"Don’t worry about the world coming to an end today.
It is already tomorrow in Australia."
~ Charles Schulz
 
Reply With Quote
 
alexandre_paterson@yahoo.fr
Guest
Posts: n/a
 
      03-25-2009
On Mar 24, 9:29 pm, horo...@gmail.com wrote:
> I had a quick question about implementing interfaces and storing those
> implementations..
>
> suppose I have a implementation that I defined:
>
> package mycom;
>
> public interface MyInterface
> {
> public void mymethod();
>
> }


Hi,

it is *strongly* discouraged to write "public void mymethod();" in
your interface. You can simply write "void mymethod();".

From the definitive authority on the subject, the JLS, which
one in this case cannot argue with:

http://java.sun.com/docs/books/jls/t...nterfaces.html

"It is permitted, but strongly discouraged as a matter of style,
"to redundantly specify the public modifier for interface methods.

Alex
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      03-25-2009
wrote:
> yeah, I sort of gathered that you couldn't do this, but I think that
> is more a limitation of java [sic] than anything else. You may argue not so
> for interfaces, because you can have a class implement many of them
> (and hence the 'inheritance' tree is really a bush), but for an
> abstract class - or any arbitrary class for that matter - I think it
> is a lot clearer to have an inherited class tied to its parent in a
> very clear way.


Java packages are namespaces and not hierarchical.

> Consider the whole reader design decision in java. I personally think
> that
>
> Reader.InputStream
>
> is a lot clearer than
>
> InputStreamReader
>
> because it implicitly shows the relationship between the two classes,


InputStream dose not depend on Reader, though Readers depend on InputStreams.
The notation 'Reader.InputStream' implies that 'InputStream' is nested
within 'Reader', which is not true.

> and lends itself to a hierarchy


Packages are not hierarchical.

> (like files inside a directory belong to that directory).
> Likewise, I'd like to be able to name my class 'File', and hnece say:


You can most certainly name your class 'File'.

> Semaphore.File sf = new Semaphore.File()
> because I'm using a *type* of Semaphore.


Well, that isn't Java. It also exposes implementation of the Semaphore, which
in OO is a Bad Thing. You're also stealing the syntax of nested classes.

> I do admit that an abstract class works better in this case than an interface,


Does it? Why?

> but as it is I need
> to manually mangle the name to avoid conflict with java's [sic] File class.


No, you don't. Use the fully-qualified names.

> Anyways, I don't want to make this a big deal, I'll work with it. It's
> not like I have any choice.


Sure you do. You can use interfaces to hide implementation details instead of
revealing them. You can use packages as they are designed, as
non-hierarchical namespaces. You can name your class 'File' if you wish. If
Java really doesn't do what you want, you can use a different language.

--
Lew
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      03-25-2009
Lew wrote:
> wrote:
>> it is *strongly* discouraged to write "public void mymethod();" in
>> your interface. You can simply write "void mymethod();".

>
> I strongly encourage keeping the 'public' in there.
>
>> From the definitive authority on the subject, the JLS, which
>> one in this case cannot argue with:
>>
>> http://java.sun.com/docs/books/jls/t...nterfaces.html
>>
>> "It is permitted, but strongly discouraged as a matter of style,
>> "to redundantly specify the public modifier for interface methods.

>
> You're right, one cannot argue that it is permitted.


"... cannot argue with ..."

--
Lew
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Block Diagram Viewer / Hierarchy Parser for VHDL/Verilog? jjohnson@cs.ucf.edu VHDL 5 07-21-2005 07:11 AM
Hierarchy in Schematic-VHDL Design ALuPin VHDL 1 04-01-2005 02:23 PM
SystemC + VHDL cosim, hierarchy probing, etc... jjohnson@cs.ucf.edu VHDL 2 12-20-2004 05:05 PM
HDL Hierarchy Manager 1.2.1 Announcement Alan VHDL 0 10-02-2003 11:47 AM
Re: hierarchy chart Alan Pretre ASP .Net 0 07-25-2003 02:51 PM



Advertisments