12

I am little bit messed up with the following concept:

Code 1 :

class New{
    func abc(){
        print("new class")
    }
}

class ClassNew: New {
    override func abc() {
        print("derived class")
    }
}

Code 2:

protocol New{}

extension New{
    func abc(){
        print("new protocol")
    }
}

class ClassNew: New {
    func abc() {
        print("derived protocol")
    }
}
  1. What is the difference between Code 1 and Code 2 as both of them serves the same purpose?

  2. In code 2, classNew is inheriting from new protocol or just conforming to the protocol?

Any Explanation will be highly appreciated!

4

4 Answers 4

15

Code 1 and code 2 are fundamentally not the same.

What is the difference between Code 1 and Code 2 as both of them serves the same purpose?

No they don't. The first one defines a class hierarchy, the second defines a protocol (an API, if you like) and a type that conforms to it.

In code 2, classNew is inheriting from new protocol or just conforming to the protocol?

It's conforming to the protocol. There's no inheritance involved (if you are being pedantic).


Code 1 defines a base class and a class that inherits from it. The subclass overrides the base class for the function abc() and it behaves as you would expect given a class hierarchy i.e.

let x: New = ClassNew()
let y: ClassNew = ClassNew()

print(x.abc()) // prints "derived class"
print(y.abc()) // prints "derived class"

Both print statements call the derived class's abc()

In code 2 you define a protocol with no methods, and an extension to the protocol with an extension method. Note that this is not a "default method" there is nothing in the protocol to default. You then define a class that conforms to the protocol and adds a new method that happens to have the same name as the extension method. The distinction (from the pure class hierarchy) is important because the version of abc() called is determined statically at compile time

protocol New2{}

extension New2{
    func abc(){
        print("new protocol")
    }
}

class ClassNew2: New2 {
    func abc() {
        print("derived protocol")
    }
}

let y2: ClassNew2 = ClassNew2()
let x2: New2 = y2

print(x2.abc()) // prints "new protocol"
print(y2.abc()) // prints "derived protocol"

Even though x2 and y2 are the same object different versions of the function are called. This is because the compiler is not allowed to assume anything about x2 except what it can infer from the protocol. So it doesn't know that the object has anabc() of its own so it must call the extension function.

If you had defined the protocol like this:

protocol New3{
    func abc()
}

extension New3{
    func abc(){
        print("new protocol")
    }
}

class ClassNew3: New3 {
    func abc() {
        print("derived protocol")
    }
}

let y3: ClassNew3 = ClassNew3()
let x3: New3 = y3

print(x3.abc())  // prints "derived protocol"
print(y3.abc())  // prints "derived protocol"

This time the compiler knows that the object should have a function abc() and will only use the extension function if it doesn't. Then the normal inheritance rules for classes apply.

2

Code 2 is an example of protocol extensions. Big difference is that you can't call super.abc() in Code 2 — you have to provide a method implementation that is not in any super context.

Just my opinion — do not use protocol default implementation, as compiler won't save you if you forget to provide "override" when you really need one.

1
  • It is not a default implementation. The protocol declares no function to default.
    – JeremyP
    Sep 4, 2017 at 14:23
1

classNew is simply conforming to the protocol in the second code. This is true to every protocol in Swift as inheritance is not currently supported with protocols.

This also answers your first question: there is no significant difference between the two codes in this use case, but, generally speaking, a protocol is good for other kinds of things than subclassing.

5
  • But implementing protocols is a way of implementing multiple inheritance then how it's not true in case of single inheritance? Sep 4, 2017 at 11:40
  • 2
    @ShubhamMishra protocols are nothing to do with inheritance at all.
    – Fogmeister
    Sep 4, 2017 at 11:46
  • See this link @Fogmeister : stackoverflow.com/questions/41054396/… Sep 4, 2017 at 11:48
  • 1
    @ShubhamMishra I fail to see he relevance. And if you read the accepted answer it stated that protocols can be “exploited to model the idea of multiple inheritance that we are used to”. It is not inheritance in itself. It is about conformance. Not inheritance.
    – Fogmeister
    Sep 4, 2017 at 11:51
  • There is a significant difference between the two fragments. Try assigning an instance of the class to a variable of type New and calling abc()
    – JeremyP
    Sep 4, 2017 at 14:22
1

Code 1 and Code 2 are exactly same, just conceptually different.

If we put it in simple words, a Class defines the object itself, and a Protocol defines the behaviour of the object.

If compared to Java, a protocol is similar to Interfaces.

Checkout the Protocol Documentation

1
  • No they are not the same.
    – JeremyP
    Sep 4, 2017 at 14:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.