4 Main Pillars Of OOP | Part 1 : Inheritance
Inheritance in simple terms means ,just as you know, inheriting something from somewhere. Further simplified, it is taking something that already exits from somewhere. For example, a father and a mother both have blue eyes and their child take their blue eyes from them or in other terms the child inherits from parents. No magic. Nothing comes out of nowhere.
In programming perspective it is just as same. You take/use of some already existing functionality from someplace. You inherit from it. But today we are going to talk inheritance in OOP (Object-Oriented-Programming) especially in .NET and C# boundaries. So first of all, what exactly is OOP ?
With the advance of computer technologies many new programming paradigms rose and fall. Right now OOP seems to be the leading paradigm that is being used heavily in the industry.
OOP resolves around classes which are just like blueprints and objects which are just like products. You only produce objects from classes.
In this example an airplane from airplane blueprint is produced. In a sense you define boundaries of your product.
Of course, you are not limited to producing only one product from a blueprint. It goes same for the objects too. You can produce as many as you want object from a class.
Objects can be identical or can have differences because classes actually determine only their boundaries and their implementations are up to objects.
All is clear for now. Let’s take one step further. What makes OOP different in a programmatical way is that objects contain both the attribute(data) and action together.
For example, a plane has attributes of Color, Production Date, Maximum Speed etc. and has actions of Fly, Land, Start Engine etc.
As you can see objects do really look like real life objects which actually was the aim so that people could write more readable and understandable code.
If you change implementations of these attributes and actions then you will have different planes.
I guess more or less OOP is okay for now, then lets dive into our real focus on this article.
Say you have a plane class already but now you also want a Jet too. Why bother repeating to write a class from ground up as they both do have common features and stem from same origin. Some can argue saying why not use same airplane class then change attributes but our new Jet class needs new features like Super Sonic Flight and already existing airplane class does not have any possible feature for it.
So what now ? Let’s dive into some code shall we. We are going to use C# but first a diagram of what we are going to do.
Pretty simple, right? Aircraft which is our base class, JetAirPlane Class and PassengerAirplane Class are our child classes.
As you can see we have some common attributes and action like color, production date, pilot name and fly. Now we can inherit from this just like this.
JetAirplane class now has all the features Aircraft class has and even more because JetAirplane class also needs SuperSonicFly so we add to it. And also we have our PassengerAirplane class.
Again inherited from Aircraft class but there is also an addition namely Passenger Count field.
By utilizing inheritance we got rid ourselves writing repetitive code and also structured our code in a readable manner. As you can see OOP inheritance can be advantageous in many situations but of course in it is not applicable for every case.
Look out for Part 2 : Polymorphism.
See for additional references : https://www.w3schools.com/cs/cs_inheritance.asp