Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare Protocol in Swift vs Interface in Java

Tags:

java

swift

People also ask

What is the difference between a protocol and an interface?

An interface defines how two entities may communicate. A protocol defines how they should communicate and what that communication means.

What is difference between protocol and class Swift?

You can create objects from classes, whereas protocols are just type definitions. Try to think of protocols as being abstract definitions, whereas classes and structs are real things you can create.

What are the protocols in Swift?

Protocols provide a blueprint for Methods, properties and other requirements functionality. It is just described as a methods or properties skeleton instead of implementation. Methods and properties implementation can further be done by defining classes, functions and enumerations.

Are there interfaces in Swift?

Interfaces are same as protocol in Swift. class and struct both can implements the protocol .


Essentially protocols are very similar to Java interfaces except for:

  • Swift protocols can also specify properties that must be implemented (i.e. fields)
  • Swift protocols need to deal with value/reference through the use of the mutating keyword (because protocols can be implemented by structures, enumerations or classes).
  • you can combine protocols at any point using "Protocol Composition". This replaces the older swift protocol<A, B> way of protocol composition. For example, declaring a function parameter that must adhere to protocol Named and Aged as:
    func wishHappyBirthday(to celebrator: Named & Aged) {}

These are the immediately apparent differences for a Java developer (or at least what I've spotted so far). There's more info here.


Complementing @Thomas Schar's answer. The Swift protocol magic comes from the extension.

  • Swift protocols can get implementations via the extension (Swift
    2). Java 8 interface can have default implementations, but it cannot be done "retroactively."
  • In Swift, you can "retroactively" add protocol requirements (and
    its implementations if needed) to any class or structure.
  • Swift protocols do not follow the generic (i.e <..>) customization pattern, but a typealias scheme (i.e. Associated Types). Can be confusing at the start, but can avoid
    "angle bracket blindness" in some cases.
  • Swift has an advanced type pattern matching, allowing to be very specific on where and how protocol requirements and extensions get applied. It can be confusing when coming from Java, but it has a lot of power.
  • A swift protocol can be composed for a property/param (i.e. celebrator: protocol)

One thing that got me scratching my head for a couple of hours is that not all protocols can be used as property types. For example, if you have a protocol with typealias, you cannot directly use it as a type of property (it makes sense when you think about it, but coming from Java we really want to have a property like userDao: IDao).