Web programming

Abstraction in Object-Oriented Programming (OOP)

Ngoc Phuong

Ngoc Phuong

2021-03-03 . 131 view

Another important property when referring to Object-Oriented Programming (OOP) is abstraction. So, what is abstraction in OOP?

I. Abstraction in OOP

1. What is abstraction?

Abstraction in OOP is the concept of only showcasing the necessary features to the user without providing the details, essentially showing just what is relevant for the end user without the operational processes.

For example, when developing a Student Management program, we only care about primary information like name, gender, address and don't necessarily need to add other details like height, weight, hobbies, etc. The reason we don't need these additional details is because we're managing Student information from a larger group, maybe the Human group. From the Human group, we can construct other groups besides Students, like Lecturers, Staff, etc.

This process is called abstraction.

2. When to use abstraction?

Abstraction is often used when child classes that inherit from a parent class are of the same essence (belong to one group of objects).

For example, we have an abstract class Animal and inheriting classes like Dog, Cat, etc.

II. Abstract class and abstract method

An abstract class is a class that may contain abstract methods or not.

An abstract method is a method that is declared or defined but is not implemented.

To declare a class or method as abstract, we use abstract.

Example:

abstract class Person {
  // Declare abstract method
  abstract public function info();  // Method 1
  abstract public function info($name);  // Method 2
  abstract public function info() : string; // Method 3
}

Note:

  • When inheriting from an abstract class, the method in the child class must be defined with the same name and access level or more permissive. That means if the abstract method in the parent class is defined as protected, the method in the child class must be defined as either protected or public.
  • Additionally, the data type and number of mandatory arguments must also match. However, child classes may have additional optional arguments.

For instance, if we have an abstract parent class Car with an abstract method intro() returning a string type. When child classes like Audi, Volvo, Citroen inherit from the abstract class Car, they must redefine the intro() method just like the abstract Car class before implementing it.

// parent class
abstract class Car {
  public $name;
  public function __construct($name) {
    $this->name = $name;
  }
  abstract public function intro() : string;
}

// child classes
class Audi extends Car {
  public function intro() : string {
    return "Choose German quality! I'm an $this->name!";
  }
}

class Volvo extends Car {
  public function intro() : string {
    return "Proud to be Swedish! I'm a $this->name!";
  }
}

class Citroen extends Car {
  public function intro() : string {
    return "French extravagance! I'm a $this->name!";
  }
}

// Creating objects from child classes
$audi = new Audi("Audi");
echo $audi->intro();
echo "
";

$volvo = new Volvo("Volvo");
echo $volvo->intro();
echo "
";

$citroen = new Citroen("Citroen");
echo $citroen->intro();

III. Important points when using abstract class and abstract method

1. When a method is declared as abstract, it can only be defined and not implemented.

abstract class Person
{
    // Correctly declare an abstract method
    abstract public function getInfo();

    // Incorrect because you cannot implement code inside a method declared as abstract
    // abstract public function getInfo()
    // {
    //     //
    // }
}

2. In an abstract class, if it's not an abstract method, you can still declare and implement it normally.

abstract class Person
{
    // The getInfo() method is not declared as an abstract method, so it is allowed to have a body like a regular method
    public function getInfo()
    {
        //
    }
}

3. Abstract methods can only be declared in an abstract class.

class Person
{
    // Incorrect because abstract methods cannot be declared within a regular class.
    abstract public function getInfo();
}

4. Properties in an abstract class cannot be declared as abstract.

class Person
{
    // INCORRECT because properties cannot be declared as abstract
    // abstract public $name;
    // CORRECT
    public $name;
}

5. An abstract class cannot be instantiated.

abstract class Person
{
    //body
}
// INCORRECT because you cannot instantiate an abstract class
$person= new Person();

6. Classes inheriting from an abstract class must redefine all the methods in that abstract class.

abstract class Person
{
    protected $name;

    abstract protected function getInfo();
}

// This class is INCORRECT because it does not implement the abstract method getInfo
class Student extends Person
{
    //
}

// This class is CORRECT because it has properly implemented the abstract method getInfo
class Teacher extends Person
{
    public function getInfo()
    {
        return $this->name;
    }
}

 

Ngoc Phuong
Ngoc Phuong

Web Developer

Thank you for visiting my website. My name is Ngoc Phuong, and I have over 10 years of experience in website development. I am confident in stating that I am an expert in creating impressive and effective websites. If you need a website designed, please feel free to contact me via email at [email protected].

0 feedback