Discover the Magic of Object-Oriented Programming: A Beginner's Guide to OOP Mastery
Tags: programming, opensource
Published: 2023-02-23 12:33:10

Hey everyone, Welcome to the world of Object-Oriented Programming (OOP)! As a beginner, you might have heard about OOP being a fundamental aspect of software development, but don't quite understand what it is and how it works. Fear not, as this blog is here to demystify OOP and guide you through your journey to becoming an OOP master. Whether you're a tech-savvy individual or a non-technical person, this comprehensive guide will break down the concepts and techniques of OOP into simple, easy-to-understand terms. Get ready to dive into the world of OOP and discover the magic of this powerful programming paradigm.

**Note**: In this article, we will be discussing what are we doing, Not how we are doing (implementation). There will be different articles coming up language specific to teach how we implement these. :)

## What is Object-Oriented Programming?

In simple words, Object-Oriented programming is the practice of breaking down your code into blocks/components such that they can be reused and even used to build upon. This practice of reusing the code makes it efficient and helps reduce redundant code. It also gets easier to write and maintain code.

## Is Object-Oriented programming language specific?

Well, In short, NO! Object-oriented programming is not specific to a particular language. It is more like an adopted practice that can be applied to many different programming languages like Java, Python, Dart, and C++ to name a few.

Of course, the syntax and approach of each language are different but the underlying concepts and principles remain the same.

The implementation differs but the core principles of OOPs are "Universal".

The choice of programming language for implementing OOP will depend on the specific needs and requirements of a project, as well as the skills and preferences of the developers involved.

So, what are these universal principles? Don't worry, we will get to them but before we do, Let us take a look at classes and objects which are the basic and fundamentals of it. Let's go!!

## Classes and Objects

Before we get into technically savvy, Let us look at a real-world example and understand them.

I am sure all of us here are aware of cars. We can define cars as a class. Now, this class of cars has some properties such as the "No. of wheels", "steering side", "fuel" etc and standard behaviours such as "starting an engine", "acceleration" etc. These are some basic properties that are used by various manufacturers to make their own cars. Now, these manufacturers use the features of class "cars" to make their own cars with even additional features if required and most importantly though the properties are the same, the values of those properties are unique. Toyota can derive properties from the class "car" to build fortuner(its own car) with more properties and the value of the properties like price, fuel type are all different. Even Ferrari will derive the. properties from the class "Cars" to build their car but their price will be much higher, they could only have 2 seats instead of one etc.

So, here these cars built by different manufacturers are nothing but "Objects".

Each of these objects would have its own unique values for the make, model, color, and other properties, but they would all inherit the same behaviours defined in the Cars class.

To define technically now, Objects are instances of a class.

A class is like a blueprint or a template for creating objects which defines the properties, methods or behaviour etc.

To make it simpler to understand: Remember that

Classes are just logical or virtual. On the other hand, objects are like the physical implementation of classes.

You can create a class using the "class" keyword in most languages.

How are these objects created? How are the objects able to inherit properties? You'll know soon enough.

Now that you know the fundamentals, it's time to jump into the principles of OOP.

## Pillars of Object-Oriented Programming

Object-Oriented Programming has only four principles that act as its pillars of it. Let us dive into them:

**Inheritance**

Inheritance is a key concept that allows a new class to inherit properties and behaviours from an existing class. This helps to reduce the amount of duplicated code in a program and provides a way to reuse code.

Think of inheritance as a type of relationship between two classes, where one class (the child class) inherits from another class (the parent class). The child class has all the properties and behaviours of the parent class but can also add new properties and behaviours of its own. But parent class will not have any knowledge of the child class or its properties and behaviours.

Drawing on the above example, consider a car and a sports car. The car is the parent class, and the sports car is the child class. The car class has properties such as the number of wheels, the colour, and the number of doors. The sports car class inherits all these properties from the car class, but it also adds additional properties such as a faster engine and better handling.

By using inheritance, the code for the car class can be reused in the sports car class, reducing the amount of duplicated code in the program. This also makes it easier to maintain the code, as changes to the car class will automatically be reflected in the sports car class.

Inheritance can also be defined as a class extending another class and ironically inheritance can be implied by using the "extends" keyword. you can refer to an example below:

