Vijay Krishna's Notes http://vijaykrishna.posterous.com Most of my notes as a student of computer software and everything around it. posterous.com Sun, 16 Jan 2011 18:10:00 -0800 Interface Inheritance http://vijaykrishna.posterous.com/interface-inheritance http://vijaykrishna.posterous.com/interface-inheritance

This friend of mine, pings me and asks my something about UML diagrams. Well, i answer his doubts and then i ask him what he was designing. That is when he tells me something i never thought of. What he told me, gave me a new insight to OOP and the fact that more the options you give to our friendly users for whom we are trying to build such brilliant technology, the more they will get confused by trying out and seriously using a gamut of all possible permutations of those options. In this case, the user is us, i.e Developers, Coders the whole class. Speaking of which, my friend told me about the most unique design pattern ever. He told me about a hierarchy of interfaces. "WHOOHA!!! Hold on!", i told my self, and tried to understand the concept, more like digest it, due to the conflict, then and slightly even now, raging in my head. He was talking about a hierarchy of interfaces wherein there was one IBaseInterface and its IChildInterface 1,2,3... and so on. This is was nothing but the inheritance of interfaces and there in lies the conflict of ideas. Interface as far as i am concerned was the perfect tool to overcome the issues with inheritance and embrace true code re-usability and freedom. Inheritance made the code rigid and tightly coupled to the parent classes. You could never quite do what you wanted to freely once you defined the object structure. It was painful. And then there were interfaces which only told you what to do. Not how to do them. The code became neater, it was fun. And then, some intelligent people mixed it. Now, lets take a step back. Inheritance all said and done did reduce a lot of our coding. If you had the correct design sense you could do wonders with the concept in designing powerful and flexible applications. But then, it things would get difficult with the client demands changed took the whole pendulum swing. Ahhhgrrrrrr!!! But, it did help a lot, there is no doubt about that. A lot of its issues were solved by interfaces. And as i kept using them more, i kept using inheritance lesser and lesser. But here i am wondering, as i always have been, why not have the best of both worlds. If you are a master designer and you can handle inheritance well, (and some things to be honest are better off done using that) why not use it! Interface Inheritance, gives you just that. The best of both worlds. Mind you you are still free to come up with different implementations for all those interfaces in the hierarchy. But you just have to remember that the Derived Interfaces also hold an implied contract of the Base/Master Interface. But the Beauty still lies in the fact that you can have multiple implementations, for all of them, and still keep them in a hierarchy. A common and valid argument i found against this approach was not use such hierarchies and simply implement all the required interfaces instead of inheriting one interface to another and implementing only the second one. While this is a valid argument, the prerogative of the design lies with the designers and the developers. You just need to keep one simple thing in mind while implementing such designs: there are implied contracts that need to be implemented and taken care of. Deal with that, and you have a very powerful tool in your hand.

Permalink | Leave a comment  »

]]>
http://files.posterous.com/user_profile_pics/1369599/pic.jpeg http://posterous.com/users/hcGXxsTkwP6SS Vijay Krishna Palepu vpalepu Vijay Krishna Palepu