|
|
|
|
|
by Someone
2854 days ago
|
|
Equivalent, but not exactly the same, mostly in the sense that Java is less dynamic than Swift. Ones I know of: - In Java, you have to declare the interfaces that a class implements when you declare the class. In Swift, you can add conformance of a class to a protocol, even if you don’t have access to the class’s source code. - In Swift, protocol definitions can contain implementations of methods (I think this is being or has already been added to Java recently). For example, a method foo that takes a string argument could call the foo overload taking a character for each character in the string. That way, classes conforming to the protocol need not define the method taking a string. - Even if the protocol definition doesn’t declare that default implementation, you can add one by writing an extension method. All of those are useful, but also can make it hard to understand what code exactly is being called. (More info at https://docs.swift.org/swift-book/LanguageGuide/Protocols.ht...) |
|
Technically, no, they can't. What you do is write your protocol as usual, then extend the protocol with a default implementation.