0

I have many classes and some of them implements interfaces, and I have an ArrayList contains some objects created from those classes. How can I make them checked if they implement any interface?

  • The `instanceof` operator works with interface types, but before you jump on that, why don't you know already? If it is important that the elements of your list implement a particular interface, then the list's type parameter should enforce that. – John Bollinger Mar 29 '22 at 17:53
  • Oh I got the point, thanks a lot.. – asdasfazamasqwexyraz Mar 29 '22 at 18:04
  • As Bollinger commented… If `Cat` and `Dog` classes implement `Animal` interface, then: `List< Animal > animals = List.of( new Dog( "Rover" ) , new Cat( "Fluffy" ) ) ;` – Basil Bourque Mar 29 '22 at 18:54

0 Answers0