OOP Concepts for Beginners: Simplified

Shamoda Jayasekara
Conceptual Viewpoint
5 min readFeb 25, 2021

What is Object-Oriented Programming and Why?

Well, we all know that there are lots of programming approaches out there. Like Procedural Programming, Functional Programming, Object-oriented Programming, and the list goes on. So, which programming approach you should take to solve a problem? Well, the simple answer is “It depends on your requirement” 👻

But why Object Orient Programming (OOP) is so popular? 🙄
In OOP you will find that it is a new way of solving problems. When you are writing a program, you may probably divide large problems into smaller sub-problems. And solve them using separate units of code. The basic idea behind OOP is instead of writing a huge program, you can create a class which is a template containing a set of attributes and methods. Inside a method, you can implement the logic to solve a sub-problem. Then you create objects using that template or the class. Objects are instances of that class, and you can interact with them in a fun and exciting way. So, here you can create any number of classes depending on your requirement. Eventually, you’ll be able to come up with a solution for your complex problem in a more convenient way.

So, what are the 4 pilers of OOP? Well, Let’s get into it.

Wait ✋, before that, I want to introduce you to some basic terminologies we often use.

Attribute:
It is a variable owned by a class and it will determine the characteristics of that class.

Method:
Methods are functions in which we can implement our logic to solve a specific problem. Methods will determine the behavior of the instances of the class.

Class:
It is a blueprint or template which contains its own attributes and methods.

Object:
Simply, it is an instance of the class. An object has its own attributes and methods which reside inside the blueprint or the class. Objects can be uniquely identified by their name.

Well, here we go, these are the basic OOP concepts you should know,

· Abstraction

· Encapsulation

· Inheritance

· Polymorphism

Okay, let’s take them one by one.

Abstraction

It is a process that provides only the functionality to the user while hiding complex implementations from the user. Simply, the user will have information on what the object does instead of how it does. Actually, users do not want to know the complex logic implemented in the methods of the class. The user is only interested in the functionality of the class.

Well, I’ll give you a simple example to understand this. Let’s take a TV remote controller as our example. We have so many buttons on that remote to control our Television. When we press the ON button, Television will be turned on. But we do not know how it happens inside the remote circuitry. Actually, we don’t need to know it, we just wanted to turn ON the TV, that’s it!

Well, we can apply the same thing in OOP. We have access to the public methods of an object, and we can use them to perform some operations. Here, we don’t need to know about the complex logical implementations behind them. To implement abstraction, we can use interfaces as well as abstract classes.

Encapsulation

Grouping related data and code together into a single unit is called Encapsulation. For example, let’s take a medicine capsule, which is wrapped up with different medicines. Simply, encapsulation will act as a security barrier that prevents accidental access to code and data by other codes defined outside the class. We can use access modifiers like private, public, and protected to secure class attributes (data) and methods (code).

Inheritance

Well, inheritance is one of the basic concepts in OOP. Here, one class (child) can acquire characteristics (attributes) and behaviors (methods) of another class (parent). It will create a Parent-Child relationship between classes. One of the main advantages of inheritance is, it provides code reusability. This simply means, when parent classes have some specific characteristics and behaviors, we don’t need to rewrite or reimplement them in its child classes. Because when we use inheritance relationship those characteristics and behaviors will inherit by the child.

Polymorphism

Polymorphism means the ability to have multiple forms. Just look at the below example. Speak() method is implemented differently in each sub-class of Animal class. When you call the Speak() method in Dog class, it will say “Woof”, when you call the same method in Cat class, it will say “Meow” likewise Cow will say “Moo”. Look! Same method but different implementations, this is what we called Polymorphism.

There are 2 types of Polymorphism.

1. Static Polymorphism: Method overloading

Method overloading performed within the same class. When we are overloading the same method inside the same class, its return type can be the same or different from each other. But remember! parameters must be different.

2. Dynamic Polymorphism: Method overriding

Method overring performed inside child classes of a parent class, which means there should be an inheritance relationship between classes. Method overring can be used to provide a specific implementation of a method that is already provided by its parent class. Remember! method signature must be the same inside each and every class.

So, I hope now you have some basic idea about what OOP concepts really are? Isn’t that so? Let me know if you have any doubts and thoughts in the comment section down below. Cheers for now!!!

Happy Coding!!!

--

--