Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why can a class implement multiple interfaces?

This is the only question about interfaces in oop I can't seem to fully explain. So again, why in oop can a class implement multiple interfaces?

If you can provide a few examples that would be great. thanks in advance.

like image 609
user1664427 Avatar asked Dec 20 '22 05:12

user1664427


1 Answers

Conceptual Example

The way I think about the multiple interfaces is interface is like the verb or adjective, and class is like the subject.

A tiger can run, so the Tiger class may implement Runnable Interface.

A tiger can eat, so the Tiger class may implement Eatable Interface.

Because an instance of the class could have different behaviors, we could have different corresponding interfaces.

Realistic Example

java.util Class HashMap<K,V>

It implements Serializable, Cloneable, Map<K,V>

All of the interfaces are the characteristics of Class HashMap.

like image 153
Mingyu Avatar answered Feb 11 '23 13:02

Mingyu