John Doe John Doe - 13 days ago 4
Java Question

Implementing generic method from interface in java

I have two interfaces and one class implementing them. In the interface I have shown one usual method and one generic method. When implementing them in main method usual method shows correct result, but the generic one does not. If possible could you tell me, why the increase_twice() method does not return correct result, please? Here are my code:

Rectangle.java:

public class Rectangle implements TwoDShape {

private double width, height;

public Rectangle (double width, double height) {
this.width = width;
this.height = height;
}

public double area() {
return width * height;
}

public double perimeter() {
return 2.0 * (width + height);
}

public void describe() {
System.out.println("Rectangle[width=" + width + ", height=" + height + "]");
}

@Override
public Rectangle increase_twice() {
// TODO Auto-generated method stub
return new Rectangle(2*this.width, 2*this.height);
}


}


TwoDShape.java:

public interface TwoDShape extends GeometricShape {
public double area();


}


GeometricShape.java:

public interface GeometricShape<T extends GeometricShape<T>>
{
public void describe();
public T increase_twice();

}


And finally test class ArrayListExample.java

import java.util.ArrayList;


public class ArrayListExample {



public static void describe_all( ArrayList<? extends GeometricShape> shapes )

{
for(int i=0;i<shapes.size();i++)
{
shapes.get(i).describe();

}
System.out.println("Total number of shapes:"+ shapes.size());
}


private static ArrayList<Rectangle> increase_size(ArrayList<Rectangle> rects)
{
for(int i=0;i<rects.size();i++)
{
rects.get(i).increase_twice();

}
return rects;

}


public static void main(String[] args) {


System.out.println("The result of describe() method:");

System.out.println();
System.out.println("Example rectangles");
ArrayList<Rectangle> rects = new ArrayList<Rectangle>();
rects.add(new Rectangle(2.0, 3.0));
rects.add(new Rectangle(5.0, 5.0));
describe_all(rects);
System.out.println();

System.out.println("The result of increase_twice() method:");
System.out.println();
System.out.println("Example rectangles after increase:");
ArrayList<Rectangle> double_rects = increase_size(rects);
describe_all(double_rects);

}


}


I expect that the increase_twice will return rectangles with Rectangle[width=4.0, height=6.0]
Rectangle[width=10.0, height=10.0] but it instead return the same as describe method which is:
Rectangle[width=2.0, height=3.0]
Rectangle[width=5.0, height=5.0]
If possible could you tell me where I make a mistake please?

Yan Yan
Answer

I think the issue is in increase_twice function. When you are calling rects.get(i).increase_twice(); you are actually creating a new Rectangle object with doubled values and returning. You are not updating the current object. The increase_twice method should be:

@Override
    public void increase_twice() {
        // TODO Auto-generated method stub
        this.width *= 2;
        this.height *= 2;
    }

This way you will update the width and height values of the current object that you are getting from the ArrayList. You might have to change the definition of increase_twice() because i don't think you need to return a new Rectangle object.

Let me know if that works.