comp1020comp comp1020comp - 2 months ago 9
Java Question

Is it possible to call the same method on different object types of same parent class with an ArrayList in Java?

Due to a homework assignment that is supposed to test our understanding of inheritance, I have many different subclasses that all end up with the same instance variables. In each of these subclasses, I have a method

printInfo()
that prints all of the instance variables.
printInfo()
is exactly the same in every class it exists in, but I'm fairly sure that I can't move it to any of the parent classes because some of the instance variables only exist within that child class (aka the class that also contains
printInfo()
). From what I read, I can create an ArrayList that contains every object by using the parent class like this:

ArrayList<Parent> myArrayList = new ArrayList<Parent>;
myArrayList.add(new Child1());
myArrayList.add(new Child2());


My problem is that I want to be able to call
printInfo()
on each object in this ArrayList. I tried to use a for loop like this:

for(int i = 0; i < myArrayList.size(); ++i) {
myArrayList.get(i).printInfo();
}


but it won't recognize my
printInfo()
method because it only exists in the subclasses and not the parent class that the ArrayList uses. I'm assuming this is because the ArrayList contains objects of the Parent class so it won't call any member functions of the child class. Is there any way that I can call the
printInfo()
method using an ArrayList (or a similar data structure)?

NOTE: I'm aware that this program is poorly designed considering that there are multiple subclasses with identical methods, but I have been told this is exactly what is expected from the assignment. I don't need advice on how to restructure the classes, I'm only interested in finding a way to call the same method efficiently on multiple objects of different classes.

Answer

I have a method printInfo() that prints all of the instance variables. printInfo() is exactly the same in every class it exists in, but I'm fairly sure that I can't move it to any of the parent classes because some of the instance variables only exist within that child class (aka the class that also contains printInfo()).

You don't need to move the method if they all contain that method. You need to override it in each of the children. It doesn't matter if some variables don't exist in the parent, just print the variables you do have. The contents of the methods are intended to be different. That's the assignment - to understand inheritance and method overriding.

If you need to print the parents variables, you can call super.printInfo() in the Child classes

For example, if both the parent and child have some instance variable of the same name, then the child class is "shadowing" the variable of the parent. Should be no need to copy anything.

but it won't recognize my printInfo() method.

This is unclear. You've said that all the classes have that method, so it should resolve

because it only exists in the subclasses and not the parent class that the ArrayList uses

I suppose you could do this. Not sure, though. I've been bad at messing with inheritance like this because I've haven't had much experience with these designs.

Parent p = myArrayList.get(i);
if (p instanceof Child1) {
    ((Child1) p).printInfo();
}