Reputation: 325
Let's assume I have a super class called animal
, and three sub classes respectively called Dog
, Cat
, and Lion
that extend the animal
class.
Then, I create an object each for the sub class and add them into an ArrayList<animal>
.
When I am going through that list, how can I distinguish between a Dog
or a Cat
object as every object in the ArrayList
is of type animal
?
Upvotes: 1
Views: 3183
Reputation: 5661
You should use instanceof
operator:
if (animal instanceof Dog) {
//something
} else if (animal instanceof Cat) {
//something
} else if (animal instanceof Lion) {
//something
}
However, you should think twice if you really need to use it. In most cases instanceof
is a bad OOP practise.
Upvotes: 7
Reputation: 25874
What Chief Two Pencils is trying to say in his comments is that you can use instanceof
but in your situation there is a much better OOP design pattern to be used. For example, instead of distinguishing the object type, why don't you simply have a base method in Animal
and override in each subtype? For example, apply
. This way you can just call animal.apply()
and the correct method for the type will apply.
Here's an example of what he means:
import java.util.ArrayList;
import java.util.List;
abstract class Animal {
abstract void apply();
}
class Dog extends Animal {
@Override
void apply() {
System.out.println("Bark bark!");
}
}
class Cat extends Animal {
@Override
void apply() {
System.out.println("Meeeeeow!");
}
}
class Lion extends Animal {
@Override
void apply() {
System.out.println("Rrrrrroar");
}
}
public class Main {
public static void main(final String[] args) {
final List<Animal> animals = new ArrayList<>();
animals.add(new Dog());
animals.add(new Cat());
animals.add(new Lion());
// Dog
animals.get(0).apply();
// Cat
animals.get(1).apply();
// Lion
animals.get(2).apply();
}
}
As you can see, I didn't need to check which type it for the correct behavior to be invoked for each animal. This is what polymorphism is all about.
Upvotes: 1
Reputation: 565
Use instanceof to differentiate between different object types in the class hierarchy
import java.util.ArrayList;
import java.util.List;
public class Inheritance {
public static void main(String[] args) {
//create a list to hold all animals
List<Animal> animals = new ArrayList<Animal>();
//add animals
animals.add(new Animal());
animals.add(new Dog());
animals.add(new Cat());
for (Animal a:animals) {
//Use instanceof to determine type of object
if(a instanceof Dog){
Dog dog = (Dog)a;
dog.displayDog();
}else if (a instanceof Cat) {
Cat cat = (Cat)a;
cat.displayCat();
}else{
a.display();
}
}
}
}
class Animal{
public void display(){
System.out.println("This is a Animal");
}
}
class Dog extends Animal{
public void displayDog(){
System.out.println("This is a Dog");
}
}
class Cat extends Animal{
public void displayCat(){
System.out.println("This is a Cat");
}
}
Upvotes: -2
Reputation: 11733
In Java you would have to use instanceof. You will find people discourage the use of instance of. Most people consider it a code smell. Why? Because the whole reason to use a typing system is so that you can treat different types as if they were the same. So in other words, if you collect a bunch of different types, based on their having similarities embodied in your base class, you should be satisfied with accessing each through that base class reference. If you are writing code that is checking each type and then doing different things, that means your type system is leaky.
For instance, consider some operation you might want to perform. Maybe you have to feed each animal (in a rescue center). You would define an operation in the base class like 'likes to eat' that would indicate what that animal wants. Each subclass would provide its own implementation of that method. But, from your main code, you could just iterate through the collection and call the method from the base class reference, not knowing the details of each. In this scenario, your model has afforded extension. Remember the adage: open to extension, closed to modification (attributed to Bertrand Meyer).
If you just throw a bunch of related objects into a collection and decide 'well I'll figure out what to do with each given the circumstances I find,' then you are not following this principle.
Upvotes: 0
Reputation: 5019
The point of inheritance is that you don't need to know what the type of animal each object, when you call method that have to animal object then each subClass can implement this method as you want.
If you anyway need to know what the type you can use the keyword instanceof, but if you need to use it probably you did something wrong as OOP programing
Upvotes: 3