```
class Cars{
    int wheels;
    int seats;
    double price;
    
    Cars(){. // Constructor 
           this.wheels = 4; // More on "this" keyword later
           this.seats = 2;
        }
}

class SportsCar extends Cars {
    int fuel;
   
    SportsCar(String fuel){
            this.fuel = fuel;
            }
}

void main(){
    // Creating a object 
    SportsCar ferrari = new SportsCar();
    // Accessing parent class element (Inheritance)
    System.out.println(ferrari.seats);
}

```
There are various types of inheritance such as Single, Multi-level, Multiple, and Hybrid. We will look into these in detail in future.

Now that you have an understanding of inheritance, let us take a look at the next one.

**Polymorphism**

If you break the word down, it is poly which stands for "Many" and morphism which means "Ways to represent". Put together it means Many ways to represent a single thing. It is derived from the greek.

Think of polymorphism as the ability of an object to take on multiple forms. For example, consider a shape class with a draw method. The draw method could be implemented differently for each type of shape (e.g., a square, a triangle, a circle). However, because they all have a draw method, they can be treated as objects of the same class.

To better understand with code, here:

```
public class Shapes {
    void area(){
        System.out.println("This is the area of the shapes");
    }

public class Circle extends Shapes{
    void area(){
        System.out.println("The area of circle is pie * r * r");
    }

void main(){
        Shapes s1 = new Shapes();
        Circle s2 = new Circle();
        
        s1.area();
        s2.area(); // try them out to better understand

```
There are two types of polymorphism:

Run-Time/Dynamic Polymorphism which can be achieved by Method overriding.

Compile-Time/Static Polymorphism which can be achieved by Method Overloading.

Polymorphism makes it easier to work with objects that have similar behaviors but different properties, and it helps to make code more flexible and reusable. This is a powerful feature of OOP that allows for greater flexibility and maintainability in code.

**Encapsulation**

Let us consider a bank account class. The class could have properties such as the account balance and the account holder's name. The class could also have methods such as deposit, withdrawal, and transfer, which allow the user to interact with the account. By using encapsulation, the bank account class protects the account balance from being modified directly, ensuring that all transactions are processed through the deposit, withdraw, and transfer methods.

Think of encapsulation as a protective barrier around an object's data and behaviour. The data is hidden from the outside world and can only be accessed and modified through the methods defined within the object. This helps to ensure that the data remains in a consistent state and is not modified accidentally.

Encapsulation solves a implementation level issue and focuses on internal working.

Encapsulation helps to simplify the complexity of an object and makes it easier to understand and use.

**Abstraction**

Continuing to draw from the above example, Let us consider a car. The car has many complex systems such as the engine, transmission, and electrical system, but the user does not need to know about the details of these systems to use the car. The user can simply get in the car, turn the key, and drive. The car provides an abstract view of the underlying systems, making it easier to use.

Think of abstraction as a simplified representation of an object, showing only the essential information and behaviour. Another example can be, consider a television remote control. The remote has buttons for power, volume, and channel, but the user does not need to know about the complex electronics and programming that make the remote work. The user can simply press the buttons and the remote takes care of the rest.

To sum it up, Abstraction is a concept that involves hiding the complexities of an object and exposing only the relevant information and behaviour to the outside world. This allows users to interact with the object at a high level, without having to know about the underlying details of the implementation.

**Conclusion**

In conclusion, the pillars of Object-Oriented Programming (OOP) - inheritance, polymorphism, encapsulation, and abstraction - provide a powerful toolset for organizing and simplifying code. These concepts allow us to create objects that are easy to understand and use while hiding the complexities of the implementation. Whether you are a seasoned programmer or a newcomer to the world of coding, understanding and using these OOP concepts will help you to create more organized, maintainable, and flexible code.

The implementations were not covered to the full extent as the implementation differs from language to language. So, I will make sure to write a blog on implementations in detail soon.

> Pro tip: You can read blogs, and watch tutorials all you want but you'll only learn and master OOPs with practice. Go and just try different things with OOPs. The hit and run or trial and error method. That's how you'll learn.

So, What are you waiting for? Go! Get started right away.

I hope the article helped you out in learning about Object-Oriented Programming. 

Follow me [here](rohitlogs.com) for more such technical content. You can catch me on my [socials here](https://twitter.com/TRRohit20) for content on tech and finance. Would love to connect.

Also make sure to drop down what you would like me to cover next, feel free to reach out for work too. Thank you.