What is Object-Oriented Programming?

Object-oriented programming (OOP) is a programming paradigm that organise a piece of software based on the concept of objects or data rather than by function and logic.

In OOP, software is designed by using data modelling to identify the various objects that makes up the system and how these objects interact with each other. These objects can range from physical entities such as a human being that is described by a set of attributes/properties like name, height and age to small computer programs such as widgets.

Once an object is identified, an extensible program-code template will then be created to generalise the object. This template will contain properties or attributes that the object can use to store data. In addition, it also contain functions or methods that define the logic sequences or statements to manipulate these data.

Features of OOP language

An OOP language usually come with support for code extensibility in the form of classes or prototypes and code reuse through inheritance. This kind of support ultimately give rise to two styles of OOP—one is class-based programming and the other is prototype-based programming.

Class-based programming languages support two main concepts: classes and objects.

A class is a program-code template that defines the data format and available procedures for that class. On the other hand, an object is an instance of the class, or in other words, an object is the realisation of a class.

Prototype-based programming languages on the other hand has no concept of classes. Objects are the primary entities. Generalised objects are typically used in these type of languages where the objects can subsequently be cloned and extended to form the foundation of future objects.

These languages also share certain features that are found in other programming languages that adhere to other types of programming paradigm such as procedural programming or functional programming. The two most common features are variables and procedures.

A variable is used to store information formatted based on a small number of data type like integer and alphanumeric characters that are built-in most languages.

A procedure, also known as function, methods, routines or subroutines, is simply a construct in code that takes some inputs, manipulate data and/or generate output. In addition, other programming constructs like loops and conditionals are also included in any OO language.

Furthermore, there are also four common mechanisms or features, if you will, that OOP language have that set them apart from other type of programming languages. These four mechanisms are Abstraction, Encapsulation, Inheritance and Polymorphism.

Abstraction

Abstract means an idea or concept that is not tied to an instance or realisation. In OOP, an abstract class or interface allows programmers to express the intent of the class instead of the actual implementation. In a way, this hides the inner working of a class from other calling classes that do not need to know in order to use it.

For example, a driver only need to know how to drive a car but does not need to know in detail of how the engine and gearbox works. In this context, the car has abstracted away the inner workings from the driver.

In an example below, we have a Vehicle class that is abstract that has implemented some logic for its move method. Another class called SedanCar is a non-abstract class but extends from Vehicle, which is a concept related to Inheritance (please see below). Then a driver gets a SedanCar by instantiating it and decides to drive it. The drive method calls the move function on the instantiated vehicle without ever needing to know what are the operations or steps needed to be executed to move the vehicle.

public abstract class Vehicle
{
        public void move(){
                //Logic to engage the engine
                //Logic to spin the axle
                //Logic to spin the wheel
        }
}

public class SedanCar extends Vehicle
{

}

public class Driver{

        Vehicle vehicle;
        
        public void getASedanCar()
        {
                vehicle = new SedanCar();
        }
        

        public void drive()
        {
            vehicle.move();
        }
}


Encapsulation

Encapsulation is related to abstraction in the sense that it hides the inner working. The difference lies in that encapsulation hides the data implementation of a class from other classes— data are made private or have restricted visibility from the outside. Through this mechanism, only the host class can change its internal data while other classes has to do it via either the provided public accessors or methods that perform actions that ultimately effect a change in the data.

Below is an example of a Person class written in Java. Similar to an actual person, the person class contains both name and age properties that are private to it. Once the class is instantiated as an object, no other objects can access the properties or make changes to it.

As described earlier, these other objects have to do it via the provided accessor methods (functions) get either the name or age and use the setter methods to set the name and age of the person object.

public class Person
{
    private String name;
    private int age;

    public void setAge(int age)
    {
        this.age = age;
    }

    public int getAge()
    {
        return age;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    public String getName()
    {
        return name;
    }
}

Inheritance

Inheritance in OOP is a mechanism that allows a class or object to be based upon another class or object and retain similar implementations. In a sense, it is similar in biology where the makeup of an organism such as a human being is based on the sum of their parents’ genes. These genes determine how they and their body react and behave in different circumstances, how they look and whether they will ultimately develop medical conditions such as diabetes, high blood pressure and heart failure.

With inheritance, original codes implemented in the parent class can be reused and help shorten the amount of codes needed. Furthermore, if there is any changes to be made to the original code, the programmer/developer only need to change the one in the parent class and all child classes will get the updated implementation.

Polymorphism

Polymorphism refers to the ability of an object to take on many forms. There are two types of polymorphism in practice and are generally applied to methods and functions instead of classes.

The first type of polymorphism is compile time polymorphism. It also goes by another term called overloading. This means that there can be multiple methods with the same name but different type of parameters.

An example of compile time polymorphism is as follows where there are two methods named move. One of the move method takes no parameter as input and the other takes an object as an input.

public class Vehicle
{
    public void move(){
    }
    
    public void move(Object object){
        }
}

The second type of polymorphism is run-time polymorphism. It also goes by another term called overriding. With this type of polymorphism, there is only one move method but different classes will implement it differently based on the context.

Below is an example where there are three classes that implements the same move method. Since both Sedan and Bus extends from Vehicle, when either of them gets instantiated and their versions of the move method gets called, then the respective move method will be executed instead of the one in Vehicle.

public class Vehicle
{
    public void move()
    {
            //some code here
    }
}

public class Sedan extends Vehicle
{
    public void move()
    {
        //some code here
    }
}

public class Bus extends Vehicle
{
    public void move()
    {
            //some code here
    }
}
Advertisement

Published by

Brandon Lim

I'm a software engineer and writing is my passion.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s