So, for example, let's say I have this class named for example "Animals", a subclass called "Dogs" and other one called "Cats" and a class for running the program.
class Animals{
int size;
String name;
}
class Dogs extends Animals{
void bark(){
System.out.println("Woff");
}
}
class Cats extends Animals{
void meow(){
System.out.println("Meow");
}
}
And in the class Test I want to create an array that holds both of them. How should I declare them so I can also access to their methods? I was thinking, for example, in something like this:
class Test{
public static void main(String[] args){
Animals [] array = new Animals[2];
array[0] = new Dogs();
array[1] = new Cats();
array[0].bark();
}
}
But it doesn't seem to work that way since Java recognizes the method as a Animals' method.
AnimalnotAnimals.