A question always lurks into most of the programmers minds, what makes a developer a senior developer or a better programmer? Or, how your college level code is different from a professional developer’s code. Now please don’t me wrong, you can be one of those wow programmers who coded like a pro in the college itself. But in general to answer these questions one major contributor is the two topics we are gonna mention here.
Inheritance and Interfaces are two critical concept that allows one to write reusable, maintainable and good (DRY & SOLID) code.
Interface : A OOPs concept where you only declare the type but do not define the details.
Example: Below is an example of a sample C# interface declaration. A few things to notice, that the interface defined below does not actually perform any logic action or operation it only provides the type information.
Like the outline of the class. Whichever class will “implement” this interface, have to define the logic. Interface is a great tool specially
to handle scenarios where we need to inherit from multiple classes. As multiple inheritance is pretty much not possible in strongly OOP languages like Java or C#, interfaces allows to perform activities where multiple inheritance is needed. In Python however the concept of Interface is handled by Abstract classes as Python allows multiple inheritance of Classes. Abstract Classes are very similar to Interfaces. Abstract class like interface can only declare methods or properties and they can be implemented or overridden in the child class. Abstract class helps us outline the class functionalities in language like Java or C# or C++ you can not inherit multiple abstract classes but you can do that with interfaces.
Another important aspect is while we write re-usable code. A re-usable code is always loosely coupled code, and interfaces provides us with that option where we can program against a flexible type rather than a solid object. Interface allows us to define and modify our code with out changing the calling code that helps in long term maintainability of the application. The list will continue, so, if you have a chance always try to code against the interface and not the object directly.
Inheritance : Another OOPs concept, as per dictionary.com the closest definition I can find is “the genetic characters transmitted from parent to offspring, taken collectively.” . This feature of OOP allow us to distribute a chunk of code in different blocks and reuse them. Again this is a feature to improve our coding practices and apply a principle called DRY –> Don’t Repeat Yourself. As a better programmer you should not copy paste code from one block to another rather you should try to re-use in a proper pattern. Below is a sample example of inheritance in Python & C#. I am using these two languages as they represent two different types of programming languages. C# is Strongly typed like Java where as Python is Loosely Typed like JavaScript, together they cover a large portion of programming languages.
Python:
Inheritance Multiple Inheritance
C# / Java: As you can notice they are syntactically different but still has the same behavior where the functionalities of the base class is passed to the child class. Clearly this helps us writing maintainable and cleaner code.
Multiple inheritance using Classes are not possible however interfaces fills-in that Gap. It does come with some other challenges that we will discuss in my future blogs specifically when I will explain SOLID & DRY principles in details.