Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Russell Bateman
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 62
MySQL Database Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20669
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(Sorry right, in my actual code, I'm not doing that. Thanks.)
 
Darryl Burke
Bartender
Posts: 5148
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rajdeep Biswas
Ranch Hand
Posts: 224
1
Eclipse IDE Java Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic