Reputation: 145
I'm trying to check if class A that is extending class B is instance of certain interface.
Basically here is my setup:
public class House
{
(variables)
public House(variables) {...}
}
public class Kitchen extends House implements IFurniture
{
(variables)
public Kitchen(variables)
{
super(variables);
}
}
public class Bathroom extends House implements IWalls
and so on...
And I have a method where I'm getting a casted, House version of Kitchen and Bathroom. I basically want to do this:
public boolean hasFurniture(House house)
{
if (house instanceof IFurniture){return true;}
}
Do I need to use house.getClass().getInterfaces() and would that even work considering that the argument has been casted?
The problem is that I'm writing a mod for a game and I can't edit games classes. Only only Kitchen, Bathroom and hasFurniture can be edited in this case.
Also ignore my class names, they are just example names so that you don't confuse Classes accidentally.
Upvotes: 1
Views: 337
Reputation: 145
I just solved it, thanks to Eran.
My problem was that I was casting a wrong element. In this case I was casting ItemStack (Item with additional data) instead of Item. By adding just .getItem() I have hopefully solved the problem.
Thank you for helping, have a nice day!
Upvotes: 0
Reputation: 7032
Let's work with a more understandable class hierarchy. You're thinking about the instanceof issue like this:
public class Person{
public void die(){
System.out.println(this + " died");
}
public void pushOffCliff(){
if(this instanceof Flyable)
Flyable f = (Flyable)this;
f.fly();
else
die();
}
}
public interface Flyable{
public void fly();
}
public class Superhero extends Person implements Flyable{
public void fly(){
System.out.println("Up, up, and away!");
}
}
When it makes much more sense to use the class extension and overriding system like Patrick does in his answer, like below. People (that are truly just people and not superheroes) don't fly. When pushed off a cliff, they should die. So the person class shouldn't deal with the "what if I'm not truly a person, I'm actually an ...." possibility; let the sub classes deal with that.
public class Person{
public void die(){
System.out.println(this + " died");
}
public void pushOffCliff(){
die();
}
}
public interface Flyable{
public void fly();
}
public class Superhero extends Person implements Flyable{
public void fly(){
System.out.println("Up, up, and away!");
}
@Override
public void pushOffCliff(){
fly();
}
}
Upvotes: 1
Reputation: 10584
You're wasting the benefits of polymorphism. You don't want to use an interface with instanceof
to check if an object has a property. implements
and extends
mean IS-A, not HAS-A. You clearly want the latter. Do it like this instead:
public interface IFurniture {
public boolean hasFurniture();
}
public class House implements IFurniture
{
(variables)
public House(variables) {...}
@Override
public boolean hasFurniture() {
return false;
}
}
public class Kitchen extends House
{
(variables)
public Kitchen(variables)
{
super(variables);
}
@Override
public boolean hasFurniture() {
return true;
}
}
EDIT: Also, Eran is right to point out that this hierarchy doesn't make a huge amount of sense in the first place -- but that's a larger issue.
Upvotes: 1