2

I know there are similar to posts to this but none of them exactly fit I was trying to do. I am a newer programmer and just started reading up on the super class for Java. I have created a super class named shape. I then created a subclass named circle. I am stumped on two parts here, calling the toString method in my super class from my subclass toString method as well as one of my constructors from my subclass to my superclass. I have provided my super class, subclass and a main test method below.

The Shape class:

public class Shape
{
   private boolean isFilled;
   private String color;

   public Shape()
   {
      this.isFilled = true;
      this.color = "Green";

   }


   public Shape(boolean x, String y)
   {
      this.isFilled = x;
      this.color = y;
   }   

   public void setFilled(boolean fill)
   {
      this.isFilled = fill;
   }

   public boolean getFilled()
   {
      return this.isFilled;
   }

   public void setColor(String x)
   {
      this.color = x;
   }

   public String getColor()
   {
      return this.color;
   }


   @Override
   public String toString()
   {
      String s = "Filled:" + this.isFilled + "\n";
      String t = "Color: " + this.color;
      String u = s + t;

      return u;

   }
}

The Circle:

public class Circle extends Shape
{
   private double radius;


   public Circle()
   {
      this.radius = 1;
   }

   public Circle(double x)
   {
      this.radius = x;
   }


   public Circle(double radius, boolean isFilled, String Color)
   {
      super(isFilled, Color);
      this.radius = radius;

   }


   public void setRadius(double radius)
   {
      this.radius = radius;
   }


   public double setRadius()
   {
      return this.radius;
   }


   public double getArea()
   {  
      double area = this.radius * this.radius * Math.PI;

      return area;
   }


   @Override
   public String toString()
   {

      String x = "Radius: " + this.radius +"\n";
      String y = "Area: " + getArea() + "\n";

      String z = x + y;
      return z;

   }    
}

A TestShape:

public class TestShape
{
   public static void main(String[] args)
   {

      Circle c1 = new Circle(2.67);
      System.out.println("c1: ");
      System.out.println(c1.toString());
      System.out.println();

      Circle c2 = new Circle(3, false, "Red");
      System.out.println("c2: ");
      System.out.println(c2.toString());
      System.out.println();
   }
}

Expected output:

c1:
Radius: 2.67
Area: 22.396099868176275
Filled: true
Color: Green

c2:  
Radius: 3.0
Area: 28.274333882308138
Filled: false
Color: Red

Actual output:

c1: 
Radius: 2.67
Area: 22.396099868176275

c2: 
Radius: 3.0
Area: 28.274333882308138
Zabuzard
  • 25,064
  • 8
  • 58
  • 82
hotrod28
  • 45
  • 8
  • 1
    *"I am stumped on two parts here"* - how come? What confuses you, why, what is your question? – luk2302 Mar 01 '18 at 21:52
  • What is your actual output? What can you learn from it? – GalAbra Mar 01 '18 at 21:53
  • I am stuck on calling my toString superclass method from my subclass which is in my description. I am also stuck on calling one of my constructors from my subclass to my superclass which is in my description as well. – hotrod28 Mar 01 '18 at 21:54
  • You said that already, but you haven't presented any specific, answerable question. It's also unclear how the code relates to your question. Does it work? If not, what doesn't work about it? What are you expecting it to do? What does it actually do? – shmosel Mar 01 '18 at 21:57
  • actual output is provided – hotrod28 Mar 01 '18 at 21:59
  • 1
    `super.toString()` – GriffeyDog Mar 01 '18 at 22:05
  • Possible duplicate of [In Java, how do I call a base class's method from the overriding method in a derived class?](https://stackoverflow.com/questions/268929/in-java-how-do-i-call-a-base-classs-method-from-the-overriding-method-in-a-der) – Loris Securo Mar 01 '18 at 22:09
  • Note that newline should not be added with `\n` (platform dependent, on Windows it should be `\r\n` for example) but with the result returned by `System.lineSeparator()` (platform independent). Also, combining strings is best done using a `StringBuilder` which is more efficient than concatenating strings. – Zabuzard Mar 01 '18 at 22:52

3 Answers3

2

Your Circle class overwrites the behavior of the parent toString method and does not combine the parent result with the own. Therefore, you only see what the Circle#toString method computes:

@Override
public String toString() {
    String x = "Radius: " + this.radius + "\n";
    String y = "Area: " + getArea() + "\n";
    String z = x + y;
    return z;
}

namely radius and area.

If you want to add the parents result you need to call it using super.toString() and combine it with what you want to add, like

@Override
public String toString() {
    // Call parent
    String parentText = super.toString();

    // Own stuff
    String x = "Radius: " + this.radius + "\n";
    String y = "Area: " + getArea() + "\n";
    String z = x + y;

    // Combine with parent
    return z + parentText;
}
Zabuzard
  • 25,064
  • 8
  • 58
  • 82
1

You are overriding the toString() method but your not calling the one in super class. You need the following method in the subclass.

 public String toString()
 {
   String x = "Radius: " + this.radius +"\n";
   String y = "Area: " + getArea() + "\n";

   String z = x + y;
   return z + super.toString();
 }   
Armin
  • 89
  • 7
0

Assuming that you expected the parent class toString() to be called, and not the child one, it is normal that it was not case, given that you have overridden toString() method in the child class, which you call explicitly in your main() method.

The fact that the child class is constructed using a parent controller will not affect the toString() because you are calling explicitly the overridden toString() of the child class.

If you want the parent toString() method to be the one executed, you could call and print super.toString() instead of c1.toString().

N. Labrahmi
  • 657
  • 4
  • 23