0

I'm new to Java and I'm currently learning about abstract methods and they sound exactly like Protocol methods in Swift. They are both methods that must be implemented and cannot have a default implementation.

However, when I Google, "Are Java abstract methods the same as Swift Protocol methods," all of the answers basically say that Swift doesn't offer abstract methods.

So my questions are:

  1. Am I right to assume that an abstract method is at least similar to a protocol method?
  2. What is the key difference between the two?
John R Perry
  • 3,916
  • 2
  • 38
  • 62
  • 3
    Did you read this https://stackoverflow.com/questions/30859334/compare-protocol-in-swift-vs-interface-in-java ? – Martin R Jun 05 '17 at 08:10
  • 1
    Don't know why that comment is getting so many upvotes. An interface can declare methods with a default implementation which is a key feature that `Procotol`s cannot do. – John R Perry Jun 05 '17 at 08:15
  • 2
    @MartinR I believe OP is talking about `abstract` methods rather than `interface`s. Swift doesn't have a direct equivalent for `abstract` classes/methods. – Hamish Jun 05 '17 at 08:15
  • 1
    @Ryan Protocol requirements *can* have default implementations by way of protocol extensions. – Hamish Jun 05 '17 at 08:18
  • I just assumed the same could be done with Java but can you explain what makes them different besides that? – John R Perry Jun 05 '17 at 08:23
  • I added the link *assuming* that it might be helpful to OP, not as a possible duplicate. I apologize if that was misleading. – Martin R Jun 05 '17 at 08:26
  • Also, that question says that `interface`s are similar to `Protocol`s where questions about `abstract` methods and Swift get answers like yours, that their is not a Swift equivalent. – John R Perry Jun 05 '17 at 08:28
  • Possibly helpful: https://stackoverflow.com/questions/30943209/is-there-a-difference-between-swift-2-0-protocol-extensions-and-java-c-abstract. – Martin R Jun 05 '17 at 08:29
  • @MartinR thank you. The second link does make this question a duplicate. – John R Perry Jun 05 '17 at 08:30

0 Answers0