Java: possible invalid abstract class impletaion

I chose this title because I noticed that I did something wrong with the implantation of an abstract class, but I’m not quite sure yet.

I created the abstract class MoveAble for training purposes and created the Ball class from it. I also created the GetPosition () method only for the moveAble class and used it from the ball class. But when I called GetPosition () on any spherical object, I got the varibale position of the Moveable Abstract object instead.

I suppose this is so, but from my understanding we still cannot use an abstract class, so I need to get the position value of the child class, even if I only implemented this method on the parent class.

Note. I am a beginner Java programmer. probably the best way to do what I did, but that is what I came out with. I would like to hear what you guys think about it, And if you think that everything is crooked, and there is a better way for all this, I will be glad to know it.

Roaming class:

public abstract class MoveAble {
    private int[] position = new int[2];
    private int[] velocity = { 1, 1 };

    public int[] getPosition() {
    return position;
    }
    public abstract void move(int width, int height) ;

Ball Class:

public class Ball extends MoveAble{

    private int[] position = new int[2];
    private int[] velocity = { 1, 1 };

    public Ball(int x_position, int y_position) {

        position[0] = x_position;
        position[1] = y_position;
    }

    @Override
    public void move(int width, int height) {
        if (position[0] >  width - 30 || position[0] <  1) {
            velocity[0] *= -1;
        }
        if (position[1] > height - 30 || position[1] <  1) {
            velocity[1] *= -1;
        }
        position[0] += velocity[0];
        position[1] += velocity[1];

    }
+4
source share
4 answers

You can create an interface called IMovable so that you can reference this object through your interface when you only care about engines ...

Now, if you have common code for several subtypes, you can create an Abstract class and call its base class code when moving, as I did below, and then add a subtype of the specific movement logic in the subtype ... see below

public class Ball extends AbstractBall {
    public Ball(int x_position, int y_position) {
        super(x_position, y_position);
    }

    public void move(int width, int height) {
        super.move(width, height);
        System.out.println("SUBTYPE-CLASS MOVE IS CALLED");
    }           

        public static void main(String[] args) {
                Ball ball = new Ball(10, 20);
                ball.move(1, 2);
        }
}

abstract class AbstractBall implements IMovable {

    protected int[] position = new int[2];
    protected int[] velocity = { 1, 1 };

    public AbstractBall(int x_position, int y_position) {
        position[0] = x_position;
        position[1] = y_position;
    }

    public void move(int width, int height) {

        System.out.println("BASE-CLASS MOVE IS CALLED");

        if (position[0] >  width - 30 || position[0] <  1) {
            velocity[0] *= -1;
        }
        if (position[1] > height - 30 || position[1] <  1) {
            velocity[1] *= -1;
        }
        position[0] += velocity[0];
        position[1] += velocity[1];
    }           
}

interface IMovable {
    public void move(int width, int height);
}

, eclipse .

enter image description here

, , , .

enter image description here

?

import javax.swing.JButton;

public class Demo {

  public static void main(String[] args) {

    IPrintable instance1 = new PrintableButton();
    instance1.print();

    PrintableButton instance2 = new PrintableButton();
    instance2.print();
  }
}

interface IPrintable {
  public void print();
}

class PrintableButton extends JButton implements IPrintable {
  private static final long serialVersionUID = 1L;

  @Override
  public void print() {
    System.out.println("Printable Button");
  }
}
+1

position velocity MoveAble Ball. , . position velocity Ball, .

, , : Ball, public int[] getPosition() MoveAble MoveAble, MoveAble ( , , ) .

: MoveAble , . ,

+5

, (re) position velocity Ball, , MoveAble.

posiition velocity Ball. protected MoveAble , , , .

+1

, . ( .) , - , .

, . , , Ball.

This is what polymorphism is. Object-oriented programming has four important characteristics: abstract data types, encapsulation, polymorphism, and inheritance. Make sure you fully understand them.

I think your mistake gives abstract and concrete classes the same member variables. Where is this reuse? Remove them from Balland make them protected in the parent. Do not override methods in a child unless they provide different behavior.

+1
source

Source: https://habr.com/ru/post/1598122/


All Articles