Hi guys, today we are going to be learning Object-Oriented Programming - OOP with real-world examples. Take a seat and Grab your popcorn.. chuckles.
What is OOP?
Object-oriented programming (OOP) refers to a type of computer programming (software design) in which programmers define the data type of a data structure, and also the types of operations (functions) that can be applied to the data structure.
In this way, the data structure becomes an object that includes both data and functions. In addition, programmers can create relationships between one object and another. For example, objects can inherit characteristics from other objects.
An Object can be defined as a data field that has unique attributes and behavior.
An object has two characteristics:
Let's take an example:
A parrot is an object, as it has the following properties:
name, age, color as attributes
singing, dancing as behavior
The concept of OOP focuses on creating reusable code. This concept is also known as DRY (Don't Repeat Yourself).
In this article, we will understand the four main pillars of OOP.
It simply means, "Many forms". A man can be a father, brother, son, or student. This means that the same person can have different roles. Just like that, in programming, one method can have various forms.
E.g, if you have played with MS paint, you will agree that selecting various shapes such as squares, rectangles, circles, etc was something we all loved.
There is one main method, createShape(), but it has different forms such as square, rectangle, circle, etc. This is done via polymorphism!
Inheritance is a way of creating a new class by using details of an existing class without modifying it. The newly formed class is a derived class (or child class). Similarly, the existing class is a base class (or parent class).
Let us look at cats. They can have the same color, name, size, fur, etc but they are not the same cat. A Maine coon, tabby, tiger cat, Persians, pussy cat, etc are all cats.
They come under the main class cat but have various properties of their own.
Just like in programming, we can have one main class, with child classes as well. The child classes will all inherit methods and attributes of their parent class but may have methods and attributes of their own too.
A laptop can do many things. You can code, make documents, do video calls, play games, etc.
It doesn't show you the inside process of how it performs all these tasks.
So implementation is hidden!
It only displays the relevant attributes and hides unnecessary info.
Abstraction is a way of hiding implementation details from users and only displays the features to them.
Using OOP, we can restrict access to methods and variables. This prevents data from direct modification which is called encapsulation.
A company can have several departments such as HR, IT, Marketing, etc. Marketing does not have access to HR, HR does not have access to IT or Marketing but all of these make up a company.
We can use encapsulation to hide data and get more flexibility such as setting variables as write-only or read-only etc.
Key Points to Remember:
Object-Oriented Programming makes the program easy to understand as well as efficient.
Since the class is sharable, the code can be reused.
Data is safe and secure with data abstraction.
Polymorphism allows the same interface for different objects, so programmers can write efficient code.
Hope you found this article useful!
Thanks for reading.