0

Let's say I have two public classes like the ones below:

The oval class which will get the width and height parameters.

public class Oval extends Shape{
    OvalClass oval;
    public Oval(int width,int height){
        oval = new OvalClass("first",10);
    }

}

and a Shape class which is supposed to have any different form (that's why I'm extending it).

public class Shape {

    public void moveLeft(){
      //object?
      object.posX += 1;
  }
}

EDIT:

We don't know enough about GOval, the other classes, and the move() method to give a good answer.

Consider this other OvalClass as the oval class:

public class OvalClass {
    String name;
    int posX;
    public OvalClass(String name, int posx){}
}

The thing is, how can I get the object oval (GOval oval) created in the Oval in the Shape class? Is there any better approach?

zurfyx
  • 31,043
  • 20
  • 111
  • 145
  • 3
    We don't know enough about GOval, the other classes, and the move() method to give a good answer. – JB Nizet Feb 28 '14 at 19:14
  • With no further information this makes no sense at all. Why would you have an GOval in Shape? Shape is general, you said, so how could it know about an Oval even if it is GOval?? no sense – iberbeu Feb 28 '14 at 19:21
  • Is GOval also a shape? Do you have source for it as well so we can see what it is? – matt forsythe Feb 28 '14 at 19:23
  • Added a sample Oval class example @JBNizet – zurfyx Feb 28 '14 at 19:28

4 Answers4

2

Moving left and right is not something that only ovals do, or only squares. Every shape can move left or right. Therefore, Shape should contain the following functions:

public abstract class Shape {

    int x;
    int y;

    public void moveLeft(){
        this.x = x - 1;
    }

    ...

    abstract double getArea();
}

For the sake of brevity, I left out the other properties of a Shape. Imagine that it also has an up, down, right, and anything else that you want all shapes to have.

Now, we create an Oval.

public class Oval extends Shape {

    int height;
    int width;

    public Oval(int height, int width, int x, int y) {
        super(x, y);
        this.height = height;
        this.width = width;
    }

    public double getarea() { return Math.PI * width * height; }

}

Notice that we didn't have to tell an Oval how to move left or right. Because it extends a Shape, it already knows how to move left and right. In fact, we can tell our Ovals to move left and right just like this:

Oval o = new Oval(1, 1, 0, 0);
o.moveLeft();

Viola! Our Oval can move, even though we did not define a method called moveLeft inside of our Oval class. That is the beauty of inheritance - the moveLeft() method was inherited by our Oval.

Rainbolt
  • 3,542
  • 1
  • 20
  • 44
  • i think "object" should represent GOval access. – pL4Gu33 Feb 28 '14 at 19:16
  • 1
    @pL4Gu33 Why would you think that? Not all shapes have an oval. – Rainbolt Feb 28 '14 at 19:17
  • ohh sry missunderstanding, my fault – pL4Gu33 Feb 28 '14 at 19:20
  • "When an object refers to itself, you use the this keyword." - It does not refer to itself @John – zurfyx Feb 28 '14 at 19:42
  • @Jerry Your object SHOULD refer to itself. An Shape moves left. A Shape moves down. In order to do that, it needs to modify it's own x- and y-coordinates. If a Shape can move left, then an Oval extending a Shape can also move left, or anything else that extends a Shape. – Rainbolt Feb 28 '14 at 19:52
  • Ah, I get what you mean, but I just didn't want the function `moveLeft()` in the oval function (as it is supposed to be a general function), it should be only inside Shape instead. – zurfyx Feb 28 '14 at 20:01
  • @Jerry Even before my major edit, I explicitly said that the move functions belong inside *Shape*, not Oval. You obviously misread my answer. You can check the revision history if you do not believe me. – Rainbolt Feb 28 '14 at 20:05
  • I'm not misreading, you are misunderstanding me. Your shape `this` should refer to the object the object oval created. – zurfyx Feb 28 '14 at 20:14
  • @Jerry What exactly is wrong with my answer? Every Shape, Oval, Square, or Circle you ever create will be able to move left if you simply copy my code. You said you wanted only the Shape class to have `moveLeft()`, and that is what I have done. – Rainbolt Feb 28 '14 at 20:33
2

Try something like this:

public class Shape {
  // Now all Shapes can move()
  protected abstract void move(int x, int y);
  public void moveLeft(){
    //object?
    move(-1,0);
  }
}

public class Oval extends Shape {
  private GOval oval;
  public Oval(int width,int height) {
    oval = new GOval(0,0,width,height);
  }
  // Implement move()
  protected void move(int x, int y) {
    oval.move(x, y);// or whatever method on GOval makes it move()
  }
}

HTH

J Steven Perry
  • 1,711
  • 1
  • 17
  • 28
  • This is going to result in a StackOverflowException with the code you have now. Move Move Move Move Move... – Rainbolt Feb 28 '14 at 19:21
  • I don't either. Retracted. +1 for my ignorance. – Rainbolt Feb 28 '14 at 19:35
  • No worries. Thanks, John. Classy move. Right back atcha. – J Steven Perry Feb 28 '14 at 19:38
  • I'm pretty sure your answer is working. But, what is the point in having `moveLeft()` method in both classes? I was looking for a general function that I can use in the Shape class only to execute that `moveLeft()` or any function inside the object's class. – zurfyx Feb 28 '14 at 19:41
  • I'm not sure what you mean. The `moveLeft()` method was original to your design, and I sort of left it alone (though for the sake of completeness you may consider adding a `moveRight()` method as well). The only method you exposed as `public` was `moveLeft()`, so I assumed that was part of your design (i.e., that the caller would call `moveLeft()` to move the shape to the left). – J Steven Perry Feb 28 '14 at 19:45
  • What I was looking for is for the `Shape()` class ONLY to have that function. You are writing the `movingLeft()` to the concrete shape (oval) as well. – zurfyx Feb 28 '14 at 19:49
0

Are you thinking about overriding super class moveLeft() ?

public class Oval extends Shape{

        GOval oval;
        public Oval(int width,int height){
            oval = new GOval(0,0,width,height);
        }

        @Override 
        public void moveLeft(){

             if (oval != null)
                oval.move(-1,0);

        }

    }
alkber
  • 1,426
  • 2
  • 20
  • 26
0

You can use a parametrized class "Shape<T>" that have a concrete geometry called "GOval" or any others.

Consider this class

public class Shape<T> {

    T oval;
    public void moveLeft(){
            oval.move(-1,0);
  }
}

And its handler

public class Oval extends Shape<GOval>{

    public Oval(int width,int height){
        oval = new GOval(0,0,width,height);
    }
}