Java how to implement and design an abstract class

user2651804

I've run into a design problem in my java code. My application uses missiles, and there are different types of missiles that all work identical except they have 3 unique attributes. The constructor of a missile must know these attributes. I decided to make missile an abstract class, but I can't assign values to protected variables in a subclass outside of a method/constructor. Also I can't declare the variables in the constructor, because I must make the call to the super-constructor first thing. How can I be smart about this problem?

public abstract class Missile {

private int x, y;
private Image image;
boolean visible;

private final int BOARD_WIDTH = 390;

protected final int MISSILE_SPEED;
protected final int MISSILE_HEIGHT;
protected String file;

public Missile(int x, int y) {
    ImageIcon ii =
        new ImageIcon(this.getClass().getResource(file));
    image = ii.getImage();
    visible = true;
    this.x = x;
    this.y = y - Math.floor(MISSILE_HEIGHT/2);
}


public Image getImage() {
    return image;
}

public int getX() {
    return x;
}

public int getY() {
    return y;
}

public boolean isVisible() {
    return visible;
}

public void move() {
    x += MISSILE_SPEED;
    if (x > BOARD_WIDTH)
        visible = false;
}
}

And there is an ideal implementation of a subclass, except it doesn't work. (it can't recognize the protected variables). What do I do?

public class Laser extends Missile {

    MISSILE_SPEED = 2;
    MISSILE_HEIGHT = 5;
    file = "laser.jpg";

public Laser(int x, int y) {
    super(x, y);
}

}

JB Nizet

Change the base class fields and constructors to

protected final int speed;
protected final int height;

public Missile(int x, int y, int speed, int height, String file) {
    ImageIcon ii =
        new ImageIcon(this.getClass().getResource(file));
    image = ii.getImage();
    visible = true;
    this.speed = speed;
    this.height = height;
    this.x = x;
    this.y = y - Math.floor(height/2);
}

And the subclass to:

public class Laser extends Missile {
    public Laser(int x, int y) {
        super(x, y, 2, 5, "laser.jpg");
    }

    ...
}

The attributes are already in the base class, so they must not be redefined in the subclass. All-uppercase naming is reserved to constants in Java.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to implement methods of an abstract class? (Java)

From Dev

Nested abstract class in an abstract class and how to implement it

From Dev

How do I implement abstract class inside abstract class in the implementation class in Java?

From Dev

How do I implement abstract class inside abstract class in the implementation class in Java?

From Dev

How to implement an abstract class in Go?

From Dev

How do you implement a Java Enumeration Abstract class and interface?

From Dev

How do you implement a Java Enumeration Abstract class and interface?

From Dev

How to create Mock to a class that implement an abstract class

From Dev

How to implement generic fields from abstract class?

From Dev

How to implement Interface to an abstract class which is constraint?

From Dev

How to Implement Extension Method in Abstract Class

From Dev

How to implement Interface to an abstract class which is constraint?

From Dev

How to implement Abstract class in laravel 5.3

From Dev

How to implement operator+ in abstract base class

From Dev

How i can design these with abstract class

From Dev

Java child class not able to implement parent class's abstract method

From Dev

Implement IDisposable in abstract class

From Dev

Java: How to use Abstract class

From Dev

How to instantiate an abstract class in Java?

From Dev

About abstract class design

From Dev

How do I implement a Decorator class of abstract class with no default constructor?

From Dev

How do I implement a Decorator class of abstract class with no default constructor?

From Dev

The class design - interface or abstract class?

From Java

How do I implement multiple methods of an abstract class efficiently?

From Dev

how to define an abstract class in python and force implement variables

From Dev

How To implement abstract function which uses data members of parent class

From Dev

How to implement ObservableCollection<InterfaceType> from abstract class in C#?

From Dev

How to correct implement ICloneable in a tree hierarchy rooted by an abstract class?

From Dev

Class must be declared as abstract or implement abstract method

Related Related

  1. 1

    How to implement methods of an abstract class? (Java)

  2. 2

    Nested abstract class in an abstract class and how to implement it

  3. 3

    How do I implement abstract class inside abstract class in the implementation class in Java?

  4. 4

    How do I implement abstract class inside abstract class in the implementation class in Java?

  5. 5

    How to implement an abstract class in Go?

  6. 6

    How do you implement a Java Enumeration Abstract class and interface?

  7. 7

    How do you implement a Java Enumeration Abstract class and interface?

  8. 8

    How to create Mock to a class that implement an abstract class

  9. 9

    How to implement generic fields from abstract class?

  10. 10

    How to implement Interface to an abstract class which is constraint?

  11. 11

    How to Implement Extension Method in Abstract Class

  12. 12

    How to implement Interface to an abstract class which is constraint?

  13. 13

    How to implement Abstract class in laravel 5.3

  14. 14

    How to implement operator+ in abstract base class

  15. 15

    How i can design these with abstract class

  16. 16

    Java child class not able to implement parent class's abstract method

  17. 17

    Implement IDisposable in abstract class

  18. 18

    Java: How to use Abstract class

  19. 19

    How to instantiate an abstract class in Java?

  20. 20

    About abstract class design

  21. 21

    How do I implement a Decorator class of abstract class with no default constructor?

  22. 22

    How do I implement a Decorator class of abstract class with no default constructor?

  23. 23

    The class design - interface or abstract class?

  24. 24

    How do I implement multiple methods of an abstract class efficiently?

  25. 25

    how to define an abstract class in python and force implement variables

  26. 26

    How To implement abstract function which uses data members of parent class

  27. 27

    How to implement ObservableCollection<InterfaceType> from abstract class in C#?

  28. 28

    How to correct implement ICloneable in a tree hierarchy rooted by an abstract class?

  29. 29

    Class must be declared as abstract or implement abstract method

HotTag

Archive