Fundamentals of Class-based OOP
Sebastian Van Rooyen
Posted on March 13, 2024
There are four fundamental concepts of Object-Oriented Programming:
- Abstraction
- Encapsulation
- Inheritance
- Polymorphism
Before we dive into these concepts. First:
What is an Object?
You can think of an object as a noun. Such as a car, book, place or even bank account.
In terms of programming, an Object is anything that you want to store and process data about. Another name for an object is an entity.
Still a bit complicated to understand?
Let's make it simpler.
Think of an object as if it were a Lego block. It has its own color, shape, size and pain level, when stepping on it.
You use these blocks to build a Lego house.
The house (software program) is made up of different blocks (objects).
Do you understand? If not, take a hard look at yourself and if programming is for you in the first place.
1. Abstraction
Abstraction is like simplifying reality.
Hear me out. A person is an object, but if we were designing a new program to process data about a person, we would only concern ourselves with the relevant data of the person and how we intend to use this data.
To create objects programmatically, we need a Class.
Class
A class is a template for creating objects.
A class is often compared with a pastry cutter. Because once it's created, it can be used to create many objects of the same type.
Each object is an instance of this class.
Person p4 = new Person();
The above creation of the p4 object is called instantiation.
2. Encapsulation
Encapsulation is hiding data and complexity.
Think of a treasure chest. It holds valuable items inside, and it's locked to keep those items safe. In programming, a class acts like a treasure chest.
It holds data (the treasures) and methods (the locks) to access or manipulate that data. The data inside the class is hidden from outside access, just like the treasures are hidden inside the chest.
Encapsulation helps keep the complexity of a program under control by organizing related data and functions together.
3. Inheritance
Inheritance is exactly what it says. A class can derive it's methods and properties from another class. Just like a child inherits from a parent.
For example, a class Employee
can derive from the class Person
because an Employee is a Person.
The same can be same for a class Customer
, because a customer is also a Person.
Classes that inherit from other classes can include their own properties/data. They can extend from the class they are inheriting.
Polymorphism
This is when a class implements an inherited method in it's own way.
The subclass(child) can override any method or property from the superclass(parent).
Posted on March 13, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.