Home > DeveloperSection > Forums > How to return different values toString?
Royce Roy
Royce Roy

Total Post:134

Points:938
Posted on    November-05-2014 3:15 AM

 Java Java 
Ratings:


 1 Reply(s)
 562  View(s)
Rate this:
I'm pretty new in java and I'm doing a simple program but I don't know why I get different values, i.e., if I use getX, getY and getZ I get (6,5,8) but if I use toString I get different values for X and Y (3, 4, 8), so can anyone explain me why it happens because as far as I understand it should get the same values in both cases or what I'm doing wrong?

public class Coordinates {

 

    private double coorX, coorY;

 

    Coordinates()

    {

        coorX = 1;

        coorY = 1;

    }

 

    Coordinates(double x, double y)

    {

        coorX = x;

        coorY = y;     

    }

 

    void setX(double x)

    {

        coorX = x;

    }

 

    void setY(double y)

    {

        coorY = y;

    }

 

    double getX()

    {

        return coorX;

    }

 

    double getY()

    {

        return coorY;

    }

 

    public String toString()

    {

        String myString = "(" + coorX + " , " + coorY + ")";

        return myString;

    }

 

    public class Coordinates3D extends Coordinates{

        private double coorZ;

 

        Coordinates3D()

        {

            super();

            coorZ = 1;

        }

 

        Coordinates3D(double x, double y, double z)

        {

            super(x,y);

            coorZ = z;

        }

 

        public void setZ(double z)

        {

            coorZ = z;

        }

 

        double getZ()

        {

            return coorZ;

        }

 

        @Override

        public String toString()

        {

            String myString = "(" + coorX + " , " + coorY + " , " + coorZ + ")" ;

            return myString;

 

        }

 

    }

 

 

    /**

     * @param args

     */

    public static void main(String[] args) {

        // TODO Auto-generated method stub

 

        Coordinates test1 = new Coordinates(3,4);

 

 

        System.out.println(test1.toString());

 

        System.out.println(test1.getX());

        System.out.println(test1.getY());

 

 

        Coordinates3D test2 = test1.new Coordinates3D(6,5,8);

 

        System.out.println(test2.toString()); ---> here is the problem

 

        System.out.println(test2.getX());

        System.out.println(test2.getY());

        System.out.println(test2.getZ());

 

 

    }

 

}



Takeshi Okada
Takeshi Okada

Total Post:89

Points:629
Posted on    November-05-2014 5:13 AM

First there is a problem on how you define the visibility of the fields of the super class:

public class Coordinates {
    private double coorX, coorY;

This is that you cannot invoke super.coorX nor super.coorY on any sub class e.g. Coordinates3D. So, in toString method, when you have this code:

String myString = "(" + coorX + " , " + coorY + " , " + coorZ + ")" ;

It compiles and runs fine because Coordinates3D is an inner class. So, when using coorX here it's accessing to the value of coorX field stored in the instance of Coordinates class that created the instance of Coordinates3D. This can be easy to replicate if you separate the classes:

class Coordinates {
    private double coorX, coorY;
}

public class Coordinates3D extends Coordinates {
    @Override
    public String toString() {
        String myString = "(" + coorX + " , " + coorY + " , " + coorZ + ")" ;
        return myString;
    }
}

The best solution would be:

  • mark the fields in the super class as protected
  • separate the classes

If you still want to keep Coordinates3D as inner class (not recommended), then:

  • mark the fields in the super class as protected
  • use super.coorX and super.coorY to not have the same unexpected behavior.

Don't want to miss updates? Please click the below button!

Follow MindStick