Given I have this class:
class Animal
{
void walk() { }
....
.... //other methods
}
Coupled with the interface for animals who are able to chew:
interface Chewable {
void chew();
}
and have Reptile class implement this and not Birds (since Birds cannot chew) :
class Reptile extends Animal implements Chewable { }
and in case of Birds:
class Bird extends Animal { }
Here, reptiles and birds are both an interface of Animal.
If I had a case where I have to loop through a list of IAnimals, and call their chew() function, what will happen when there is a bird in the list ?
CodePudding user response:
The class Animal
is not implementing the Chewable
interface and therefore if you have a List of type Animal
containing both Birds and Reptiles it will not have a chew()
method. This is because the type Animal
only "knows" of its own members and methods.
CodePudding user response:
If you loop through a List of Animal (List), you could only call methods belonging to Animal. Therefore you can not call chew().