Click to See Complete Forum and Search --> : OO Design - what to overload?


nanode
01-19-2001, 12:25 PM
I have a class that has a Color property (which is another class). A method exists that returns this color:

public Color getColor()

One of the uses of this method is for a property window to display the value of the color in string representation (Object.toString()).

By default the string representation looks like this:
javax.swing.plaf.ColorUIResource[r=0,g=0,b=0]

I'd like to say black or at least [r=0,g=0,b=0].

My only idea is to subclass Color and overload the .toString() method to return a truncated or converted string. I then would need to overload the getColor() method in my main class to return the subclassed Color.

Do I really need to do all this?

YaRness
01-19-2001, 01:17 PM
i'm not much on OO stuff, but if you use this a lot, then it sounds pretty good. i'm assuming that sometime somewhere a program NEEDS that full length string... otherwise i'd say edit the class so it only returns a shorter version (if that's an option).

if you are only using it once, i'd say just parse it inline.

now if you definitely want it to return "black" or "mauve" or whatever, then overloading or some kind of inheritance thingy would prolly be a sound and useful idea.

------------------
"Assembly of Japanese bicycle require great peace of mind."
Registered Linux User #188285 http://counter.li.org/

Stuka
01-19-2001, 01:23 PM
Two questions: 1) Is the Color class yours, or a platform/library type class? (Sorry, not very familiar w/Java yet http://www.linuxnewbie.org/ubb/smile.gif) 2) Will you ever want to use the standard/current toString() method, or just the new one?
If Color is not your class, then I'd suggest subclassing and either overriding or overloading the toString() method (depending on the answer to #2). If Color is yours, then just overload the toString() method, with a toString(TEXT) or something like that.

nanode
01-19-2001, 01:24 PM
The only concern I have with editing the String inline someplace, is that many other objects may display that string. If I can make all the changes within the Color class, then everything *should* be transparent to the observing objects.

Object.toString() is simply the name that is displayed when the object is cast to a String. The color would still be a color, it's display name would just change.

TheLinuxDuck
01-19-2001, 03:25 PM
Originally posted by nanode:
Object.toString() is simply the name that is displayed when the object is cast to a String. The color would still be a color, it's display name would just change.

(In my limited java experience) I've used the toString function alot. It's very handy, as far as I can tell. I am assuming the color class is your own creation? I don't see why using toString() would cause any troubles. It sounds like using it to display the color information would be beneficial in the long run.

But, I don't have a lot of java cuh-xperience, so maybe I'm full of hooey. http://www.linuxnewbie.org/ubb/smile.gif

------------------
TheLinuxDuck
I have a belly button.
:wq

Dru Lee Parsec
01-19-2001, 05:13 PM
The Color class is a standar Java class from(I think) the java.awt.library.

You may want to either overload Color adn simply add a new method that returns what you need. OR, if you don't use this alternative text very often then you can use the getRed(), getGreen(), and getBlue() methods.

Now, those methods return an int. I know nanode knows this but for the rest of the Java newbies a quick and dirty way to change an int to a string is this:

int x = 5;
String s = "" + x;

if you looked at s it would be the string "5"; Java automatically knows how to concat strings, so if you concat a null string (which is the "" with no spce between the quotes) with a numeric value then it will automatically change into a string.

This is why you can do things like this so easily:

int x = 5;
String s = "Danny will be " + x + " years old today.";
and it will work without throwing class cast exceptions.

By the way. It warms my heart to see so many of you discussing Java intelligently. Good job guys. http://www.linuxnewbie.org/ubb/smile.gif

nanode
01-19-2001, 05:36 PM
I ultimately subclassed the standard Color class and overloaded toString():


public String toString() {
String s = new String(); //super.toString();
s = "[R=" + getRed() + ",G=" + getGreen() + ",B=" + getBlue() + "]";
return(s);
}


Then the class that implements this, substitutes the new color class for the standard color when it is constructed:


super.setForeground(new SmartColor(getForeground().getRGB()));


From this point on, any objects that call getForeground() will get my SmartColor class - and when toString() is called on that, it will return my R,G,B stuff.

Dru Lee:

Thanks for the encouragement - not everyone appreciates this stuff.

TheLinuxDuck
01-19-2001, 05:36 PM
Originally posted by Dru Lee Parsec:
int x = 5;
String s = "" + x;

I knew that you could do stuff like that for printing ints, but I hadn't thought of it for creating a string from it.. qool. http://www.linuxnewbie.org/ubb/wink.gif

[quote]By the way. It warms my heart to see so many of you discussing Java intelligently. Good job guys. http://www.linuxnewbie.org/ubb/smile.gif

Heck, Mr Dru, it's you who got me into Java. And, I don't think this comment is really directed at me anyway... http://www.linuxnewbie.org/ubb/biggrin.gif http://www.linuxnewbie.org/ubb/biggrin.gif

I haven't really done much with Java today.. but, I didn't finish the Hunt the Wumpus, though.. http://www.linuxnewbie.org/ubb/smile.gif

------------------
TheLinuxDuck
I have a belly button.
:wq

Stuka
01-19-2001, 06:50 PM
Who's discussing Java intelligently? I'm just talkin' about OO design, and flaunting my ignorance of Java! http://www.linuxnewbie.org/ubb/smile.gif I learned OOD in C++, but fortunately OO is OO, just differs in implementation details. I do plan to learn Java ASAP, but school may interfere w/that (or it could help - my Data Structures instructor accepts Java or C, but no C++ http://www.linuxnewbie.org/ubb/frown.gif). And I do plan on finishing Hunt the Wumpus, even if I have to do it on Windows (I managed to hose my Linux install http://www.linuxnewbie.org/ubb/frown.gif http://www.linuxnewbie.org/ubb/frown.gif).