Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can i able to support multiple inheritance with protocol in swift?

In Swift, with the use of extension you can give method body in "protocol". in my code I am able to give method body, see

protocol Test1{
    func display()
}
extension Test1{

    func display(){
        print("display Test1")
    }
}

protocol Test2{
    func display()
}
extension Test2{

    func display(){
        print("display Test2")
    }
}
class ViewController: UIViewController,Test1,Test2 {

    var test1 : Test1?
    var test2 : Test2?

    func display() {
        print("display")
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        self.test1 = self
        test1?.display()

        self.test2 = self
        test2?.display()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

I know that I give test1 and test2 object's address in ViewController class. so "display" is printed two times. but in both "protocol" I am able to body of those methods.

so my question is that why apple give me functionality to write methods body in "protocol" ?

Can anyone help me to understand this functionality ?

like image 611
vikas prajapati Avatar asked Dec 09 '16 06:12

vikas prajapati


1 Answers

Though the question looks simple explaining all the aspects of Protocol Oriented Programming and importance of out will make me write whole blog around it :)

Anyway, Protocol Oriented Programming basically enables you to exploit the multiple inheritance which Object Oriented Programming languages like JAVA, Objective-C won't support because of DiamondProblem

Now though Protocol Oriented Programming allows you to model the capability /functionality as a protocol rather than as instance method of classes (as in case of Objective C) we have to agree that we all enjoyed Inheritance always!!, Remember you used to declare a method with some operation and then extend it write whatever specific to child and still use all the code in parent method simply by calling super.methodname??? Now how can you possibly achieve it in protocol ??? You can't re use the code ??

So Protocols default implementations are the simple ways to provide the default implementation to the classes, which simply wants to extend and confirm the protocol but does not bother to modify it.

Example : Assume I have a protocol which prints my family name, if parent class confirms to it it will print same family name as me if I confirm to same protocol as well :) Correct after all its a family name it won't change!!!

Just because you confirm to a protocol u need not provide its methods implementation if the protocol already has its own default implementation. You will provide your own if you want to do something other than the default implementation provided :)

protocol Test {
    func test1()
}

extension Test {
    func test1() {
        print("Yo man")
    }
}

protocol Test2 {
    func test2()
}

extension Test2 {
    func test2() {
        print("Bye man")
    }
}

class ViewController: UIViewController,Test,Test2 {

    override func viewDidLoad() {
        super.viewDidLoad()
        self.test1()
        self.test2()
    }
}

As you can see I have not provided any implementation to test1 or test2 yet I use it as if its already implemented for me :)

Thats the intention of default implementation of protocol. Hope it helps

Observation:

You have two protocols, Test1 and Test2 both have same method display, now you are getting into the fundamental issue of DiamondProblem, if I simply call display() which implementation to call ?? Test1's or Test2's ?? Swift solves it at compile time and tells you that ambiguous use of display() when you call self.display()

The only reason its still working probably in ur code because you have created two variables of type test1 and test2 and you have called methods using test1.display() and test2.display() which is ok no ambiguity there, but thats not how you want to use it isn't it ??

So basically you can never get into Diamond problem with Protocols :)

like image 171
Sandeep Bhandari Avatar answered Oct 19 '22 23:10

Sandeep Bhandari