Programming Pandit

c/c++/c#/Javav/Python


Latest Update

Monday, November 4, 2024

Abstract Classes and Concrete Classes

In object-oriented programming, abstract classes and concrete classes are two fundamental concepts that serve different purposes within a class hierarchy. 

Abstract Classes

An abstract class is a class that cannot be instantiated on its own and is meant to be inherited by other classes. It serves as a blueprint for other classes and often includes one or more pure virtual functions (or abstract methods) that derived classes must implement. Abstract classes help define a general concept that can be specialized by subclasses.

Key points:

  • Purpose: Define a common interface or shared functionality for subclasses.
  • Pure Virtual Functions: An abstract class usually has one or more pure virtual functions, which are functions without a body. Declaring a function as pure virtual means any derived class must provide an implementation for it.
  • Inheritance Requirement: Abstract classes are meant to be inherited by concrete classes that will implement the details of the abstract methods.
  • Instantiation: You cannot create an instance of an abstract class.

Syntax:

class Shape 
        public
      // Pure virtual function making Shape an abstract class 
       virtual double area() = 0
 };

Concrete Classes

A concrete class is a fully defined class that can be instantiated to create objects. It provides implementations for all its methods and does not contain any pure virtual functions. Concrete classes can be derived from abstract classes or other concrete classes.

Key points:

  • Purpose: Provide specific implementations for the functions defined in abstract classes or other base classes.
  • Full Implementation: Concrete classes have complete definitions for all methods, making them ready to instantiate.
  • Instantiation: Concrete classes can be instantiated, allowing for the creation of objects.

Syntax

class Rectangle :  public Shape 
{
  public: double length, width; 
  Rectangle(double l, double w) : length(l), width(w) {} 
  // Implement area() for Rectangle 

       double area() override 
    {
       return length * width; 
     } 
};


Example: Abstract Class vs Concrete Class:

#include <iostream>
using namespace std;

// Abstract class Shape
class Shape {
public:
    // Pure virtual function to make Shape an abstract class
    virtual double area() = 0;
};

// Concrete class Rectangle derived from abstract class Shape
class Rectangle : public Shape {
    double length, width;
public:
    Rectangle(double l, double w) : length(l), width(w) {}

    // Overridden function to calculate area
    double area() override {
        return length * width;
    }
};

// Concrete class Circle derived from abstract class Shape
class Circle : public Shape {
    double radius;
public:
    Circle(double r) : radius(r) {}

    // Overridden function to calculate area
    double area() override {
        return 3.14159 * radius * radius;
    }
};

int main() {
    // Shape s;  // This line would cause an error since Shape is abstract and can't be instantiated

    Shape* rect = new Rectangle(5.0, 3.0);  // Rectangle object
    Shape* circ = new Circle(4.0);          // Circle object

    cout << "Area of Rectangle: " << rect->area() << endl;
    cout << "Area of Circle: " << circ->area() << endl;

    delete rect;
    delete circ;

    return 0;
}

Output:

Area of Rectangle: 15 
Area of Circle: 50.2654

Explanation

  • Abstract Class (Shape): Defines a pure virtual function area(), making it abstract.
  • Concrete Classes (Rectangle and Circle): Implement the area() function with specific formulas for each shape. These classes are complete, so they can be instantiated to create objects.

Summary

  • Abstract Class: Defines an interface for other classes and may include some shared code. It cannot be instantiated and is often used to provide a common foundation for a hierarchy of derived classes.
  • Concrete Class: Provides full implementations of all its methods and can be instantiated. Concrete classes often implement the details defined by abstract classes.

No comments:

Post a Comment