Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inheritance in Class Vs Protocol

Tags:

swift

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!

like image 956
Shubham Mishra Avatar asked Sep 04 '17 11:09

Shubham Mishra


People also ask

Can a class protocol be inherited?

One protocol can inherit from another in a process known as protocol inheritance. Unlike with classes, you can inherit from multiple protocols at the same time before you add your own customizations on top. Now we can make new types conform to that single protocol rather than each of the three individual ones.

What is the difference between protocol and class?

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.

CAN protocol inherit from class Swift?

A Swift protocol can inherit from other protocols, requiring conforming types to provide implementations for all the propertypropertyA property, in some object-oriented programming languages, is a special sort of class member, intermediate in functionality between a field (or data member) and a method.https://en.wikipedia.org › wiki › Property_(programming)Property (programming) - Wikipedia and method requirements in the entire protocol hierarchy.


2 Answers

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.

like image 115
Alexey Kozhevnikov Avatar answered Oct 11 '22 07:10

Alexey Kozhevnikov


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.

like image 41
JeremyP Avatar answered Oct 11 '22 09:10

JeremyP