I'm having difficulty figuring out how to overwrite parts of the “toString” methods in superclasses...
I have a superclass "Vessel" and a subclass "Bottle". The subclass "Bottle" further has two subclasses "GlasBottle" and "PlasticBottle".
I want the following output:
This vessel has a volume of 1
This bottle has a volume of 1 and contains juice
This green glas bottle has a volume of 2 and contains juice
This white glas bottle has a volume of 1 and contains beer
This OTHER bottle has a volume of 2 and contains cola
This PET bottle has a volume of 1 and contains milk
However, I'm having problems overriding the different toString methods in each class.
This is what I get as my output:
This vessel has a volume of 1
This bottle This vessel has a volume of 1 and contains juice
This green glas bottle This bottle This vessel has a volume of 2 and contains
juice
This white glas bottle This bottle This vessel has a volume of 1 and contains beer
This OTHER bottle This bottle This vessel has a volume of 2 and contains cola
This PET bottle This bottle This vessel has a volume of 1 and contains milk
As you can see from the second output, it inserts "This vessel" since that string is part of the "toString" method that I'm calling, so it makes sense that it's there. However, I want java to omit that part of the superclass when I call the super.toString()
and only include the other necessary parts. Here are my superclass and subclasses:
public class Vessel {
private int volume=0;
public Vessel(int volume) {
this.volume = volume;
}
public String toString() {
return "This vessel has a volume of "+volume;
}
}
public class Bottle extends Vessel {
private String content="";
public Bottle(int volume, String content) {
super(volume);
this.setContent(content);
}
public String getContent(String content) {
return this.content = content;
}
public void setContent(String content) {
this.content = content;
}
public String toString() {
return "This bottle "+super.toString()+" and contains "+content;
}
}
public class GlasBottle extends Bottle {
private String color="";
public GlasBottle(int volume, String content, String color) {
super(volume, content);
this.setColor(color);
}
public String getColor(String color) {
return this.color = color;
}
public void setColor(String color) {
this.color = color;
}
public String toString() {
return "This "+color+" glas bottle "+super.toString();
}
}
public class PlasticBottle extends Bottle {
private String material="";
public PlasticBottle(int volume, String content, String material) {
super(volume, content);
if(material != "PET") {
this.material = "OTHER";
}else {
this.setMaterial(material);
}
}
public String getMaterial(String material) {
return this.material = material;
}
public void setMaterial(String material) {
this.material = material;
}
public String toString() {
return "This "+material+" bottle "+super.toString();
}
}
java
add a comment |
I have a superclass "Vessel" and a subclass "Bottle". The subclass "Bottle" further has two subclasses "GlasBottle" and "PlasticBottle".
I want the following output:
This vessel has a volume of 1
This bottle has a volume of 1 and contains juice
This green glas bottle has a volume of 2 and contains juice
This white glas bottle has a volume of 1 and contains beer
This OTHER bottle has a volume of 2 and contains cola
This PET bottle has a volume of 1 and contains milk
However, I'm having problems overriding the different toString methods in each class.
This is what I get as my output:
This vessel has a volume of 1
This bottle This vessel has a volume of 1 and contains juice
This green glas bottle This bottle This vessel has a volume of 2 and contains
juice
This white glas bottle This bottle This vessel has a volume of 1 and contains beer
This OTHER bottle This bottle This vessel has a volume of 2 and contains cola
This PET bottle This bottle This vessel has a volume of 1 and contains milk
As you can see from the second output, it inserts "This vessel" since that string is part of the "toString" method that I'm calling, so it makes sense that it's there. However, I want java to omit that part of the superclass when I call the super.toString()
and only include the other necessary parts. Here are my superclass and subclasses:
public class Vessel {
private int volume=0;
public Vessel(int volume) {
this.volume = volume;
}
public String toString() {
return "This vessel has a volume of "+volume;
}
}
public class Bottle extends Vessel {
private String content="";
public Bottle(int volume, String content) {
super(volume);
this.setContent(content);
}
public String getContent(String content) {
return this.content = content;
}
public void setContent(String content) {
this.content = content;
}
public String toString() {
return "This bottle "+super.toString()+" and contains "+content;
}
}
public class GlasBottle extends Bottle {
private String color="";
public GlasBottle(int volume, String content, String color) {
super(volume, content);
this.setColor(color);
}
public String getColor(String color) {
return this.color = color;
}
public void setColor(String color) {
this.color = color;
}
public String toString() {
return "This "+color+" glas bottle "+super.toString();
}
}
public class PlasticBottle extends Bottle {
private String material="";
public PlasticBottle(int volume, String content, String material) {
super(volume, content);
if(material != "PET") {
this.material = "OTHER";
}else {
this.setMaterial(material);
}
}
public String getMaterial(String material) {
return this.material = material;
}
public void setMaterial(String material) {
this.material = material;
}
public String toString() {
return "This "+material+" bottle "+super.toString();
}
}
java
What you want to do is have fields that contain the name of the object, like String thisName = "bottle". Then in your toString you just need to do return "This" + thisName + "whatever";
– PhaseRush
Nov 21 '18 at 20:10
add a comment |
I have a superclass "Vessel" and a subclass "Bottle". The subclass "Bottle" further has two subclasses "GlasBottle" and "PlasticBottle".
I want the following output:
This vessel has a volume of 1
This bottle has a volume of 1 and contains juice
This green glas bottle has a volume of 2 and contains juice
This white glas bottle has a volume of 1 and contains beer
This OTHER bottle has a volume of 2 and contains cola
This PET bottle has a volume of 1 and contains milk
However, I'm having problems overriding the different toString methods in each class.
This is what I get as my output:
This vessel has a volume of 1
This bottle This vessel has a volume of 1 and contains juice
This green glas bottle This bottle This vessel has a volume of 2 and contains
juice
This white glas bottle This bottle This vessel has a volume of 1 and contains beer
This OTHER bottle This bottle This vessel has a volume of 2 and contains cola
This PET bottle This bottle This vessel has a volume of 1 and contains milk
As you can see from the second output, it inserts "This vessel" since that string is part of the "toString" method that I'm calling, so it makes sense that it's there. However, I want java to omit that part of the superclass when I call the super.toString()
and only include the other necessary parts. Here are my superclass and subclasses:
public class Vessel {
private int volume=0;
public Vessel(int volume) {
this.volume = volume;
}
public String toString() {
return "This vessel has a volume of "+volume;
}
}
public class Bottle extends Vessel {
private String content="";
public Bottle(int volume, String content) {
super(volume);
this.setContent(content);
}
public String getContent(String content) {
return this.content = content;
}
public void setContent(String content) {
this.content = content;
}
public String toString() {
return "This bottle "+super.toString()+" and contains "+content;
}
}
public class GlasBottle extends Bottle {
private String color="";
public GlasBottle(int volume, String content, String color) {
super(volume, content);
this.setColor(color);
}
public String getColor(String color) {
return this.color = color;
}
public void setColor(String color) {
this.color = color;
}
public String toString() {
return "This "+color+" glas bottle "+super.toString();
}
}
public class PlasticBottle extends Bottle {
private String material="";
public PlasticBottle(int volume, String content, String material) {
super(volume, content);
if(material != "PET") {
this.material = "OTHER";
}else {
this.setMaterial(material);
}
}
public String getMaterial(String material) {
return this.material = material;
}
public void setMaterial(String material) {
this.material = material;
}
public String toString() {
return "This "+material+" bottle "+super.toString();
}
}
java
I have a superclass "Vessel" and a subclass "Bottle". The subclass "Bottle" further has two subclasses "GlasBottle" and "PlasticBottle".
I want the following output:
This vessel has a volume of 1
This bottle has a volume of 1 and contains juice
This green glas bottle has a volume of 2 and contains juice
This white glas bottle has a volume of 1 and contains beer
This OTHER bottle has a volume of 2 and contains cola
This PET bottle has a volume of 1 and contains milk
However, I'm having problems overriding the different toString methods in each class.
This is what I get as my output:
This vessel has a volume of 1
This bottle This vessel has a volume of 1 and contains juice
This green glas bottle This bottle This vessel has a volume of 2 and contains
juice
This white glas bottle This bottle This vessel has a volume of 1 and contains beer
This OTHER bottle This bottle This vessel has a volume of 2 and contains cola
This PET bottle This bottle This vessel has a volume of 1 and contains milk
As you can see from the second output, it inserts "This vessel" since that string is part of the "toString" method that I'm calling, so it makes sense that it's there. However, I want java to omit that part of the superclass when I call the super.toString()
and only include the other necessary parts. Here are my superclass and subclasses:
public class Vessel {
private int volume=0;
public Vessel(int volume) {
this.volume = volume;
}
public String toString() {
return "This vessel has a volume of "+volume;
}
}
public class Bottle extends Vessel {
private String content="";
public Bottle(int volume, String content) {
super(volume);
this.setContent(content);
}
public String getContent(String content) {
return this.content = content;
}
public void setContent(String content) {
this.content = content;
}
public String toString() {
return "This bottle "+super.toString()+" and contains "+content;
}
}
public class GlasBottle extends Bottle {
private String color="";
public GlasBottle(int volume, String content, String color) {
super(volume, content);
this.setColor(color);
}
public String getColor(String color) {
return this.color = color;
}
public void setColor(String color) {
this.color = color;
}
public String toString() {
return "This "+color+" glas bottle "+super.toString();
}
}
public class PlasticBottle extends Bottle {
private String material="";
public PlasticBottle(int volume, String content, String material) {
super(volume, content);
if(material != "PET") {
this.material = "OTHER";
}else {
this.setMaterial(material);
}
}
public String getMaterial(String material) {
return this.material = material;
}
public void setMaterial(String material) {
this.material = material;
}
public String toString() {
return "This "+material+" bottle "+super.toString();
}
}
java
java
asked Nov 21 '18 at 20:09
WoeIsWoeIs
510111
510111
What you want to do is have fields that contain the name of the object, like String thisName = "bottle". Then in your toString you just need to do return "This" + thisName + "whatever";
– PhaseRush
Nov 21 '18 at 20:10
add a comment |
What you want to do is have fields that contain the name of the object, like String thisName = "bottle". Then in your toString you just need to do return "This" + thisName + "whatever";
– PhaseRush
Nov 21 '18 at 20:10
What you want to do is have fields that contain the name of the object, like String thisName = "bottle". Then in your toString you just need to do return "This" + thisName + "whatever";
– PhaseRush
Nov 21 '18 at 20:10
What you want to do is have fields that contain the name of the object, like String thisName = "bottle". Then in your toString you just need to do return "This" + thisName + "whatever";
– PhaseRush
Nov 21 '18 at 20:10
add a comment |
4 Answers
4
active
oldest
votes
What you want is 'overriding' not overwriting.
First of all add a getVolume()
method to your top level Vessel
class, since your volume
is private
.
You can simply put the parts that change as separate methods.
So in Bottle
you put a method like this:
protected String getBottleType() {
return "bottle";
}
and change your toString()
like this:
public String toString() {
return "This " + getBottleType() + " has a volume of " + getVolume() + " and contains " + content;
}
Then in each subclass of Bottle
you just override getBottleType()
. For example in GlassBottle
you just do:
@Override
protected String getBottleType() {
return color + " glass bottle";
}
You can then actually leave out the toString()
of the sub classes unless you wish to change the sentence.
add a comment |
I suppose that you would avoid duplication by using the volume information of the toString()
parent class :
public String toString() {
return "This vessel has a volume of "+volume;
}
But it will not give the expected result in this way :
public String toString() {
return "This bottle "+super.toString()+" and contains "+content;
}
It will just mix the current class and the parent class information in the built String.
What you want in the subclass is :
public String toString() {
return "This bottle has a volume of " + getVolume() + " and contains "+content;
}
In this case you should introduce a getter to access to volume
from the subclasses.
Note that to avoid duplication and keep the volume
field private
you could also write something like in the subclass :
public String toString() {
return "This bottle" + toStringVolume() " and contains " + content;
}
And define in the parent class :
public final String toStringVolume(){
return "has a volume of " + volume;
}
But is it really valuable ?
It creates some indirection in the reading for not a great value.
@WoeIs add a public getter forvolume
– Andrew Tobilko
Nov 21 '18 at 20:17
add a comment |
I want java to omit that part of the superclass when I call the
super.toString()
and only include the other necessary parts.
You understand that super.toString()
returns the whole string as you defined. "the other necessary parts" is the thing only you are aware of. You could parse super.toString()
to extract the parts you need, but it's tedious and folly.
A reasonable way is to access parent's properties by public getters and construct a unique String
for each subclass ignoring how the parent represented itself.
add a comment |
I think the simplest thing to do is as follows:
Remove the toString methods from GlasBottle and PlasticBottle, introduce the notion of a 'vesselQualifier' (e.g. color, material), and rewrite the toString methods in Vessel and Bottle as shown below, which obtain the paired down classname from the corresponding sub-class at runtime. I've removed all of the other unrelated code for the sake of brevity.
public class Vessel {
protected String vesselQualifier() {
return null;
}
public String toString() {
String classNameParts = this.getClass().getCanonicalName().split("\.");
return "This "
+(vesselQualifier()==null?"":(vesselQualifier()+" "))
+classNameParts[classNameParts.length-1]+" has a volume of " + volume;
}
}
public static class Bottle extends Vessel {
public String toString() {
return super.toString() + " and contains " + content;
}
}
public static class GlasBottle extends Bottle {
@Override
protected String vesselQualifier() {
return color;
}
}
public static class PlasticBottle extends Bottle {
@Override
protected String vesselQualifier() {
return material;
}
}
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53419766%2fim-having-difficulty-figuring-out-how-to-overwrite-parts-of-the-tostring-meth%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
What you want is 'overriding' not overwriting.
First of all add a getVolume()
method to your top level Vessel
class, since your volume
is private
.
You can simply put the parts that change as separate methods.
So in Bottle
you put a method like this:
protected String getBottleType() {
return "bottle";
}
and change your toString()
like this:
public String toString() {
return "This " + getBottleType() + " has a volume of " + getVolume() + " and contains " + content;
}
Then in each subclass of Bottle
you just override getBottleType()
. For example in GlassBottle
you just do:
@Override
protected String getBottleType() {
return color + " glass bottle";
}
You can then actually leave out the toString()
of the sub classes unless you wish to change the sentence.
add a comment |
What you want is 'overriding' not overwriting.
First of all add a getVolume()
method to your top level Vessel
class, since your volume
is private
.
You can simply put the parts that change as separate methods.
So in Bottle
you put a method like this:
protected String getBottleType() {
return "bottle";
}
and change your toString()
like this:
public String toString() {
return "This " + getBottleType() + " has a volume of " + getVolume() + " and contains " + content;
}
Then in each subclass of Bottle
you just override getBottleType()
. For example in GlassBottle
you just do:
@Override
protected String getBottleType() {
return color + " glass bottle";
}
You can then actually leave out the toString()
of the sub classes unless you wish to change the sentence.
add a comment |
What you want is 'overriding' not overwriting.
First of all add a getVolume()
method to your top level Vessel
class, since your volume
is private
.
You can simply put the parts that change as separate methods.
So in Bottle
you put a method like this:
protected String getBottleType() {
return "bottle";
}
and change your toString()
like this:
public String toString() {
return "This " + getBottleType() + " has a volume of " + getVolume() + " and contains " + content;
}
Then in each subclass of Bottle
you just override getBottleType()
. For example in GlassBottle
you just do:
@Override
protected String getBottleType() {
return color + " glass bottle";
}
You can then actually leave out the toString()
of the sub classes unless you wish to change the sentence.
What you want is 'overriding' not overwriting.
First of all add a getVolume()
method to your top level Vessel
class, since your volume
is private
.
You can simply put the parts that change as separate methods.
So in Bottle
you put a method like this:
protected String getBottleType() {
return "bottle";
}
and change your toString()
like this:
public String toString() {
return "This " + getBottleType() + " has a volume of " + getVolume() + " and contains " + content;
}
Then in each subclass of Bottle
you just override getBottleType()
. For example in GlassBottle
you just do:
@Override
protected String getBottleType() {
return color + " glass bottle";
}
You can then actually leave out the toString()
of the sub classes unless you wish to change the sentence.
edited Nov 21 '18 at 21:12
answered Nov 21 '18 at 20:19
jbxjbx
12k1060113
12k1060113
add a comment |
add a comment |
I suppose that you would avoid duplication by using the volume information of the toString()
parent class :
public String toString() {
return "This vessel has a volume of "+volume;
}
But it will not give the expected result in this way :
public String toString() {
return "This bottle "+super.toString()+" and contains "+content;
}
It will just mix the current class and the parent class information in the built String.
What you want in the subclass is :
public String toString() {
return "This bottle has a volume of " + getVolume() + " and contains "+content;
}
In this case you should introduce a getter to access to volume
from the subclasses.
Note that to avoid duplication and keep the volume
field private
you could also write something like in the subclass :
public String toString() {
return "This bottle" + toStringVolume() " and contains " + content;
}
And define in the parent class :
public final String toStringVolume(){
return "has a volume of " + volume;
}
But is it really valuable ?
It creates some indirection in the reading for not a great value.
@WoeIs add a public getter forvolume
– Andrew Tobilko
Nov 21 '18 at 20:17
add a comment |
I suppose that you would avoid duplication by using the volume information of the toString()
parent class :
public String toString() {
return "This vessel has a volume of "+volume;
}
But it will not give the expected result in this way :
public String toString() {
return "This bottle "+super.toString()+" and contains "+content;
}
It will just mix the current class and the parent class information in the built String.
What you want in the subclass is :
public String toString() {
return "This bottle has a volume of " + getVolume() + " and contains "+content;
}
In this case you should introduce a getter to access to volume
from the subclasses.
Note that to avoid duplication and keep the volume
field private
you could also write something like in the subclass :
public String toString() {
return "This bottle" + toStringVolume() " and contains " + content;
}
And define in the parent class :
public final String toStringVolume(){
return "has a volume of " + volume;
}
But is it really valuable ?
It creates some indirection in the reading for not a great value.
@WoeIs add a public getter forvolume
– Andrew Tobilko
Nov 21 '18 at 20:17
add a comment |
I suppose that you would avoid duplication by using the volume information of the toString()
parent class :
public String toString() {
return "This vessel has a volume of "+volume;
}
But it will not give the expected result in this way :
public String toString() {
return "This bottle "+super.toString()+" and contains "+content;
}
It will just mix the current class and the parent class information in the built String.
What you want in the subclass is :
public String toString() {
return "This bottle has a volume of " + getVolume() + " and contains "+content;
}
In this case you should introduce a getter to access to volume
from the subclasses.
Note that to avoid duplication and keep the volume
field private
you could also write something like in the subclass :
public String toString() {
return "This bottle" + toStringVolume() " and contains " + content;
}
And define in the parent class :
public final String toStringVolume(){
return "has a volume of " + volume;
}
But is it really valuable ?
It creates some indirection in the reading for not a great value.
I suppose that you would avoid duplication by using the volume information of the toString()
parent class :
public String toString() {
return "This vessel has a volume of "+volume;
}
But it will not give the expected result in this way :
public String toString() {
return "This bottle "+super.toString()+" and contains "+content;
}
It will just mix the current class and the parent class information in the built String.
What you want in the subclass is :
public String toString() {
return "This bottle has a volume of " + getVolume() + " and contains "+content;
}
In this case you should introduce a getter to access to volume
from the subclasses.
Note that to avoid duplication and keep the volume
field private
you could also write something like in the subclass :
public String toString() {
return "This bottle" + toStringVolume() " and contains " + content;
}
And define in the parent class :
public final String toStringVolume(){
return "has a volume of " + volume;
}
But is it really valuable ?
It creates some indirection in the reading for not a great value.
edited Nov 21 '18 at 20:17
answered Nov 21 '18 at 20:13
davidxxxdavidxxx
69k675100
69k675100
@WoeIs add a public getter forvolume
– Andrew Tobilko
Nov 21 '18 at 20:17
add a comment |
@WoeIs add a public getter forvolume
– Andrew Tobilko
Nov 21 '18 at 20:17
@WoeIs add a public getter for
volume
– Andrew Tobilko
Nov 21 '18 at 20:17
@WoeIs add a public getter for
volume
– Andrew Tobilko
Nov 21 '18 at 20:17
add a comment |
I want java to omit that part of the superclass when I call the
super.toString()
and only include the other necessary parts.
You understand that super.toString()
returns the whole string as you defined. "the other necessary parts" is the thing only you are aware of. You could parse super.toString()
to extract the parts you need, but it's tedious and folly.
A reasonable way is to access parent's properties by public getters and construct a unique String
for each subclass ignoring how the parent represented itself.
add a comment |
I want java to omit that part of the superclass when I call the
super.toString()
and only include the other necessary parts.
You understand that super.toString()
returns the whole string as you defined. "the other necessary parts" is the thing only you are aware of. You could parse super.toString()
to extract the parts you need, but it's tedious and folly.
A reasonable way is to access parent's properties by public getters and construct a unique String
for each subclass ignoring how the parent represented itself.
add a comment |
I want java to omit that part of the superclass when I call the
super.toString()
and only include the other necessary parts.
You understand that super.toString()
returns the whole string as you defined. "the other necessary parts" is the thing only you are aware of. You could parse super.toString()
to extract the parts you need, but it's tedious and folly.
A reasonable way is to access parent's properties by public getters and construct a unique String
for each subclass ignoring how the parent represented itself.
I want java to omit that part of the superclass when I call the
super.toString()
and only include the other necessary parts.
You understand that super.toString()
returns the whole string as you defined. "the other necessary parts" is the thing only you are aware of. You could parse super.toString()
to extract the parts you need, but it's tedious and folly.
A reasonable way is to access parent's properties by public getters and construct a unique String
for each subclass ignoring how the parent represented itself.
edited Nov 21 '18 at 20:27
answered Nov 21 '18 at 20:21
Andrew TobilkoAndrew Tobilko
28.5k104589
28.5k104589
add a comment |
add a comment |
I think the simplest thing to do is as follows:
Remove the toString methods from GlasBottle and PlasticBottle, introduce the notion of a 'vesselQualifier' (e.g. color, material), and rewrite the toString methods in Vessel and Bottle as shown below, which obtain the paired down classname from the corresponding sub-class at runtime. I've removed all of the other unrelated code for the sake of brevity.
public class Vessel {
protected String vesselQualifier() {
return null;
}
public String toString() {
String classNameParts = this.getClass().getCanonicalName().split("\.");
return "This "
+(vesselQualifier()==null?"":(vesselQualifier()+" "))
+classNameParts[classNameParts.length-1]+" has a volume of " + volume;
}
}
public static class Bottle extends Vessel {
public String toString() {
return super.toString() + " and contains " + content;
}
}
public static class GlasBottle extends Bottle {
@Override
protected String vesselQualifier() {
return color;
}
}
public static class PlasticBottle extends Bottle {
@Override
protected String vesselQualifier() {
return material;
}
}
add a comment |
I think the simplest thing to do is as follows:
Remove the toString methods from GlasBottle and PlasticBottle, introduce the notion of a 'vesselQualifier' (e.g. color, material), and rewrite the toString methods in Vessel and Bottle as shown below, which obtain the paired down classname from the corresponding sub-class at runtime. I've removed all of the other unrelated code for the sake of brevity.
public class Vessel {
protected String vesselQualifier() {
return null;
}
public String toString() {
String classNameParts = this.getClass().getCanonicalName().split("\.");
return "This "
+(vesselQualifier()==null?"":(vesselQualifier()+" "))
+classNameParts[classNameParts.length-1]+" has a volume of " + volume;
}
}
public static class Bottle extends Vessel {
public String toString() {
return super.toString() + " and contains " + content;
}
}
public static class GlasBottle extends Bottle {
@Override
protected String vesselQualifier() {
return color;
}
}
public static class PlasticBottle extends Bottle {
@Override
protected String vesselQualifier() {
return material;
}
}
add a comment |
I think the simplest thing to do is as follows:
Remove the toString methods from GlasBottle and PlasticBottle, introduce the notion of a 'vesselQualifier' (e.g. color, material), and rewrite the toString methods in Vessel and Bottle as shown below, which obtain the paired down classname from the corresponding sub-class at runtime. I've removed all of the other unrelated code for the sake of brevity.
public class Vessel {
protected String vesselQualifier() {
return null;
}
public String toString() {
String classNameParts = this.getClass().getCanonicalName().split("\.");
return "This "
+(vesselQualifier()==null?"":(vesselQualifier()+" "))
+classNameParts[classNameParts.length-1]+" has a volume of " + volume;
}
}
public static class Bottle extends Vessel {
public String toString() {
return super.toString() + " and contains " + content;
}
}
public static class GlasBottle extends Bottle {
@Override
protected String vesselQualifier() {
return color;
}
}
public static class PlasticBottle extends Bottle {
@Override
protected String vesselQualifier() {
return material;
}
}
I think the simplest thing to do is as follows:
Remove the toString methods from GlasBottle and PlasticBottle, introduce the notion of a 'vesselQualifier' (e.g. color, material), and rewrite the toString methods in Vessel and Bottle as shown below, which obtain the paired down classname from the corresponding sub-class at runtime. I've removed all of the other unrelated code for the sake of brevity.
public class Vessel {
protected String vesselQualifier() {
return null;
}
public String toString() {
String classNameParts = this.getClass().getCanonicalName().split("\.");
return "This "
+(vesselQualifier()==null?"":(vesselQualifier()+" "))
+classNameParts[classNameParts.length-1]+" has a volume of " + volume;
}
}
public static class Bottle extends Vessel {
public String toString() {
return super.toString() + " and contains " + content;
}
}
public static class GlasBottle extends Bottle {
@Override
protected String vesselQualifier() {
return color;
}
}
public static class PlasticBottle extends Bottle {
@Override
protected String vesselQualifier() {
return material;
}
}
answered Nov 21 '18 at 20:59
Tom DrakeTom Drake
43749
43749
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53419766%2fim-having-difficulty-figuring-out-how-to-overwrite-parts-of-the-tostring-meth%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What you want to do is have fields that contain the name of the object, like String thisName = "bottle". Then in your toString you just need to do return "This" + thisName + "whatever";
– PhaseRush
Nov 21 '18 at 20:10