Java how to implement and design an abstract class - java

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);
}
}

I think the best way to do what you want it to do is make abstract methods in Missile that the subclasses have to implement. For example, add these to Missile:
public abstract int getMissileSpeed();
public abstract int getMissileHeight();
public abstract int getFileName();
Then your subclass has to implement it, and you can make it constant like so:
public class Laser extends Missile {
public Laser(int x, int y) {
super(x, y);
}
public int getMissileSpeed() {
return 2;
}
public int getMissileHeight() {
return 5;
}
public String getFileName() {
return "laser.jpg";
}
}
edit: And then of course anywhere that you want to retrieve the constant value you just call those methods.

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.

I'm not sure if missile needs to be an abstract class, but I think something like this might be what you're going for:
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;
public Missile(int x, int y, int speed, int height, String file) {
MISSILE_SPEED = speed;
MISSILE_HEIGHT = height;
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 class Laser extends Missile {
public Laser(int x, int y) {
super(x, y, 2, 5, "laser.jpg");
}
}

Create an interface and put all your final fields in it.
Now implement this interface within Missile and Laser both. At least that would solve the issue of access.

Related

Moving circle towards touched point

i'm trying to move a circle to the point i touch in my running app. I want to see the circle moving along a path towards this point i touch.
I have three classes:
public class Drawing extends View{
Context ctx;
static Circle c1;
private float circleCenterX = 100;
private float circleCenterY = 100;
private float lerpX;
private float lerpY;
private float time = 25;
private float frames = 100;
public Drawing(Context context) {
super(context);
this.ctx = context;
c1 = new Circle (165, 350, 33);
}
public void update(float x, float y) {
this.circleCenterX = x;
this.circleCenterY = y;
}
protected void onDraw (android.graphics.Canvas canvas){
Paint p = new Paint();
p.setColor(Color.GREEN);
lerpX = (circleCenterX - c1.getX()) * (time / frames) + c1.getX();
lerpY = (circleCenterY - c1.getY()) * (time / frames) + c1.getY();
canvas.drawCircle(lerpX, lerpY, c1.getR(), p);
c1.setX(lerpX);
c1.setY(lerpY);
}
public class Circle {
private float x;
private float y;
private float r;
public Circle(float x, float y, float r) {
super();
this.x = x;
this.y = y;
this.r = r;
}
public float getX() {
return x;
}
public void setX(float x) {
this.x = x;
}
public float getY() {
return y;
}
public void setY(float y) {
this.y = y;
}
public float getR() {
return r;
}
public void setR(float r) {
this.r = r;
}`
public class Game extends Activity implements OnTouchListener {
Drawing d;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
d=new Drawing(this);
setContentView(d);
d.setOnTouchListener(this);
}
#Override
public boolean onTouch(View v, MotionEvent me) {
d.update(me.getX(), me.getY());
d.invalidate();
return true;
}
I think i will need something like a while or for loop to increment the x and y coords and or maybe need a speed value?!
Maybe im totally wrong and its a lot of more math to get it.
Thanks for your help
Cheers
Every time onDraw is called you need to move the circle a bit. The easiest way is just to move a certain number of pixels each time it's called.
To be able to do this you need to keep track of:
Where the animation started
Where you want the animation to end
Use linear interpolation to calculate the position of the circle in each onDraw call

Rectangle Wall collision in Java

I really could use some help in order to find a working solution for my game.
My game is almost done, but the walls in my game are still not working as they should.
I have tried to find a solution on the internet for this problem, but i still haven't found a simple way to stop a rectangle just before it will collide with a wall (another rectangle).
Right now i have implemented a collision detection between the player rectangle and the wall rectangle and then stopped it to move, but then it gets stuck inside a wall when it hits.
Want it to stop just before, so it still can move. The code i have done this with so far is here:
Pacman Class
public class Pacman {
private String pacmanup = "pacmanup.png";
private String pacmandown = "pacmandown.png";
private String pacmanleft = "pacmanleft.png";
private String pacmanright = "pacmanright.png";
private int dx;
private int dy;
private int x;
private int y;
private int width;
private int height;
private boolean visible;
private Image imageup;
private Image imagedown;
private Image imageleft;
private Image imageright;
public Pacman() {
ImageIcon i1 = new ImageIcon(this.getClass().getResource(pacmanup));
imageup = i1.getImage();
ImageIcon i2 = new ImageIcon(this.getClass().getResource(pacmandown));
imagedown = i2.getImage();
ImageIcon i3 = new ImageIcon(this.getClass().getResource(pacmanleft));
imageleft = i3.getImage();
ImageIcon i4 = new ImageIcon(this.getClass().getResource(pacmanright));
imageright = i4.getImage();
width = imageup.getWidth(null);
height = imageup.getHeight(null);
visible = true;
x = 270;
y = 189;
}
public int getDx() {
return dx;
}
public void setDx(int dx) {
this.dx = dx;
}
public int getDy() {
return dy;
}
public void setDy(int dy) {
this.dy = dy;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public void setX(int x) {
this.x = x;
}
public void setY(int y) {
this.y = y;
}
public Image getImageup() {
return imageup;
}
public Image getImagedown() {
return imagedown;
}
public Image getImageleft() {
return imageleft;
}
public Image getImageright() {
return imageright;
}
public void setVisible(boolean visible) {
this.visible = visible;
}
public boolean isVisible() {
return visible;
}
public Rectangle getBounds() {
return new Rectangle(x, y, width, height);
}
public void move() {
x += dx;
y += dy;
}
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_LEFT) {
dx = -2;
dy = 0;
}
if (key == KeyEvent.VK_RIGHT) {
dx = 2;
dy = 0;
}
if (key == KeyEvent.VK_UP) {
dx = 0;
dy = -2;
}
if (key == KeyEvent.VK_DOWN) {
dx = 0;
dy = 2;
}
}
Here i have created a Rectangle getBounds method which i use to create an rectangle of the pacman and place an image over it.
Barrier class / Wall class
public class Barrier {
private String barrier = "barrier.png";
private int x;
private int y;
private int width;
private int height;
private boolean visible;
private Image image;
public Barrier(int x, int y) {
ImageIcon ii = new ImageIcon(this.getClass().getResource(barrier));
image = ii.getImage();
width = image.getWidth(null);
height = image.getHeight(null);
visible = true;
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public boolean isVisible() {
return visible;
}
public void setVisible(Boolean visible) {
this.visible = visible;
}
public Image getImage() {
return image;
}
public Rectangle getBounds() {
return new Rectangle(x, y, width, height);
}
This class also have the Rectangle getBounds class which i use to detect collision.
The last code i show is how i do the collision detection so far:
Code inside Board class
Rectangle r3 = pacman.getBounds();
for (int j = 0; j<barriers.size(); j++) {
Barrier b = (Barrier) barriers.get(j);
Rectangle r4 = b.getBounds();
if (r3.intersects(r4)) {
System.out.println("Wall hit");
pacman.setDx(0);
pacman.setDy(0);
}
}
Well, what i do, if there is a collision between r3 and r4, i gonna set Dx and Dy to 0.. what i want to find another solution so it detect for collision but i wont get stuck inside a wall, but i don't know how to :/
Hope someone will help.
There are two approaches you can follow. One is ugly but easier, the other one requires a deeper redesign of your classes.
1) Ugly/Simple Approach
In the ugly one, you keep moving your guy before doing the collision checks. Simply move your pacman back to the point where it was not stuck. You accomplish that by inverting the last directions used:
Code inside Board class
Change your reaction in case you find a collision: just walk the same distance, backwards.
if (r3.intersects(r4)) {
System.out.println("Wall hit, move back");
pacman.setDx(-pacman.getDx());
pacman.setDy(-pacman.getDy());
// Possibly need to call move() here again.
pacman.move();
break;
}
Ugly, but should work.
Not recommended to coding perfectionists with OCD and heart disease, though.
2) Redesign
In this approach, you test the position pacman will occupy before doing any actual moves. If that spot is not into any barrier, then perform the movement for real.
Code inside Pacman class
Add this method, so that you can check for collisions against the new bounds.
public Rectangle getOffsetBounds() {
return new Rectangle(x + dx, y + dy, width, height);
}
Code inside Board class
// Strip the call to pacman.move() prior to this point.
Rectangle r3 = pacman.getOffsetBounds(); // Check against the candidate position.
for (int j = 0; j<barriers.size(); j++) {
Barrier b = (Barrier) barriers.get(j);
Rectangle r4 = b.getBounds();
if (r3.intersects(r4)) {
System.out.println("Wall hit");
pacman.setDx(0);
pacman.setDy(0);
// Quit the loop. It's pointless to check other barriers once we hit one.
break;
}
}
// Now we're good to move only in case there's no barrier on our way.
pacman.move();
Particularly I prefer this approach, but it's up to you to pick the best one.

Java awt Why can I only remove most recent addition to Linked List

I'm creating a space shooter game in Java awt for my college computer science project.
The enemies that I have spawn every 3 seconds via a timer and are added to a LinkedList, and a for loop renders them all.
In the class I have for my player's bullet object, there are if statements to check whether the laser comes into the bounds of an enemy, and if they are all true it removes the enemy from the LinkedList.
However, only the most recent addition to the LinkedList is being removed; the bullet passes through the others and nothing happens. This is my first time making a game, and the first time I've ever used a LinkedList, so excuse any misunderstandings.
The controller class controls the enemies, the Laser class is the bullet and the Enemy class is the Enemy object. There's also a player, Main and GUI class.
import java.awt.*;
import java.util.LinkedList;
import java.util.Timer;
import java.util.TimerTask;
public class Controller
{
private LinkedList<Enemy> e = new LinkedList<Enemy>();
Enemy tempEnemy, tempEnemy2
;
Main main;
int refreshSpawn = 3000; //move timer refresh rate
int xpos;
int width;
int ypos;
int height;
Timer spawnTimer = new Timer();
public Controller(Main main)
{
this.main = main;
spawn();
}
public void spawn()
{
spawnTimer.schedule(new TimerTask()
{
public void run() //run method and timer
{
addEnemy(new Enemy(main, (int)(Math.random()*4+2)));
}
}, 0, refreshSpawn);
}
public void render(Graphics g)
{
for(int i = 0; i < e.size(); i++)
{
tempEnemy = e.get(i);
xpos = tempEnemy.getX();
width = tempEnemy.getXsize();
ypos = tempEnemy.getY();
height = tempEnemy.getYsize();
tempEnemy.render(g);
}
}
public void update()
{
for(int i = 0; i < e.size(); i++)
{
tempEnemy2 = e.get(i);
tempEnemy2.move();
}
}
public void addEnemy(Enemy enemy)
{
e.add(enemy);
System.out.println(e.size());
//spawn();
}
public void removeEnemy()
{
e.remove(tempEnemy);
}
public int getX()
{
return xpos;
}
public int getY()
{
return ypos;
}
public int getXsize()
{
return width;
}
public int getYsize()
{
return height;
}
public Enemy getEnemy()
{
return tempEnemy;
}
}
import java.awt.*;
public class Enemy
{
Image ship; //image of enemy ship
int x, y; //ship position
int speed;
public Enemy(Main main, int speed) //constructing enemy
{
this.speed = speed;
ship = main.getImage(main.getDocumentBase(), "enemyShip"+(int)(Math.random()*6+1)+".png"); //picture for enemy ship
x = (int)(Math.random()*900+1); //enemy has a starting position at a random x point
y = -100; //start ship slightly off screen so it doesn't suddenly appear
}
public void move()
{
y += speed;
if(y > 600)
{
y = -100;
x = (int)(Math.random()*900);
}
}
public void render(Graphics g)
{
g.drawImage(ship, x, y, null);
}
public int getX()
{
return x;
}
public int getY()
{
return y;
}
public int getXsize()
{
return ship.getWidth(null);
}
public int getYsize()
{
return ship.getHeight(null);
}
}
import java.awt.*;
public class Laser
{
Image img; //image of laser
int laserSpeed = 10; //speed of laser
int x, y; //position of laser
int xSize, ySize; //size of laser
Controller cont;
GUI gui;
public Laser(Image img, int x, int y, Controller cont, GUI gui) //constructing laser
{
this.cont = cont;
this.img = img; //setting laser image
this.gui = gui;
xSize = x; //size of laser
ySize = y; //size of laser
}
public void shoot(int x, int y, int shipSize)
{
this.x = x + (shipSize/2) - (xSize/2);
this.y = y;
}
public void move()
{
y -= laserSpeed;
if(x <= cont.getX() + cont.getXsize() && x + xSize >= cont.getX() - cont.getXsize())
{
if(y <= cont.getY() + cont.getYsize() && y > 0)
{
remove();
cont.removeEnemy();
gui.scoreUp(5);
}
}
}
public int getX()
{
return x;
}
public int getY()
{
return y;
}
public int getXSize()
{
return xSize;
}
public int getYSize()
{
return ySize;
}
public Image getImage()
{
return img;
}
public void remove()
{
y = -ySize;
x = -100;
}
}
From what I can tell, tempEnemy is assigned to the last element in the LinkedList by the render method. This means that when you call removeEnemy it is removing the last rendered object (likely the last object you added).
What you should be doing is telling the Controller which Enemy it should be using, it has absolutely no idea what your intentions are when you call it...

Drawing Issues - Drawing a compound shape from other shapes

So I have an assignment for University. The concept was that we were to complete some class hierarchy stuff. Basically it was stuff to allow us to draw different shapes.
I can successfully draw each shape; where I need it to be and how big I need it to be, like required... the part I'm having trouble on is this compound hierarchy.
Basically we're supposed to have a new class called Compound.java and extending from that we are supposed to have three other classes, House, tree, and earth; each of which are supposed to take the shape objects we created (Rectangle, Square, Line, Oval and Circle) and draw the required pictures as denoted by the class name.
Where I am having the problem is in the house class; for example: I can get it to draw one rectangle but when I try to get it to draw the second rectangle after, it basically forgets about the first and only draws the second!
We haven't had any practice with the Graphics stuff so I don't know any methods or anything that I can call to draw then continue in the House constructor.
I understand why it overwrites the first rectangle, when the House constructor is called, it runs through all the stuff in the constructor, then goes back up to the Compound.java and draws it using the draw(Graphics g) method....
But I don't know how to fix it! Any help would be appreciated... it's due tomorrow.
Here's all the code:
Shape.java:
import java.awt.*;
public abstract class Shape {
int initX, initY;
Color fillColour;
public Shape() {
initX = 0;
initY = 0;
}
public Shape(int x, int y) {
initX = x;
initY = y;
}
public void setInitX (int x) {
initX = x;
}
public void setInitY (int y) {
initY = y;
}
public abstract void draw(Graphics g);
public abstract double Area();
public abstract double Perimeter();
public void Move(int deltaX, int deltaY){
//future work
}
}
ClosedShape.java :
import java.awt.Graphics;
public abstract class ClosedShape extends Shape {
boolean polygon;
int numPoints;
int[] xVertices;
int[] yVertices;
int x,y,width, height;
public ClosedShape(boolean isPolygon, int numPoints) {
super(0,0);
this.polygon = isPolygon;
this.numPoints = numPoints;
}
public ClosedShape(boolean isPolygon, int numPoints, int[] x, int[] y) {
super(x[0],y[0]);
this.polygon = isPolygon;
if (isPolygon) {
this.numPoints = numPoints;
xVertices = new int[numPoints]; // error check? if x.length == numPoints
for (int i = 0; i < x.length; i++) { // make copy of array: why?
xVertices[i] = x[i];
}
yVertices = new int[numPoints]; // error check? if y.length == numPoints
for (int i = 0; i < y.length; i++) { // make copy of array
yVertices[i] = y[i];
}
}
else { // its an oval - define bounding box
this.numPoints = 4;
this.x = x[0];
this.y = y[0];
width = x[1];
height = y[1];
}
}
public void setXYCoords(int[] x, int[] y){
this.xVertices = x;
this.yVertices = y;
}
// Gives access to the width attribute
public void setWidth(int width){
this.width = width;
}
// Gives access to the height attribute
public void setHeight(int height) {
this.height = height;
}
public void draw(Graphics g) {
if (polygon) {
g.drawPolygon(xVertices, yVertices, numPoints);
}
else {
g.drawOval(x, y, width, height);
}
}
public abstract double Area();
public abstract double Perimeter();
}
Rectangle.java :
public class Rectangle extends ClosedShape
{
public Rectangle(int x, int y, int width, int height)
{
super(true, 4);
setWidth(width);
setHeight(height);
int [] arrayX = new int[4];
arrayX[0] = x;
arrayX[1] = (x+width);
arrayX[2] = (x+width);
arrayX[3] = x;
int [] arrayY = new int[4];
arrayY[0] = y;
arrayY[1] = y;
arrayY[2] = y+height;
arrayY[3] = y+height;
setXYCoords(arrayX, arrayY);
}
public double Area()
{
return 0;
}
public double Perimeter()
{
return 0;
}
}
Compound.java :
import java.awt.*;
import java.awt.Graphics;
public class Compound
{
boolean polygon;
int[] xVertices;
int[] yVertices;
int initX, initY;
Color fillColour;
public void setXYCoords(int[] x, int[] y)
{
this.xVertices = x;
this.yVertices = y;
}
public void draw(Graphics g)
{
if (polygon) {
g.drawPolygon(xVertices, yVertices, 4);
}
else {
g.drawOval(1, 1, 1, 1);
}
}
}
House.java :
import java.awt.*;
import java.awt.Graphics;
public class House extends Compound
{
public House(int x, int y, int width, int height)
{
int [] arrayX = new int[4];
arrayX[0] = x;
arrayX[1] = (x+width);
arrayX[2] = (x+width);
arrayX[3] = x;
int [] arrayY = new int[4];
arrayY[0] = y;
arrayY[1] = y;
arrayY[2] = y+height;
arrayY[3] = y+height;
setXYCoords(arrayX, arrayY);
this.polygon = true;
Rectangle house = new Rectangle(x, y, width, height);
int [] arrayXTwo = new int[4];
arrayXTwo[0] = x+(width/4);
arrayXTwo[1] = x+(2*(width/4));
arrayXTwo[2] = x+(2*(width/4));
arrayXTwo[3] = x+(width/4);
int [] arrayYTwo = new int[4];
arrayYTwo[0] = y+(height/4);
arrayYTwo[1] = y+(height/4);
arrayYTwo[2] = y+height;
arrayYTwo[3] = y+height;
setXYCoords(arrayXTwo, arrayYTwo);
this.polygon = true;
Rectangle door = new Rectangle(x, y, width, height);
}
}
From your sample code, there is no way that to "add" any shapes to the Compound class. From your description, Compound should be a "container" of shapes. Something more along the lines of...
public class Compound
{
private List<Shape> shapes;
public Compound() {
shapes = new ArrayList<Shape>(25);
}
public void addShape(Shape shape) {
shapes.add(shape);
}
public Iterable<Shape> getShapes() {
return shape;
}
public void draw(Graphics g) {
for (Shape shape : shapes) {
shape.draw(g);
}
}
}
Now you need to decide, where is it best to associate the Color with the Shape should it be defined for the shape itself? That means you can't reuse the shape. Or with the Compound shape?

Java: When I Instantiate a Subclass of an Abstract Class It Doesn't Recognize the Constructor of its Superclass

I do not have very much Java experience but I see codes where there is an abstract class with a certain constructor and then a subclass of that abstract class without a constructor. Then when the subclass is instantiated it is constructed with its superclass constructor. Is that right?
I have this abstract class:
public abstract class Tile{
public int x;
public int y;
public int z;
protected Color color;
protected float friction;
protected float bounce;
protected boolean liquid;
public void Tile(int x, int y, int z){
this.x = x;
this.y = y;
this.z = z;
init();
}
abstract protected void init();
And this subclass:
public class TestTile extends Tile{
protected void init(){
color = Color.RED;
friction = 0.1f;
bounce = 0.2f;
liquid = false;
}
}
But when I instantiate a TestTile with this:
Tile tile = new TestTile(0, 0, 0);
the init() method never runs. All of the values defined inside it are null. I tried making what I though might be a redundant constructor in the subclass which just called super with the exact same parameters, but when I did that, even with super(x, y, z) the only statement inside it, it said this:
TestTile.java:27: call to super must be first statement in constructor
I want to make a bunch of subclasses of Tile which implement the properties of a Tile. If this is not the correct way to do that, what is a better way?
I am using 32-bit Ubuntu Linux 11.04 if it has to do with anything.
Thanks.
Your constructor is not in propert constructor format, it's void, make it:
public Tile(int x, int y, int z){
this.x = x;
this.y = y;
this.z = z;
init();
}
I don't see a constructor for TestTime that takes three arguments. I don't see any ctor at all, which means that all you have is the default that the compiler gives you. Did I go too fast and miss it?
I'd recommend paying careful attention to this. I'd rethink this design:
What's wrong with overridable method calls in constructors?
Try this - it includes the fix for your constructor and avoids the issue that the other thread points out:
public abstract class Tile{
public int x;
public int y;
public int z;
protected Color color;
protected float friction;
protected float bounce;
protected boolean liquid;
public Tile(int x, int y, int z){
this.x = x;
this.y = y;
this.z = z;
}
}
public class TestTile extends Tile{
// You're missing this.
public TestTile(int x, int y, int z)
{
super(x, y, z);
this.init();
}
protected void init(){
color = Color.RED;
friction = 0.1f;
bounce = 0.2f;
liquid = false;
}
}
First of all, Tile has only one constructor with the x, y, z parameters, no default constructor, so you have to call super(x, y, z) in the TestTile constructor. As slandau said, the "constructor" has a wrong void return type.
The TestTile needs to declare the parameters or pass default values:
public TestTile(int x, int y, int z) {
super(x, y, z);
}
public TestTile() {
super(0, 0, 0);
}
In Java, there are many riscs to call an abstract method in a constructor, see also here, the instance is not initialized properly. You can only call static methods safe (which will not work here).
public TestTile(int x, int y, int z) {
super(x, y, z);
color = Color.RED;
friction = 0.1f;
bounce = 0.2f;
liquid = false;
}
or you need to call a private method in the derived class (remove the abstract init() from Tile):
public TestTile(int x, int y, int z) {
super(x, y, z);
init();
}
private void init() {
color = Color.RED;
friction = 0.1f;
bounce = 0.2f;
liquid = false;
}
Are you sure members are the right implementation here? Maybe abstract methods (getters) may be better here to declare a behavior and implement it in the subclass?
public abstract class Tile {
public int x;
public int y;
public int z;
public Tile(int x, int y, int z) {
this.x = x;
this.y = y;
this.z = z;
}
public abstract Color getColor();
public abstract float getFriction();
public abstract float getBounce();
public abstract boolean isLiquid();
}
public class TestTile extends Tile {
public TestTile(int x, int y, int z) {
super(x, y, z);
}
public Color getColor() {
return Color.RED;
}
public float getFriction() {
return 0.1f;
}
public float getBounce() {
return 0.2f;
}
public boolean isLiquid() {
return false;
}
}
Constructors are not inherited, so the three-parameter constructor of Tile is not invoked when you create your TestTile object. You need to explicitly call the three-parameter Tile constructor from a TestTile constructor, like you said you did try, but that call to super(x,x,x) must be the first statement of the TestTile constructor.
And like Matt Ball said, your Tile "constructor" isn't really a constructor until you remove the void return type.

Categories

Resources