1

I have a class called WallTile

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;

public class WallTile extends Tile
{
    private int x;
    private int y;
    private int id=1;
    private ImageIcon image;
    private String[] flags=new String[]{"[IMPASSABLE]", "[SIGHT_BLOCKER]"};
    public WallTile(int x, int y)
    {
        this.x=x;
        this.y=y;
        if((int)(Math.random()*2)==0){
            this.image=Sprites.WALLTILE1_SP;
        }
        else{
            this.image=Sprites.WALLTILE2_SP;
        }
    }

}

which inherits from class Tile

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;

public class Tile
{
    private int x;
    private int y;
    private int id;
    private ImageIcon image;
    private String[] flags;

    public int getX(){
        return(this.x);
    }
    public int getY(){
        return(this.y);
    }
    public int getId(){
        return(this.id);
    }
    public ImageIcon getImage(){
        //System.out.println(this.image);
        return(this.image);
    }
    public String[] getFlags(){
        return(this.flags);
    }
    public boolean testFlag(String test){//returns true if flag is not present
        return(java.util.Arrays.asList(this.flags).indexOf(test)==-1);
    }
}

creating an instance of wallTile and calling a method defined in Tile always returns null e.x:

WallTile wall = new WallTile(3,7);
System.out.println(wall.getImage());//returns null

However, if I copy one of the functions from Tile and paste it into WallTile, that function returns the correct value.

Is there any to call the functions defined in tile with an instance of WallTile without having to copy all of the functions defined in tile over to WallTile?

  • 1
    You set image in the WallTile constructor. Unfortunately, you have shadowed the Tile image, so getImage(), which is defined in Tile, gets the Til3.image rather than the WallTile.image. – NomadMaker Jan 30 '21 at 03:25
  • Try add this code : [Problem Source](https://stackoverflow.com/questions/65035/does-a-finally-block-always-get-executed-in-java?rq=1) – Zeta Jan 30 '21 at 03:26
  • @NomadMaker what do I need to do to make getIamage return the WallTile image instead of the Tile image? – ELLIOT WHITE Jan 30 '21 at 03:33
  • 1
    Redefine Tile to allow you to write to Tile.image and remove the image variable from WallTile. – NomadMaker Jan 30 '21 at 03:37
  • 1
    Either override `getImage()` or set the field in the superclass rather than creating a field in the subclass. The latter option is better, in my opinion. – Slaw Jan 30 '21 at 03:37
  • 1
    This is an aside, but if you declare properties on both the parent and the child, then objects will have both properties and calling super methods will interact with different values than the child's methods. Unless this is the intended goal, you would be better off not redeclaring the properties on WallTile, supplying getter methods at the parent that the child can use or giving the properties appropriate access modifiers (default, presumably) so that the child can access them in it's own methods. – msg45f Jan 30 '21 at 05:27

2 Answers2

1

Simple fix. In Tile

public class Tile
{
    ...
    protected ImageIcon image;
    ^^^^^^^^^

change image to protected.

Then in WallTile remove the declaration of image, which was shadowing the declaration in Tile. protected will grant WallTile access to its image member.

Jim Garrison
  • 85,615
  • 20
  • 155
  • 190
0

Tile

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;

public class Tile
{
    private int x;
    private int y;
    private int id;
    private ImageIcon image;
    private String[] flags;
    
    public Tile(int x, int y, int id, ImageIcon image, String[] flags){
        this.x=x;
        this.y=y;
        this.id=id;
        this.image=image;
        this.flags=flags;
    }
    
    public int getX(){
        return(this.x);
    }
    public int getY(){
        return(this.y);
    }
    public int getId(){
        return(this.id);
    }
    public ImageIcon getImage(){
        //System.out.println(this.image);
        return(this.image);
    }
    public String[] getFlags(){
        return(this.flags);
    }
    public boolean testFlag(String test){//returns true if flag is not present
        return(java.util.Arrays.asList(this.flags).indexOf(test)==-1);
    }
}

WallTile

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;

public class WallTile extends Tile
{
    private int x;
    private int y;
    private int id=1;
    private ImageIcon image;
    private String[] flags;
    
    public WallTile(int x, int y)
    {
        super(x,y,1,Sprites.WALLTILE1_SP,new String[]{"[IMPASSABLE]", "[SIGHT_BLOCKER]"});
        this.x=x;
        this.y=y;
    }

}