Summary

We had a pretty detailed look at Java interfaces and got to know that interfaces are contracts. When a class implements the contract, we can reuse the existing code. The iterator pattern is an example of such a contract. By following the interface iterable's contract, we were able to reuse the Java code for each loop with our own custom class.

When a language supports multiple inheritances, the method resolution might get ambiguous. Java does not support multiple inheritance, hence, we don't see this problem.

Scala traits resolve many of these problems. We can put in reusable code in traits and then mix-in traits into our classes or objects as needed. We looked at how mix-ins enable rich interfaces. Traits are stackable and allow us to change ...

Get Scala Functional Programming Patterns now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.