Reputation: 32767
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?
Upvotes: 0
Views: 381
Reputation: 3660
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.
Upvotes: 2
Reputation: 1277
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);
}
}
Upvotes: 0
Reputation: 1436
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);
}
}
Upvotes: 0
Reputation: 1751
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
Upvotes: 2