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 ?

Object-Oriented-Programming (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.

Inheritance

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.

Conclusion

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

https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/inheritance

--

--

--

Software Developer specialized in back-end development mainly with C#/.NET and JavaScript related technologies.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Ubuntu on Windows: Install Java 8 and Android Studio 4 + NDK

How do QR codes work and How to create a QR code?

Why We Need Open-Source Spirit

First Half Recap, Second Half Look Ahead

Using Excel reference functions in Pandas

Comprehensive guide to successful Netsuite ERP Integration

Platformer Wall Jumping In Unity

eBPF & the future of osquery on Linux

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Ahmet Kuris

Ahmet Kuris

Software Developer specialized in back-end development mainly with C#/.NET and JavaScript related technologies.

More from Medium

Factory Pattern

Filter JUnit 5 tests on Gradle with command-line arguments

Refactoring Code Smells: Data Clumps

Java Threads