Lecture 11 – Abstract Classes and Interfaces

In Object-Oriented Programming, abstraction is the process of hiding unnecessary details and showing only essential information.

To implement abstraction in real code, we use:

  • Abstract Classes, and
  • Interfaces

These tools help create structured, flexible, and reusable code where we define what to do, but let subclasses decide how to do it.

Abstract Classes and Interfaces

Abstract Classes

An abstract class is a class that cannot be instantiated directly.
It serves as a blueprint for other classes.

Definition:

An abstract class contains one or more abstract methods methods that are declared but not implemented.

Example (C++ – Using Pure Virtual Function):
#include <iostream>
using namespace std;

class Shape {
public:
    // Pure virtual function (no definition)
    virtual void draw() = 0;

    void message() {
        cout << "This is a shape." << endl;
    }
};

class Circle : public Shape {
public:
    void draw() override {
        cout << "Drawing a Circle." << endl;
    }
};

int main() {
    // Shape s; // ❌ Cannot create object of abstract class
    Circle c;
    c.draw();
    c.message();
}

Explanation:

  • Shape is an abstract class because it has a pure virtual function draw().
  • Any class inheriting it must override draw() or else it also becomes abstract.

Example (Java – Using Abstract Class):

abstract class Shape {
    abstract void draw(); // Abstract method

    void message() {
        System.out.println("This is a shape.");
    }
}

class Circle extends Shape {
    void draw() {
        System.out.println("Drawing a Circle.");
    }
}

public class Main {
    public static void main(String[] args) {
        // Shape s = new Shape(); // ❌ Not allowed
        Circle c = new Circle();
        c.draw();
        c.message();
    }
}

Java follows the same principle you can define both abstract and concrete methods inside an abstract class.

As we are discussing Abstract Classes and Interfaces in oop, Polymorphism is also a main pillar of oop. You can explore Polymorphism: Compile-time and Run-time.

Partial Abstraction

An abstract class can have:

  • Abstract methods → no implementation (to be defined by child)
  • Concrete methods → already implemented

This is called partial abstraction because it hides some details but not all.

Example:
In the above example, draw() is abstract, but message() is concrete.
Hence, Shape is partially abstract.

Interfaces

An interface is like a fully abstract class.
It only defines what must be done, not how to do it.

Example (Java Interface):
interface Drawable {
    void draw();  // Always abstract
}

class Circle implements Drawable {
    public void draw() {
        System.out.println("Drawing a Circle.");
    }
}

public class Main {
    public static void main(String[] args) {
        Drawable d = new Circle();
        d.draw();
    }
}

Key Points:

  • All methods in an interface are public and abstract by default.
  • A class must use the keyword implements to use an interface.
  • Interfaces achieve 100% abstraction.

Example (C++ – Using Pure Virtual Functions):

#include <iostream>
using namespace std;

class Drawable {
public:
    virtual void draw() = 0; // Pure virtual function
};

class Circle : public Drawable {
public:
    void draw() override {
        cout << "Drawing a Circle." << endl;
    }
};

int main() {
    Drawable* d = new Circle();
    d->draw();
    delete d;
}

In C++, an interface is created by defining a class with only pure virtual functions.

Real-World Applications

ScenarioAbstraction TypeExample
Payment SystemsInterfacePayment interface → PayPal, Stripe classes implement it
Shapes in GraphicsAbstract ClassShape base class → Circle, Rectangle subclasses
VehiclesAbstract ClassVehicleCar, Bike
DevicesInterfaceConnectableBluetoothDevice, WiFiDevice

Abstract Class vs Interface

FeatureAbstract ClassInterface
MethodsCan have both abstract and concreteOnly abstract methods (in classic Java)
VariablesCan have instance variablesOnly constants (final static)
Multiple InheritanceNot supportedSupported via multiple interfaces
Abstraction TypePartialFull
Use CaseWhen base class shares logicWhen only behavior needs to be defined

Summary of Lecture 11

ConceptDescriptionExample
Abstract ClassBlueprint for subclassesabstract class Shape
Abstract MethodDeclared, not definedvoid draw();
Interface100% abstractioninterface Drawable
Pure Virtual FunctionAbstract method in C++virtual void draw() = 0;
Positive Thought for Students

Abstraction is wisdom knowing what to show, what to hide, and how to keep things simple.

People also ask:

What is the main purpose of an abstract class?

An abstract class provides a base structure for subclasses and allows both defined and undefined (abstract) methods.

How does an interface differ from an abstract class?

An interface contains only method declarations, while an abstract class can have both declarations and implementations.

Can a class implement multiple interfaces?

Yes, a class can implement multiple interfaces to achieve multiple inheritance in a flexible and modular way.

Leave a Reply

Your email address will not be published. Required fields are marked *