Why we use interface in java? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Why we use interface in java?

29th Nov 2016, 3:26 AM
Sagar Maharana
Sagar Maharana - avatar
3 Answers
+ 2
mostly because Java doesn't support multi inheritance tu avoid the the diamond of death caused by circular dependency if not checked but also because if you use inheritance you can set a contract for anyone intending to use your class should abide by, by setting behavior you expect to be overridden by anyone using your interface, it provides structure
29th Nov 2016, 3:50 AM
Eric Gitangu
Eric Gitangu - avatar
+ 2
what is diamond of death?
29th Nov 2016, 4:09 AM
Sagar Maharana
Sagar Maharana - avatar
+ 2
if class a depends on class b, class b depends on class c and class c depends on class a, this could lead the OS into a state of deadlock since we have circular dependency, in this situation the outcome is not known but to prevent against this multiple inheritance is not allowed in Java, not this is a common phenomena in C since this permitted.
29th Nov 2016, 4:16 AM
Eric Gitangu
Eric Gitangu - avatar