LESSON 11
3.1 Lesson Outcomes
After completing this lesson, learners will be able to:
- Define abstract classes in Java.
- Create abstract classes and abstract methods.
- Explain the purpose of abstraction in Java.
- Differentiate between abstract and regular classes.
- Implement abstract classes in Java applications.
3.2 Overview
Object-oriented programming allows developers to create generalized structures that can be shared by multiple related classes. Abstract classes are used when a class should provide common functionality but should not be instantiated directly.
This lesson introduces learners to abstract classes and explains how abstraction helps simplify software design and improve code organization. Learners will also explore abstract methods and how subclasses implement inherited abstract behaviour.
Abstract classes are commonly used in:
- banking systems,
- enterprise software,
- frameworks,
- management systems,
- and application architectures.
Understanding abstract classes is essential because abstraction is one of the key principles of object-oriented programming.
KT1101 — Introduction to Abstract Classes
An abstract class is a class declared using the abstract keyword.
Abstract classes:
- cannot be instantiated directly,
- may contain abstract methods,
- and may also contain normal methods.
Syntax of an Abstract Class
abstract class Animal {
}
Explanation
| Component | Description |
|---|---|
| abstract | Keyword declaring abstract class |
| class | Declares class |
| Animal | Class name |
Importance of Abstract Classes
Abstract classes help developers:
- create generalized templates,
- enforce method implementation,
- improve code reuse,
- and support abstraction.
KT1102 — Abstract Methods
An abstract method is a method declared without a body.
Abstract methods must be implemented by subclasses.
Syntax of an Abstract Method
abstract void sound();
Example
abstract class Animal {
abstract void sound();
}
Explanation
| Component | Description |
|---|---|
| abstract | Declares abstract method |
| void | No return value |
| sound() | Method name |
| ; | No method body |
Importance of Abstract Methods
Abstract methods:
- define required behaviour,
- enforce consistency,
- and support standardized application design.
KT1103 — Implementing Abstract Classes
Subclasses inherit abstract classes and implement abstract methods.
Example
abstract class Animal {
abstract void sound();
}
Subclass Example
class Dog extends Animal {
void sound() {
System.out.println("Dog barks");
}
}
Main Class Example
public class Main {
public static void main(String[] args) {
Dog d = new Dog();
d.sound();
}
}
Output
Dog barks
Importance of Implementation
Implementation allows subclasses to:
- define specific behaviour,
- customize functionality,
- and inherit shared structures.
KT1104 — Abstract Classes with Normal Methods
Abstract classes may also contain fully implemented methods.
Example
abstract class Animal {
void sleep() {
System.out.println("Animal is sleeping");
}
}
Explanation
Abstract classes may contain:
- abstract methods,
- normal methods,
- variables,
- and constructors.
Importance of Normal Methods
Normal methods:
- provide reusable functionality,
- reduce duplicated code,
- and simplify software maintenance.
KT1105 — Difference Between Abstract and Regular Classes
| Abstract Class | Regular Class |
|---|---|
| Cannot create objects directly | Can create objects |
| May contain abstract methods | Cannot contain abstract methods |
| Used for inheritance | Used for direct object creation |
| Supports abstraction | Supports standard object creation |
Example of a Regular Class
class Student {
}
Objects can be created directly from regular classes.
KT1106 — Real-World Use of Abstract Classes
Abstract classes are commonly used in:
- banking applications,
- employee management systems,
- game development,
- frameworks,
- and enterprise software.
Examples:
- Vehicle class
- Employee class
- Account class
- Shape class
Abstract classes define shared structures while allowing subclasses to provide specific implementations.
3.5 Key Notes / Summary
- Abstract classes use the abstract keyword.
- Abstract classes cannot be instantiated directly.
- Abstract methods do not contain method bodies.
- Subclasses implement abstract methods.
- Abstract classes may contain normal methods.
- Abstraction improves code reuse and application structure.
- Abstract classes support object-oriented programming design.
.