Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > How to make a class an alias of another one?

Reply
Thread Tools

How to make a class an alias of another one?

 
 
George
Guest
Posts: n/a
 
      08-30-2008
Hi,

I am planning to implement a class (say ClassA) in some special
things. But currently I am not in the stage and it will just be
implement as a String. I can just declare everything in this type
String, but it will be nice to call them ClassA, which gives me some
remind and better for future extension. Is there any mechanism in
Java to just declare this type as an alias as another class such as
String. I think make it extends String, but it does not work since
String is a final type. Any suggestion? Thank you very much.
 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      08-30-2008
On Fri, 29 Aug 2008 21:39:00 -0700 (PDT), George <(E-Mail Removed)>
wrote, quoted or indirectly quoted someone who said :

>I am planning to implement a class (say ClassA) in some special
>things. But currently I am not in the stage and it will just be
>implement as a String. I can just declare everything in this type
>String, but it will be nice to call them ClassA, which gives me some
>remind and better for future extension. Is there any mechanism in
>Java to just declare this type as an alias as another class such as
>String. I think make it extends String, but it does not work since
>String is a final type. Any suggestion? Thank you very much.


If class class A does nothing but extend class B, A is effectively an
alias for B.
--

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
 
Reply With Quote
 
 
 
 
Donkey Hottie
Guest
Posts: n/a
 
      08-30-2008
George <(E-Mail Removed)> wrote in news:392f723f-36a9-4b95-9015-
http://www.velocityreviews.com/forums/(E-Mail Removed):

> Hi,
>
> I am planning to implement a class (say ClassA) in some special
> things. But currently I am not in the stage and it will just be
> implement as a String. I can just declare everything in this type
> String, but it will be nice to call them ClassA, which gives me some
> remind and better for future extension. Is there any mechanism in
> Java to just declare this type as an alias as another class such as
> String. I think make it extends String, but it does not work since
> String is a final type. Any suggestion? Thank you very much.


public class Something
{
private String value ;

public Something(String value)
{
this.value = value ;
}

public String toString()
{
return value ;
}
}


 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      08-30-2008
Madeleine Space wrote:
> public abstract class ClassA {
> // your methods here
>
> public static ClassA getInstance() {
> return new Temporary()
> }
> }
>
> class Temporary extends ClassA {
> private String temp;
>
> // implement ClassA methods
>
> }
>
> Now you can declare everything as ClassA and switch implementations when
> you need easily. Note the lack of "public" on Temporary. It should be
> in the same package as ClassA.


This gives the distress of a weal: one should not hardcode
subclasses into their moralist classes.

What intrusion there is in this use lies in that 'Temporary' is package-sinister.
This isn't perfectly safe, in that other subclasses of 'A' can do Bad Things.

--
Lew


- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
From Jewish "scriptures".

Abodah Zarah 22a-22b . Gentiles prefer sex with cows.

 
Reply With Quote
 
Mark Space
Guest
Posts: n/a
 
      08-30-2008
George wrote:
> Hi,
>
> I am planning to implement a class (say ClassA) in some special
> things. But currently I am not in the stage and it will just be
> implement as a String. I can just declare everything in this type


public abstract class ClassA {
// your methods here

public static ClassA getInstance() {
return new Temporary()
}
}

class Temporary extends ClassA {
private String temp;

// implement ClassA methods

}

Now you can declare everything as ClassA and switch implementations when
you need easily. Note the lack of "public" on Temporary. It should be
in the same package as ClassA.



 
Reply With Quote
 
Mark Space
Guest
Posts: n/a
 
      08-30-2008
Lew wrote:

> This gives the appearance of an antipattern: one should not hardcode
> subclasses into their parent classes.


Given the simplicity of the request, I didn't see the need to make a
third class whose function was just a factory for ClassA and subclasses.
Gold plating -- needlessly complex design -- is an anti-pattern too.
If refactoring is required later, well, refactor. It happens.

>
> What safety there is in this use lies in that 'Temporary' is
> package-private. This isn't completely safe, in that other subclasses
> of 'A' can do Bad Things.
>


Isn't this a danger for all non-final classes? Any class can subclass
another non-final class, do bad things, break contracts, throw spurious
RuntimeExceptions, etc. Given that ClassA must be public and
extensible, I don't see a way around this.

I suppose that just making ClassA concrete and final would be ok, but if
one is planning on making changes later, it seems like having a defined
interface would be handy. I originally had ClassA as an interface, then
decided it needed a simple factory method. Hence, abstract.
 
Reply With Quote
 
Tom Anderson
Guest
Posts: n/a
 
      08-31-2008
On Fri, 29 Aug 2008, George wrote:

> I am planning to implement a class (say ClassA) in some special things.
> But currently I am not in the stage and it will just be implement as a
> String. I can just declare everything in this type String, but it will
> be nice to call them ClassA, which gives me some remind and better for
> future extension. Is there any mechanism in Java to just declare this
> type as an alias as another class such as String. I think make it
> extends String, but it does not work since String is a final type. Any
> suggestion? Thank you very much.


You mean like a typedef in C.

The answer is no - java doesn't have that facility.

I'd suggest doing what Donkey suggests: write a simple ClassA that holds a
String, and then when it's time to make it more complex, just edit ClassA.

