Object Oriented Programming(OOP) Series: Abstraction

Object Oriented Programming(OOP) Series: Abstraction

Abstraction in simple terms with examples

In this series as promised earlier we'll be brushing up OOP concepts in the most simplest form with examples in PHP, Javascript and Dart.

So far in this series we've covered Classes & Objects , Attributes & Methods, Constructor & Destructor, Access modifier, Inheritance, Polymorphism

Abstraction

Let's take a look at the meaning In order to understand it's usage in object oriented programming.

Abstract : in simple term can be referred to as concise summary. It's meant to give an overview of what the bigger picture looks like.

For example a 3 minutes shot from a video of 2hrs length can serve as an abstract of the movie. It's extracted to give a brief and important features of the movie.

A more clearer example is the layout a driver interacts with to control a car. There's such a detached system that controls the car but there's more to it. The idea behind abstraction is to find a way to make something concrete to be understandable with brief explanation. It's not same with data hiding.

A phone has quite a couple of logic involved in it functioning but it's been summarized into one or more functions for user to interact with through a tap on the screen. A user clicks on the camera icon to launch the camera rather than having to write a program to launch the camera.

Abstraction is a term that refers to the separation of what is of importance to meeting a particular need from the larger picture.

The meaning of abstraction in some articles is almost similarly with encapsulation. But in the next part of this series we'll be looking at Data hiding (encapsulation). They're quite similar but there exist some difference.

The main purpose of using this concept in oop is the reason why anyone can use an electronic device today. Unlike before where systems operation where left for only experts and mathematical gurus.

Abstraction in OOP is a scenario where we take care of the complexity for users without having to be involved in every detailed process to achieve a task by providing key and concise functionalities that can be extended to be used.

The OS is the brain behind the application been used but the user only interacts with the abstracted view to ease comprehension and use.

Just a little touch abstraction takes care of ensuring that the most important features are exposed while encapsulation handles binding what is left into a unit for operation. For example, a camera button is an abstract of the entire camera functionality in the operating system this is because the user doesn't need to know if there's anything like on press method or OS API camera service while polymorphism takes care of organizing what is needed for the camera operation for security reasons. We don't want a situation where a user will click on a camera icon and the phone will put on the phone settings.

We can now have more people using computer devices without studying mathematics or sciences because abstraction takes care of the complexity.

Abstract classes and methods

Abstract methods extended from classes are implemented using sub classes.

It's closely related to the concept of interface but there are some differences. Interface can be directly instantiated while abstract cannot. All the methods in interface must be overridden while in abstract class only abstract method needs to be overridden or implemented.

An abstract class may or may not contain abstract methods.

An abstract class exposes functionality that are important for implementation. An abstract class is a class that cannot be instantiated directly except by a class that extends it to an object.

An abstract method is a method without implementation.

Please NOTE: The examples below are just to support the explanation of abstraction.

Simple illustration in Javascript:

There's no need reserved keyword abstract in javascript unlike other languages however it does implement such approach.

class Car {
    constructor() {

        //if this class is accessed directly throw an error because it's an abstract class
        if (this.constructor === Car) {
            throw new TypeError('Abstract class "Car" cannot be instantiated directly.'); 
        }

        //if this class is extended and this abstract method is not instantiated throw an error
        if (this.info === undefined) {
            throw new TypeError('info defined but not instantiated and cannot be accessed directly');
        }
    }
}

class Toyota extends Car {
    constructor() {
        super();
    }

    get info() {
        return 'This method only works because its expected to be instantiated after the car class is extended';
    }
}

// const car = new Car();
// throws an error because we're not allowed to access the class directly
//Abstract class "Car" cannot be instantiated directly.'
//if the info method is not implemented in the extending class it cannot be implemented

const item = new  Toyota();
//if the name isn't same with the method in the abstract it'll throw an error
console.log(item.info);

Output This method only works because its expected to be instantiated after the car class is extended

Simple illustration in Dart:

//abstract class
abstract class Car {

// abstract method
    void info();
}

class Toyota extends Car{
//implementation of abstract method
    void info()
    {
        print("Toyota implements the abstract class and method");
    }
}

main(){
    Toyota camry = new Toyota();
    camry.info();
}

Output

Toyota implements the abstract class and method

Simple illustration in PHP:

<?php
//abstract class
abstract class Car{

//abstract methods

abstract public function info() : string;

abstract public function type($model);

}


class Toyota extends Car {

  public function info() : string {
    return "This is toyota car";
  }

//overriding the abstract method
public function type($model){
return $model;
}
}

$toyota = new Toyota();
echo $toyota->info();
echo "<br>";
echo $toyota->type(2021);


?>

Output This is toyota car 2021

Thank you for reading this article.

Stay tune for the next article on the OOP Series: Encapsulation

Please kindly share with your network and feel free to use the comment section for questions, answers and contributions.

You love this article?? please follow me on hashnode or twitter @alemsbaja to stay updated for more on these OOP series.

Did you find this article valuable?

Support Alemoh Rapheal Baja by becoming a sponsor. Any amount is appreciated!