0

The following is a toy problem of my original problem. Bird is an interface. Cardinal is the subclass of Point and it implements the Bird interface. The Aviary class carries out the implementation.

Question: What should I put in the getPosition() instance method such that the Aviary class carries the getPosition() method correctly?

Please correct me if the abstract method in the bird interface is coded wrong.

public interface Bird{
    public Point getPosition();
}

public class Point{
    private int x;
    private int y;

 // Constructs a new Point at the given initial x/y position.
    public Point(int x, int y){
        this.x = x;
        this.y = y;
    }

// Returns the x-coordinate of this point
    public int getX(){
        return x;
    }

    // Returns the y-coordinate of this Point
    public int getY(){
        return y;
    }
}

Question is in the following code:

public class Cardinal extends Point implements Bird{

    // Constructors
    public Cardinal(int x , int y){
        this(x,y);
    }

     // not sure how to write this instance method
     public Point getPosition(){
        ???????????
    }

}

public class Aviary{
       public static void main(String[] args){
                Bird bird1 = new Cardinal(3,8);
                Point pos = bird1.getPosition();
                System.out.println("X: " + pos.getX() + ", Y: " + pos.getY() );
       }
}
5
  • In getPosition(),write: return this Commented Sep 25, 2014 at 4:13
  • 1
    Why is a Cardinal a Point? Shouldn't a Cardinal instance instead use a Point instance variable to keep track of its location? It'd be pretty surprising if code expecting a location got a bird. Commented Sep 25, 2014 at 4:15
  • 1
    Is a Cardinal a Point? This is the typical question to ask to check if inheritance makes sense. On the other hand, it makes complete sense to say "A cardinal has a position which is a point." This implies that it makes more sense to use composition instead. To do this, simply add a Point member variable to your Cardinal class. Better yet, make Bird an abstract class with a Point member variable since every bird has a position. Commented Sep 25, 2014 at 4:16
  • Using composition will also make the answer to your more immediate question incredibly obvious. Commented Sep 25, 2014 at 4:18
  • Hi, I am new to Java. Please elaborate on composition. Thanks. I am happy to learn new things. Commented Sep 25, 2014 at 4:29

2 Answers 2

3

Just return the object itself:

public Point getPosition(){
    return this; // returns a Point object
}

I gave an answer, but I am not sure if you have a design nightmare or a one-of-a-kind design simplification. A Point subclass implementing a Bird makes me bang my head on the wall, but having both types in one object will make the calculations pretty neat, (if you have massive calculations, that is). Because instead of bird.getPosition().getX(), you can write bird.getX().

Point bird1 = new Cardinal(3, 8);
Point bird2 = new Cardinal(4, 12);

// calculate the distance between two birds
double distance = Math.sqrt(Math.pow(bird2.getX() - bird1.getX(), 2) + Math.pow(bird2.getY() - bird2.getY(), 2));

But if your system is not a bird simulator that needs heavy calculations on birds represented by mere Point objects, I think you should use composition over inheritance.

public interface IBird {
    public Point getPosition()
}

class Bird implements IBird {
    private Point position;

    public Bird(int x, int y) {
        this.position = new Point(x, y);
    }

    public Point getPosition() {
        return this.position;
    }
}

// and then in main()
Bird bird = new Bird(3, 8);
Point pos = bird.getPosition();
System.out.println("X: " + pos.getX() + ", Y: " + pos.getY() );
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. This makes a lot more sense now. And you made me realise I shouldn't use inheritance blindly.
2

The Cardinal class objects have an is-a relationship with the Point class objects, so you could just return this; as Krumia suggested.

P.S. you can use the super keyword when referring to a superclass within a subclass to access it's protected and public methods.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.