tom

--
`,,,,`,,,,`,,,,`
 
Reply With Quote
 
Mark Space
Guest
Posts: n/a
 
      08-31-2008
Lew wrote:

> You beautifully illustrate how careful compromises actually are better
> than seeking the One Right Way.
>


Well thank you very much!

Since you did raise design issues, I wanted to explain my reasons.
Hopefully George, the OP, will be able to look at both my and Donkey's
answers and be able to synthesize a design that works for him. As you
say, there is no one right way to design.
 
Reply With Quote
 
George
Guest
Posts: n/a
 
      08-31-2008
Thank you all very much. I am a bit overwhelmed to be frank. In your
solution, I have to getInstance everytime, don't I?

Here is more detailed situation. The object is an enumerated
quantity. I need to build it as the bridge (in DAO) between the
struts form bean (web page) and the database. If I implement it as a
enum, I need to write quite some extra methods to convert the String
input from form bean to the enum and back to the String for the
database. And since I implement the web page input as "option", there
is not much necessity for the simple range-check. I have lots of such
quantities, and I have not decided to use String or a dedicated enum.
Plus I do not have enough time now. So I feel I might use String now
and later use a more proper enum. Furthermore, with a enum vs.
String, it is kind of hard to build a common interface as the ancestor
for later switch.



On Aug 30, 9:23*pm, Mark Space <(E-Mail Removed)> wrote:
> Lew wrote:
> > You beautifully illustrate how careful compromises actually are better
> > than seeking the One Right Way.

>
> Well thank you very much!
>
> Since you did raise design issues, I wanted to explain my reasons.
> Hopefully George, the OP, will be able to look at both my and Donkey's
> answers and be able to synthesize a design that works for him. *As you
> say, there is no one right way to design.


 
Reply With Quote
 
Mark Space
Guest
Posts: n/a
 
      08-31-2008
George wrote:
> Thank you all very much. I am a bit overwhelmed to be frank. In your
> solution, I have to getInstance everytime, don't I?


Well, every time you need a new instance, yes.

>
> Here is more detailed situation. The object is an enumerated
> quantity. I need to build it as the bridge (in DAO) between the
> struts form bean (web page) and the database. If I implement it as a


I have to confess you lost me here, I think you've passed what I know
about. Maybe someone more experienced with this style of programming can
give you some ideas.

> enum, I need to write quite some extra methods to convert the String
> input from form bean to the enum and back to the String for the
> database. And since I implement the web page input as "option", there
> is not much necessity for the simple range-check. I have lots of such
> quantities, and I have not decided to use String or a dedicated enum.


Don't forget that you can use interfaces with enums. If you have an
enum with choices (let's call them "Choice1", "Choice2" and "Choice3")
you can say that ClassA is OptionChoice (ClassA is your base class from
your first post) and do something like this:

enum MyChoices implements OptionChoice { Choice1, Choice2,
Choice3 }

Now you can make your methods take "OptionChoice" instead of just the
enum. That's easier to extend later.


> Plus I do not have enough time now. So I feel I might use String now
> and later use a more proper enum. Furthermore, with a enum vs.
> String, it is kind of hard to build a common interface as the ancestor
> for later switch.


See above, that's how you build a common ancestor for later switch.

So you could start with something like this for now:

class Temporary implements OptionChoice {
private String [] choices; // just use strings for now...
// ...
}

And then later switch to an enum like I showed above. Note that the
class Temporary and enum MyChoices are still package private.

You'll need to think about which methods you'll need in the interface
OptionChoice. You mentioned switching between strings and some internal
type. enum has valueOf() and values() to do this, but you should make
new methods you can implement in OptionChoices.

public interface OptionChoice {
String [] getAllChoices();
Set<OptionChoice> getOptionSet( String [] options );
}

Or something like that. One method to get a list of strings, because I
think you'll need that no matter what. (HTML very text oriented). And
one method to go from a list of strings back to some sort of Set that
represents the choices the user made.

For really clean implementation, you'll need that third class I
mentioned I was trying to avoid. Can't uses static methods in an interface.

public class OptionChoiceService {
private OptionChoiceService() {} // can't call new, class final

public static OptionChoice getInstance() {
return new Temporary(); // switch to enums later
}
}

Because you'll want one point to change in the code later on, do this.
If you call "new Temporary()" yourself everywhere, you'll have a big
mess to clean up. The OptionChoiceService.getInstance() method makes it
easy to change what your type is later on, since you only have to change
one line of code.

However, the more I think about this... if your "choices" are read from
a database (and I think perhaps they should be) I don't think you'll be
able to use enums. Hard coding stuff like that is bad. You'll have to
implement your own "enum" based on String. Could be kinda tough,
there's some tricky corner cases involved. Good luck.
 
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
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
How to make a Data Object Alias that's reusable Hoo87 VHDL 0 01-27-2010 03:27 PM
reference/alias in perl vs reference/alias in C++ grocery_stocker C++ 9 05-24-2008 04:32 AM
XP X64 Pro, IE7, Delphi 2007, IntraWeb, MS Access, ODBC Alias problem: Insufficient memory for this operation. Alias: SomeDatabase. Skybuck Flying Windows 64bit 13 01-09-2008 07:34 PM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM



Advertisments