Lesson 35 of 55
15 mins readPython Abstract Classes and Methods
In Plain English
Abstract classes serve as base classes from which other classes may be derived. Unlike interfaces, abstract classes can contain implementation details for their members.
Core Rules to Remember

Template Methods: Combines abstract method contracts with shared concrete implementations.

No Direct Instantiation: Abstract classes cannot be instantiated with new directly.
Live Interactive Example
Hit Run Code to see it liveTemplate Pattern with Abstract Classes
Python 3.12
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Output Console
Click "Run Code" to view the rendered output.
How it works: PaymentProcessor cannot be instantiated directly; StripeProcessor provides the required processPayment implementation.
Your Turn: Micro Challenge
No pressure! Edit the starter code below and test your solution with instant feedback.
Micro Exercise
Build Abstract Database Driver
Declare abstract class Database with constructor(public connectionString: string).
Add abstract connect(): boolean and concrete describe(): string returning `DB at ${this.connectionString}`.
Create class MongoDb extending Database with connect() returning true. Print describe() and connect().
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Sandbox Output
Click "Run & Check" to test your solution.
Finished reading and practicing?
Mark this lesson as completed to update your course progress.