panda
panda

Reputation: 11

Interfaces in Java

So I have a class cat which implements a interface Vocal, I was wondering why when I created a ArrayList(Vocal) I was able to add instances of cat to it?

Thank You

Upvotes: 1

Views: 147

Answers (3)

gmhk
gmhk

Reputation: 15940

In your case, the relationship will be exactly the same

Cats IS A Vocal too.

So that was the reason you are able to add the arraylist of CATS in Vocal Type Arraylist

Upvotes: 0

Ted Hopp
Ted Hopp

Reputation: 234795

Yes. You will be able to add an instance of any class that is declared to implement Vocal.

Upvotes: 1

Cameron Skinner
Cameron Skinner

Reputation: 54276

It's because when Cat implements Vocal it's an IS-A relationship: Cat IS-A Vocal.

Since the List<Vocal> accepts anything that is Vocal, adding an instance of Cat is perfectly legal.

Upvotes: 6

Related Questions