views:

35

answers:

1

I'm sure this is incredibly common with as OOP centered as Java is. In java is there a way to make a base type variable that accepts all inherited subtypes? Like if I have;

class Mammal {...}
class Dog extends Mammal {...}
class Cat extends Mammal {...}

class ABC {
    private Mammal x;

    ABC() {
        this.x = new Dog();
        -or-
        this.x = new Cat();
    }
}

I need the variable to be able to accept any extended version too, but not in specific one extended kind.

There are some ways that I know, but don't want to use. I could make an attribute for each subtype, then only have the one attribute actually used. Make an array and shove it in there.

Any other ideas or a way to get a "base class" type variable?


Ok since I know using polymorphic duck typing isn't a great idea in Java, but since I don't think I can avoid it. Is the only way to use subclass methods dynamically to re assign a casted version of the varible ie, I get an error with this;

Mammal x;
x = new Dog();
System.out.println(x.getClass());
x.breath();
if (x instanceof Dog) {
  x.bark();
} else if (x instanceof Cat) {
  x.meow();
}

Saying symbol not found, however this works;

Mammal x;
x = new Dog();
System.out.println(x.getClass());
x.breath();
if (x instanceof Dog) {
  Dog d = (Dog) x;
  d.bark();
} else if (x instanceof Cat) {
  Cat c = (Cat) x;
  c.meow();
}

That last one the only way to do it?

+1  A: 
polygenelubricants
Ok thanks that's what I was wondering. But that does leave the question, how do you know if it has the bark or meow method? Just check the type and branch code with an if-then? Or does java do the Ruby/Javascript thing where it duck types?
Axpen
If you wanted to call a method on an object that extends a parent class, and adds a method that the parent class doesn't have, you would have to cast it as the object that it really is or something similar
Falmarri
Thanks I'll keep an eye on it. What I'm wanting to do is an RPG database thing. The method of obtaining the item has the same base principle, but there are differences it how it's obtained; mob drop (mob name), vendor bought (vendor name) or quest reward (quest starter, quest name).
Axpen
@Axpen: There was an RPG-flavored question asked a few months back. Basically it has an interaction method between a pair of objects, but since Java is single-dispatch, the code ended up being very messy. The nice solution was to use visitor pattern. I've since lost the link to this question, but maybe someone else can dig it up for you; it may be relevant to what you're doing.
polygenelubricants
@Axpen: you may also want to look up `enum`, which is _VERY_ powerful in Java. http://download-llnw.oracle.com/javase/1.5.0/docs/guide/language/enums.html ; http://stackoverflow.com/questions/3363120/enumerations-why-when/3363326#3363326
polygenelubricants