aspose file tools*
The moose likes Beginning Java and the fly likes Several different classes to extend one, each reusing toString() method from superclass? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Several different classes to extend one, each reusing toString() method from superclass?" Watch "Several different classes to extend one, each reusing toString() method from superclass?" New topic
Author

Several different classes to extend one, each reusing toString() method from superclass?

Russell Bateman
Ranch Hand

Joined: Feb 26, 2008
Posts: 69
I know this is a pretty beginning Java question and if I knew what to call it, I could simply Google for the answer. If I have a class A that's going to be a "subset" of other classes B, C and probably others...

How do I code class B (and C, etc.) such that...

(Please answer the question in the comment above.)

Profuse thanks for taking pity on an old C programmer still too often struggling with the concepts.

Russ
Phil English
Ranch Hand

Joined: Jun 18, 2012
Posts: 62

Russell Bateman wrote:I know this is a pretty beginning Java question and if I knew what to call it, I could simply Google for the answer. If I have a class A that's going to be a "subset" of other classes B, C and probably others...

How do I code class B (and C, etc.) such that...

(Please answer the question in the comment above.)

Profuse thanks for taking pity on an old C programmer still too often struggling with the concepts.

Russ



How about declaring a int printMe variable in the super class. Then each subclass can set whatever value to printMe and resuse the super method?

Note - assuming you want to preserve x and not overwrite that.
Russell Bateman
Ranch Hand

Joined: Feb 26, 2008
Posts: 69
After cracking Eckel's Thinking in Java, I am reminded that all I was looking for was

Sorry about wasting everyone's time.

Thanks.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19720
    
  20

Russell Bateman wrote:

Don't do that. just use sb.append(y). You probably have that + "" to convert y into a String. However, by appending it to the StringBuilder you're already doing that (in a more efficient way).
Russell Bateman
Ranch Hand

Joined: Feb 26, 2008
Posts: 69
(Sorry right, in my actual code, I'm not doing that. Thanks.)
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4642
    
    5

There's the pattern adopted in the JDK Component class hierarchy; java.awt.Component's toString() method looks like this:Subclasses override a protected String paramString() method that returns information relevant to the specific class, sometimes calling into the super implementation and appending to it.


luck, db
There are no new questions, but there may be new answers.
Rajdeep Biswas
Ranch Hand

Joined: Mar 26, 2012
Posts: 186

Darryl Burke wrote:There's the pattern adopted in the JDK Component class hierarchy; java.awt.Component's toString() method looks like this:Subclasses override a protected String paramString() method that returns information relevant to the specific class, sometimes calling into the super implementation and appending to it.

This is a very good implementation, although you can always SB the Strings.


The biggest gamble will be to ask a question whose answer you know in that it will challenge your theory | www.TechAspire.blogspot.in
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Several different classes to extend one, each reusing toString() method from superclass?