System Design Principles
ICT2622 - Object-Oriented Analysis · Designing Object-Oriented Systems
System Design Principles
System design principles guide the development of object-oriented systems. These principles help ensure that software is robust, maintainable, and scalable. In this lesson, you will learn about several key design principles: encapsulation, abstraction, inheritance, and polymorphism.
Encapsulation
Encapsulation is a fundamental principle of object-oriented design. It refers to the bundling of data and methods that operate on that data within a single unit, known as a class. Encapsulation restricts direct access to some of an object's components, which helps prevent unintended interference and misuse.
Example: Consider a class namedBankAccount. This class can encapsulate attributes such as accountNumber and balance, along with methods to deposit and withdraw money.class BankAccount {
private String accountNumber;
private double balance;
public BankAccount(String accountNumber) {
this.accountNumber = accountNumber;
this.balance = 0.0;
}
public void deposit(double amount) {
this.balance += amount;
}
public void withdraw(double amount) {
if (amount <= balance) {
this.balance -= amount;
} else {
System.out.println("Insufficient funds");
}
}
}
Remember: Use access modifiers (like private) to protect the data within the class.
Abstraction
Abstraction is the principle of simplifying complex systems by modelling classes based on the essential characteristics of an object. It allows developers to focus on what an object does instead of how it does it. This principle helps in reducing complexity and increases efficiency.
Example: In the context of aVehicle class, you can define methods such as start() and stop() without detailing how these actions are implemented for different types of vehicles.abstract class Vehicle {
abstract void start();
abstract void stop();
}
class Car extends Vehicle {
void start() {
System.out.println("Car starting");
}
void stop() {
System.out.println("Car stopping");
}
}
Tip: Use abstract classes to define common behaviours while leaving implementation details to subclasses.
Inheritance
Inheritance is a mechanism that allows one class to inherit the properties and methods of another class. This promotes code reusability and establishes a relationship between the parent (superclass) and child (subclass) classes. Inheritance can be single or multiple, but C++ supports only single inheritance.
Example: Building on theVehicle class, you can create a subclass called Bike that inherits from Vehicle.class Bike extends Vehicle {
void start() {
System.out.println("Bike starting");
}
void stop() {
System.out.println("Bike stopping");
}
}
Watch out: Be careful with the