-1

If I have a inheritance class like

class Animals{}
class Dog extends Animals{}
class Cat extends Animals{}

How I can know which type of class are? (I mean, if a want know if one object below to dog or cat)

I have an ArrayList of Animals who contain Cats and Dogs. I save this information in a file, for later load the information. But the problem is when I want load the information I need to know which type of class is, for call the dog or cat constructor (cat and dogs have different constructor parameters).

So one of my solutions is store the type of object for when I load the information call the correct constructor. Is any better solution for this?

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
Jota_sk
  • 169
  • 2
  • 14
  • possible duplicate of [Check if a subclass is an instance of a class at runtime in Java?](http://stackoverflow.com/questions/2410304/check-if-a-subclass-is-an-instance-of-a-class-at-runtime-in-java) – BartoszKP Apr 11 '15 at 20:55
  • 1
    @Beko: the `instanceof` operator is considered to be bad OO-design, because it is not scalable. – Willem Van Onsem Apr 11 '15 at 20:58
  • 2
    `instanceof` just won't work here. He's trying to find the instance of data stored in a file... – Obicere Apr 11 '15 at 20:59

3 Answers3

2

I think you want to reimplement the serialization of java. Instead of that

  1. you should make the class Animals to implement the Serializable marker interface
  2. simple write to and then read from the file your array like described here
Laszlo Hirdi
  • 1,140
  • 12
  • 18
1

Since you are saving the information, you can also print out the type.

For example, you can use something like:

for (final Animals animal : myAnimalList){
    output.print(animal.getClass().getSimpleName());
    output.println(animal.getData());
}

For instances of Dog, this will print out "Dog" and for instances of Cat, it will print out "Cat". Followed by the getData() information. This is assuming that getData() would contain the appropriate data for printing out the information.

Then you can achieve something like:

Dog[name=Ruff, age=17, favoriteToy=Ball]
Cat[name=Tom, age=102, hates=Everyone]

Then, you can easily fill in the information required to reconstruct dogs and cats, based off of the required fields.

That being said, you can then write this out to the file. This is, as suggested in your question, similar to the way you are already doing this.

This has already been done before, so creating your own methods and all that is not recommended.


The alternative is to have a unique identifier for each class (think serialVersionUID).

This is just a form of Serialization. Having basic read and write methods can then handle parsing the correct data on a class-to-class basis.

It would be far better to implement Serializable for the Animals class, then sent fields you don't want to be written and read to transient.

Then, you can just fill all the appropriate methods with these signatures:

private void writeObject(java.io.ObjectOutputStream out) throws IOException
private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException;
private void readObjectNoData() throws ObjectStreamException;
Obicere
  • 2,999
  • 3
  • 20
  • 31
  • What's wrong with *Java's default serializer*? No identifiers you have to care for, it handles the entire process all by itself. – Willem Van Onsem Apr 11 '15 at 21:07
  • 1
    @CommuSoft nothing, that is why I said creating your own is a bad idea and that you should use the serializer.... – Obicere Apr 11 '15 at 21:08
-1

use getClass() for example:

Animal dog = new Dog();
if (dog.getClass().equals(Dog.class)) {
    System.out.printf("Its dog!");
}

Or as Beko pointed out

Animal dog = new Dog();
if (dog instanceof Dog) {
    System.out.printf("Its dog!");
}
almeynman
  • 7,088
  • 3
  • 23
  • 37
  • First of all, `.getClass()` and `instanceof` behave differently, a class `BlackDog extends Dog` would fail the first `if` and succeed the second, and furthermore using such tests is in general bad design. – Willem Van Onsem Apr 11 '15 at 21:03
  • @ChiefTwoPencils: well the question is vague on that part. It is not entirely clear if the OP has written some kind of parser him/herself that parses the file into an `ArrayList`, if one however wishes to perform different operations for `Cat`s and `Dog`s one should use a virtual method. In case one wishes to store/load to/from files, the *Java Serializer* is the best option. – Willem Van Onsem Apr 11 '15 at 21:06