Java interfaces vs Swift protocol

Guide for java or swift programmers who want to learn the other language.

Introduction

Java interface is a set of methods without their implementation. Concrete implementations of those methods are inside class, which implements the interface. Interface can be called a completely abstract class. Additionally, it can contain constants, default methods (which have their method bodies inside interface) and static methods.
Swift protocol main feature is the same as interface. It defines a blueprint for methods, properties and other requirements that suit a particular task or piece of functionality. There are some differences which are described below.

Defining interface and protocol

In Java language to define interfaces we use interface keyword. Below we can see example of Bicycle interface with three void methods, each taking integer argument and last one returning integer value.

text
Code listing 1

In Swift language we use protocol keyword. Below we can see example of protocol with the same methods as in Bicycle interface.

text
Code listing 2

Properties

Variables are static and final by default in Java interface. They are static because Java interfaces cannot be instantiated in their own right. The value of the variable must be assigned in a static context in which no instance exists. The final modifier ensures the value assigned to the interface variable is a true constant that cannot be re-assigned by program code.

text
Code listing 3

Otherwise protocols can require any conforming type to provide an instance property or type property with a particular name, type and to determine if it is gettable and settable or only gettable. They are declared with var keyword prefix, name, type and { get set } after their type declaration. The rule is that a { get } protocol property may be settable, but a { get set } protocol property must be settable.

text
Code listing 4

As a type

In both languages we can use interface/protocol anywhere we can use any other data type name, including parameter and return type of a method, type of constant, variable, property and type of items in containers.
Additionally, in Swift we can require a type to conform to two or more protocols. It will behave as a temporary local protocol that has the combined requirements of all protocols in the composition. Below we can see an example of such a composition.

text
Code listing 5

Default implementation

We can use default methods in Java. They allow us to define method’s body inside interface. With this we can avoid implementing methods that could be the same in many implementations. If there is the same method definition in a class which implements interface, it would override default method from its interface.

text
Code listing 6

In Swift in order to add default implementation we must use protocol extensions. Example of this is shown below. Just like in Java if a conforming type provides its own implementation of a required method or property, that implementation will be used instead of the one provided by the extension.

text
Code listing 7

Optional requirements

Every method (except default methods) in an interface must be implemented in this interface implementation, but the implementation can be left either blank or it can throw e.g. UnsupportedOperationException. This is a big difference in Swift, because there are two ways to accomplish optional requirements. First is to use optional keyword in front of a method name. With this we don’t need any default implementation, but it comes with a disadvantage. Protocol with optional methods can be implemented only in classes that inherit from NSObject. Another thing is
having to check if optional method was implemented each time, we call it. Second option is to use default implementations provided in protocol extension. By doing this we can implement protocol in any class, struct and enum. We also don’t need to check if implementation is provided. The disadvantage to this solution is that if we have not void returning method, we have to return a reasonable default value.

Conclusion

We learned about major similarities and differences of interfaces and protocols. They are both a fundamental feature of their programming language and need to be deeply understood. To learn more about interfaces please visit official Java documentation https://docs.oracle.com/javase/tutorial/java/IandI/createinterface.html and to learn more
about protocol here is official Swift documentation https://docs.swift.org/swiftbook/LanguageGuide/Protocols.html

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x