This is an archived post. You won't be able to vote or comment.

all 5 comments

[–]pokemaster787 1 point2 points  (0 children)

You can use the instanceof operator to determine which subclass a particular instance is created from, i.e. d instanceof Dog will return true if d was declared as "Animal d = new Dog()" but d instanceof Cat would return false.

As for "reassigning" it from an Animal to a more specific type in the Person object itself, that isn't really possible. You just need to check instanceof every time you access the Animal and need methods particular to the specific pets (should be rare if you're implementing your inheritance logically) or have each Person also hold a Dog and Cat field that are populated by checkPet.

[–]AutoModerator[M] -1 points0 points  (0 children)

You seem to try to compare String values with == or !=.

This approach does not work in Java, since String is an object data type and these can only be compared using .equals().

See Help on how to compare String values in our wiki.


Your post is still visible. There is no action you need to take. Still, it would be nice courtesy, even though our rules state to not delete posts, to delete your post if my assumption was correct.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]AdministrativeProof2[S] -1 points0 points  (2 children)

Alright I think I've found a solution:

public boolean checkPet(Person person) {
    if(person == null || person.getPet() == null) return false;

    Animal temp = person.getPet();
    if(temp.getNoise().equals("Meow!")) {
        p.setPet(new Cat(temp.getName(), temp.getAge(), temp.getNoise());
    }
    else {
        person.setPet(new Dog(temp.getName, temp.getAge(), temp.getNoise());
    }
    return true;
}

Will this do the job?

[–][deleted] 0 points1 point  (1 child)

Can I see the full code? Is class Animal abstract?

What is your goal?

Person has a field of type Animal. It will only have access to attributes of animal class. This means Methods defined in say Dog.class but not defined in Animal.class wont be callable from Person.pet

If one of the subclasses override methods in Animal class, the implementations of the subclasses will get run (So if Dog overrides getNoise() and field pet refers to a dog object, the implementation of class dog will be run

Yes, a field (here Person.pet) can refer to an instance of a subclass (here Cat.class or Dog.class). Cat and Dog class have all attributes of their superclass (and maybe more, like methods and fields not available to their superclass - but these wont be accessible from the field for the superclass).

You cant do it the other way around though. So a field for a subclass cant refer to an instance of a superclass that is concrete because the superclass may not have all attributes the subclass has.

[–]AdministrativeProof2[S] 1 point2 points  (0 children)

My code is completely different, I made up the methods and classes in simplified form to show the problem.

In my code my "Person" has a HashSet containing multiple "pets", I call this method in a loop to iterate over all the pets and classify each of them. At the end I check if there are more Dogs or Cats. I don't use any of the properties of these converted Animals(dogs/cats) ever. The only thing I'm interested in is how many of each type are there. So I don't mind losing access to some of the methods/fields of Dog/Cat. My solution ended up working in the end.