DEV Community

Cover image for Fundamentals of Class-based OOP
Sebastian Van Rooyen
Sebastian Van Rooyen

Posted on • Edited on

Fundamentals of Class-based OOP

There are four fundamental concepts of Object-Oriented Programming:

  1. Abstraction
  2. Encapsulation
  3. Inheritance
  4. 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.

Image description

To create objects programmatically, we need a Class.

Class

A class is a template for creating objects.

Image description

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.

Image description

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.

Image description

Classes that inherit from other classes can include their own properties/data. They can extend from the class they are inheriting.

Image description

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).

Learning resource credit

Top comments (3)

Collapse
 
jonrandy profile image
Jon Randy πŸŽ–οΈ • Edited

To create objects programmatically, we need a Class.

Well, yes, if you're using a language that uses class-based OOP... but that's not the only type of OOP. Arguably the most popular programming language in the world - JavaScript - uses a prototype based OOP model, classes are not used.

Collapse
 
sebastiandevelops profile image
Sebastian Van Rooyen

I agree, a more appropriate title would've been Fundamentals of OOP in Java and C#?

Collapse
 
jonrandy profile image
Jon Randy πŸŽ–οΈ

Maybe "Fundamentals of OOP in Class-based Languages" or "Fundamentals of Class-based OOP"