Skip to content

Abstraction: abstract class and abstract methods

    abstraction: abstract class and abstract methods
    Abstraction Simplifying System in Software Engineering
    In software engineering and computer science, the process of removing physical, spatial, or temporal details or attributes in the study of objects or systems in order to more closely attend to other details of interest; it is also very similar in nature to the process of generalization.

    What is an Abstract Class?

    An abstract class is a must-inherit class that cannot be instantiated. It serves as a blueprint for other classes and provides a common interface for its subclasses. Abstract classes are designed to be extended by other classes, which means they can define common methods and properties that the subclasses inherit.

    One of the defining features of an abstract class is that it can contain both regular methods with method instructions and abstract methods without any method instructions. Abstract methods are declared without any code and are meant to be implemented by the subclasses. This allows the abstract class to define a contract that its subclasses must adhere to.

    Understanding Abstract Methods:

    An abstract method is a must override method. It acts as a contract that ensures that any class inheriting from an abstract class must provide an implementation for the abstract method. This ensures that certain methods with common functionality are present in all the derived classes, while allowing each class to define its own unique implementation. By utilizing abstract methods, programming languages allow for code reusability, modularity, and flexibility.

    Benefits of Abstraction

    Abstraction provides several benefits in object-oriented programming:

    • Simplification: By hiding unnecessary details, abstraction allows us to focus on the essential aspects of a system, making it easier to understand and work with.
    • Code Reusability: Abstract classes can define common methods and properties that can be inherited by multiple subclasses, promoting code reuse.
    • Flexibility: Abstract classes provide a flexible structure that can be extended and customized by subclasses, allowing for variations in behavior while maintaining a common interface.
    • Polymorphism: Abstraction enables polymorphism, which allows objects of different classes to be treated as objects of the same abstract class, providing a way to write flexible and reusable code.

    Links

    Code Examples

    C#
    // Abstract class public abstract class Shape { public abstract double CalculateArea(); } // Concrete class public class Circle : Shape { private double radius; public Circle(double radius){ this.radius = radius; } public override double CalculateArea(){ return Math.PI * Math.Pow(radius, 2); } }
    JavaScript
    // Define an abstract class class Shape { constructor() { if (new.target === Shape) { throw new Error("Cannot instantiate abstract class"); } } // Define abstract methods calculateArea() { throw new Error("Abstract method 'calculateArea()' must be implemented"); } calculatePerimeter() { throw new Error("Abstract method 'calculatePerimeter()' must be implemented"); } } // Create a subclass of Shape class Rectangle extends Shape { constructor(length, width) { super(); this.length = length; this.width = width; } // Implement the abstract methods calculateArea() { return this.length * this.width; } calculatePerimeter() { return 2 * (this.length + this.width); } } // Create an instance of Rectangle const rectangle = new Rectangle(5, 10); console.log(rectangle.calculateArea()); // Output: 50 console.log(rectangle.calculatePerimeter()); // Output: 30
    Python
    from abc import ABC, abstractmethod # Abstract base class class Shape(ABC): @abstractmethod def calculate_area(self): pass # Concrete class class Circle(Shape): def __init__(self, radius): self.radius = radius def calculate_area(self): return 3.14159 * (self.radius ** 2)
    PHP
    <?php // Abstract class abstract class Shape { abstract public function calculateArea(); } // Concrete class class Circle extends Shape { private $radius; public function __construct($radius) { $this->radius = $radius; } public function calculateArea() { return pi() * pow($this->radius, 2); } }

    Conclusion

    Abstraction is a fundamental concept in software engineering and computer science. By removing unnecessary details, developers can focus on the essential aspects of a system, simplifying complexity and promoting code reuse. Through practical examples in C#, JavaScript, Python, and PHP, we have seen how abstraction can be implemented in different programming languages. Embracing abstraction allows developers to build robust and scalable software systems that are easier to understand, maintain, and evolve.