Comprehend Object-Oriented programming in 6 minutes

--

Object-oriented programming or OOP is a fundamental programming standard used by several developers, As it focuses on the idea of “objects” that developers want to manipulate rather than logic.

An object is a data field with unique attributes and behavior. Like, properties and methods. This approach gives many benefits like code reusability, scalability, and efficiency. Therefore, it is appropriate to use for complex programs like microservices, etc.

First and foremost, let’s start with the fundamental building blocks associated with Object-oriented programming. “Classes”, “Objects”, “Methods”, and “Properties”.

1. Classes — are user-defined data types that act as blueprints for individual objects, attributes, and methods. For example, The human classes are Man and Woman.

2. Objects — are instances of a class created with a combination of variables, functions, and data structures, etc. When the class is defined initially, the description is the only object that is defined. For example, the name of the Human can be (Billy).

3. Methods — are functions defined inside the class that describe a procedure or behavior of an object. For example, The Human can “shoot” the ball on the basketball court.

4. Properties — are attributes defined in the class and represent the state of an object. For example, The human attributes are their height, and weight. etc.

Example code using C#.

Here’s the guideline for the fundamental building blocks of OOP

Alright, we already understand the main components of OOP.

Next, let’s proceed to the four pillars of the OOP.

The four pillars of object-oriented programming are abstraction, encapsulation, inheritance, and polymorphism.

Inheritance — Inheritance supports reusability because it allows child classes to inherit or acquire the features of parent classes. In other words, parent classes extend or give their attributes and behavior to the child classes.

The simplest example to explain this is our FAMILY because Inheritance is typically acquiring traits or features or attributes from your parents.

Encapsulation — The encapsulation is also known as “data hiding” because it only exposes the selected class/function/variable to be accessible in a different class.

According to the rules, encapsulation comes in when each object has its state or access modifier.

Encapsulation requires defining state or access modifiers.

1. Public — Object that has public access modifier is accessible from everywhere inside the project.

2. Private — Object that has private access modifier is only accessible inside a class. Therefore, it’s not accessible outside the class created.

3. Protected — Object that has a protected access modifier is commonly used in Inheritance. Thus, the object is only accessible inside the class and in classes that derive from that class.

4. Internal — Object with an internal access modifier is only accessible inside its own assembly/project but not in other assemblies.

5. Protected Internal — Object that has protected internal access modifier is the combination of protected and internal. Therefore, it is only accessible in the same assembly/project or a derived class in another assembly/project.

6. Private Protected — Object with a private protected access modifier implies that we can access members inside the containing class or a class that derives from a containing class, but only in the same assembly/project. Therefore, if we try to access it from another assembly, we will get an error.

Example code using C#.

Inside the Alphabet class. We have method A with the access modifier of Public and method B with the access modifier of Private. Other class doesn’t have direct access to method B because it’s for the alphabet class only. Instead, they can only trigger method A since it was a public access modifier.

The benefits of encapsulation are summarized here:

1. Adds security: Only public methods and properties are accessible from the outside.

2. Protects against common mistakes: Only public fields & methods are accessible, so developers don’t accidentally change something dangerous.

3. Supportable: The codes are easy to make updates and improvements.

4. Hides complexity: No one can see what’s behind the object’s curtain!.

Abstraction — Abstraction is an extension of encapsulation. Thus, the main goal of abstraction is to handle complexity by hiding unnecessary details from the user.

Furthermore, most real-world programs have a large codebase. Hence, maintaining this large codebase is difficult since a lot of objects are communicating with each other. Applying abstraction makes things easy for this problem because each object reveals only on a high-level mechanism.

For example, imagine we buy a burger in a restaurant, And we can tell the counter what we want. Nevertheless, we don’t need to know how the chef made the burger. In other words, we don’t need to know the complete logic of how something works. Our job is to use it. That’s the abstraction.

Ultimately, abstraction contains security because it only exposes selected pieces of data, And protects necessary data from exposure. Moreover, it only allows data to be accessed through classes and modified through methods.

The benefits of abstraction are summarized below:

  • Simple, high-level user interfaces
  • Complex code is hidden
  • Security
  • Easier software maintenance
  • Code updates rarely change the abstraction

Difference between Abstraction and Encapsulation.

Abstraction — It is used to hide unwanted data and shows only the required properties and methods.

Encapsulation— It binds data members and member functions into a single unit to prevent outsiders from accessing it directly.

Polymorphism — Polymorphism means “many forms” or “many shapes” that designed the objects to share behaviors. Also, they can take on more than one form.

For Instance, Imagine we are on the Olympics sprint (100m), then the referee starts the competition by saying the command “Ready, Set, Go!”.

From here, we see that each player heard the command but ran at a different speed.

Typically, Polymorphism allows the same method to execute with different behavior or result.

There are two types of Polymorphism:

Dynamic Polymorphism — could be determined at “run-time” that is uses for method overriding. In method overriding, a child class can provide a different implementation than its parent class.

Example code using C#.

Static Polymorphism — could be determined at “compile-time” that is uses for method overloading. Therefore, Methods may have the same name with different numbers of parameters passed into the method.

Example code using C#.

The benefits of Polymorphism are:

  • Method overriding
  • Method overloading
  • Objects of different types can pass through the same interface.

In summary, Object-Oriented Programming (OOP) is thinking about the program structure so that our codes become simple, reusable classes, easy to maintain, available for extension, and more outstanding benefits. For that reason, we could have the best quality product because of our codebase.

Author: Marc Kenneth Lomio | Full Stack Software Engineer | BlastAsia, Inc.

--

--

Marc Kenneth Lomio & Melrose Mejidana

Software engineers that shares knowledge and experience through this medium.