Object-Oriented Programming is a methodology or paradigm to design a program using classes and objects. It allows you to break down complex problems into smaller pieces of code and reuse bits of code to save time in the long run.
In this post we will look at the following concepts of Objects Oriented Programming.
- Classes
- Objects
- Abstraction
- Encapsulation
- Inheritance
- Polymorphism
OOP Basics
Adopting object-orientated programming starts with learning to clearly identify objects and define their relationships through data modeling and class definitions. This necessitates that developers grasp the three major components of OOP:
Classes
Classes define the state, behavior and identity of every object.
For e.g, we can consider a car as a class that has characteristics like steering wheels, seats, brakes, etc.
Classes help developers understand the set of properties associated with an object or shared among a group of objects.
Objects
Objects are instances of a classes created with specific data.
For e.g. a dog is an object because it has states like color, name, breed, etc. as well as behaviors like wagging the tail, barking, eating, etc.
Objects have two key characteristics: data (which specifies an object's status and unique properties) and behavior (indicates how it interacts with other objects).
Methods
Methods define the behavior of the objects created from the class.
For e.g. a car is an object with properties like Make, Model and Color and methods like Start, Stop and Move.
Simply put, a method is an action that an object can execute. These are functions defined in a class that describe the behaviour of the object.
OOP Principles
Object-oriented programming is a collection of guiding notions rather than a technical method. There are four main OOP principles, specifically:
Abstraction
Abstraction means to simplify reality.
For e.g. To ride a bike, you simply need to know the pedals, brakes, and gears to get started; you do not need to know the names of all the parts.
The main purpose of abstraction is to hide the background implementation and show only functionality to the user.
Encapsulation
Encapsulation means to hide data and complexity of the inner-workings of an object from the programs and the programmers that make use of it.
For e.g. A doctor provides medicine where the capsule is clear but you can see little pellets inside.
That is encapsulation.
It is often referred to as Information hiding
because the data contained within an object and the functions that manipulate the data are bound together and protected from outside interference.
Inheritance
Inheritance means that a class can derive its methods and properties from another class.
For e.g. The Doctor says you need to go see a specialist about your sinuses. All doctors must go through the same training, then they go into their specialised training areas. All are doctors with different levels of training.
Inheritence can result in a hierarchy of classes.
Polymorphism
Polymorphism means that a class can implement an inherited method in its own way.
For e.g. when it comes to fruit, a banana must be peeled before consumption, as opposed to an apple, which can be eaten with the skin on.
This is polymorphism, the ability of an object to take on many forms. Thus reducing the need for duplicate code.
Advantages of OOPS: 👇
Scaleable and upgradable
All data can be kept in one place.
Security
Using encapsulation and abstraction, complex code is - hidden, maintenance is easier and protocols are protected.
Productivity
Makes it easy to maintain and reusable code.
Modularity
Encapsulation enables objects to be self-contained, and provides a
clear modular structure for collaboration and troubleshooting code.
Reusability
Components can be reused through inheritance, the same code can be adapted and modified multiple times.
Disadvantages of OOPS: 👇
The following are typical drawbacks and critiques made by those who work in the space:
Steep learning curve
The learning process in OOP may not be as simple compared to others. Everybody will have different learning curves, and it may depend on their level of understanding of the paradigm and experience in the industry.
Bigger program size
Seeing the long lines of code explains why some criticize OOP. However, this trade-off comes with an excellent result. Using OOP yields a greater chance of solving problems quicker and more efficiently.
Slower programs
Object-oriented programs are seen as slow by some programmers. They think that procedural programs are much faster. But programs under OOP are widely used by all sorts of programmers.
It can be inefficient
Some programmers see that OOP uses more CPU than other alternatives. They see this potential to produce limitations and delays in the processes. Because of this, they see OOP as inefficient compared to other programs that are less CPU-demanding.
Conclusion
So with that, I hope this helps you gain confidence in your ability to think and problem-solve like a programmer in order to prepare for your next tech role.