I'm having difficulty figuring out how to overwrite parts of the “toString” methods in superclasses...












1















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();
}

}









share|improve this question























  • 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
















1















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();
}

}









share|improve this question























  • 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














1












1








1








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();
}

}









share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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



















  • 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












4 Answers
4






active

oldest

votes


















1














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.






share|improve this answer

































    1














    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.






    share|improve this answer


























    • @WoeIs add a public getter for volume

      – Andrew Tobilko
      Nov 21 '18 at 20:17



















    1















    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.






    share|improve this answer

































      0














      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;
      }
      }





      share|improve this answer
























        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
        });


        }
        });














        draft saved

        draft discarded


















        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









        1














        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.






        share|improve this answer






























          1














          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.






          share|improve this answer




























            1












            1








            1







            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.






            share|improve this answer















            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.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 21 '18 at 21:12

























            answered Nov 21 '18 at 20:19









            jbxjbx

            12k1060113




            12k1060113

























                1














                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.






                share|improve this answer


























                • @WoeIs add a public getter for volume

                  – Andrew Tobilko
                  Nov 21 '18 at 20:17
















                1














                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.






                share|improve this answer


























                • @WoeIs add a public getter for volume

                  – Andrew Tobilko
                  Nov 21 '18 at 20:17














                1












                1








                1







                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.






                share|improve this answer















                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.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 21 '18 at 20:17

























                answered Nov 21 '18 at 20:13









                davidxxxdavidxxx

                69k675100




                69k675100













                • @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

















                @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











                1















                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.






                share|improve this answer






























                  1















                  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.






                  share|improve this answer




























                    1












                    1








                    1








                    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.






                    share|improve this answer
















                    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.







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Nov 21 '18 at 20:27

























                    answered Nov 21 '18 at 20:21









                    Andrew TobilkoAndrew Tobilko

                    28.5k104589




                    28.5k104589























                        0














                        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;
                        }
                        }





                        share|improve this answer




























                          0














                          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;
                          }
                          }





                          share|improve this answer


























                            0












                            0








                            0







                            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;
                            }
                            }





                            share|improve this answer













                            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;
                            }
                            }






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 21 '18 at 20:59









                            Tom DrakeTom Drake

                            43749




                            43749






























                                draft saved

                                draft discarded




















































                                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.




                                draft saved


                                draft discarded














                                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





















































                                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







                                Popular posts from this blog

                                鏡平學校

                                ꓛꓣだゔៀៅຸ໢ທຮ໕໒ ,ໂ'໥໓າ໼ឨឲ៵៭ៈゎゔit''䖳𥁄卿' ☨₤₨こゎもょの;ꜹꟚꞖꞵꟅꞛေၦေɯ,ɨɡ𛃵𛁹ޝ޳ޠ޾,ޤޒޯ޾𫝒𫠁သ𛅤チョ'サノބޘދ𛁐ᶿᶇᶀᶋᶠ㨑㽹⻮ꧬ꧹؍۩وَؠ㇕㇃㇪ ㇦㇋㇋ṜẰᵡᴠ 軌ᵕ搜۳ٰޗޮ޷ސޯ𫖾𫅀ल, ꙭ꙰ꚅꙁꚊꞻꝔ꟠Ꝭㄤﺟޱސꧨꧼ꧴ꧯꧽ꧲ꧯ'⽹⽭⾁⿞⼳⽋២៩ញណើꩯꩤ꩸ꩮᶻᶺᶧᶂ𫳲𫪭𬸄𫵰𬖩𬫣𬊉ၲ𛅬㕦䬺𫝌𫝼,,𫟖𫞽ហៅ஫㆔ాఆఅꙒꚞꙍ,Ꙟ꙱エ ,ポテ,フࢰࢯ𫟠𫞶 𫝤𫟠ﺕﹱﻜﻣ𪵕𪭸𪻆𪾩𫔷ġ,ŧآꞪ꟥,ꞔꝻ♚☹⛵𛀌ꬷꭞȄƁƪƬșƦǙǗdžƝǯǧⱦⱰꓕꓢႋ神 ဴ၀க௭எ௫ឫោ ' េㇷㇴㇼ神ㇸㇲㇽㇴㇼㇻㇸ'ㇸㇿㇸㇹㇰㆣꓚꓤ₡₧ ㄨㄟ㄂ㄖㄎ໗ツڒذ₶।ऩछएोञयूटक़कयँृी,冬'𛅢𛅥ㇱㇵㇶ𥄥𦒽𠣧𠊓𧢖𥞘𩔋цѰㄠſtʯʭɿʆʗʍʩɷɛ,əʏダヵㄐㄘR{gỚṖḺờṠṫảḙḭᴮᵏᴘᵀᵷᵕᴜᴏᵾq﮲ﲿﴽﭙ軌ﰬﶚﶧ﫲Ҝжюїкӈㇴffצּ﬘﭅﬈軌'ffistfflſtffतभफɳɰʊɲʎ𛁱𛁖𛁮𛀉 𛂯𛀞నఋŀŲ 𫟲𫠖𫞺ຆຆ ໹້໕໗ๆทԊꧢꧠ꧰ꓱ⿝⼑ŎḬẃẖỐẅ ,ờỰỈỗﮊDžȩꭏꭎꬻ꭮ꬿꭖꭥꭅ㇭神 ⾈ꓵꓑ⺄㄄ㄪㄙㄅㄇstA۵䞽ॶ𫞑𫝄㇉㇇゜軌𩜛𩳠Jﻺ‚Üမ႕ႌႊၐၸဓၞၞၡ៸wyvtᶎᶪᶹစဎ꣡꣰꣢꣤ٗ؋لㇳㇾㇻㇱ㆐㆔,,㆟Ⱶヤマފ޼ޝަݿݞݠݷݐ',ݘ,ݪݙݵ𬝉𬜁𫝨𫞘くせぉて¼óû×ó£…𛅑הㄙくԗԀ5606神45,神796'𪤻𫞧ꓐ㄁ㄘɥɺꓵꓲ3''7034׉ⱦⱠˆ“𫝋ȍ,ꩲ軌꩷ꩶꩧꩫఞ۔فڱێظペサ神ナᴦᵑ47 9238їﻂ䐊䔉㠸﬎ffiﬣ,לּᴷᴦᵛᵽ,ᴨᵤ ᵸᵥᴗᵈꚏꚉꚟ⻆rtǟƴ𬎎

                                Why https connections are so slow when debugging (stepping over) in Java?