Extras
Purpose
Additional resources and practice for mastering OOP principles.
This section demonstrates abstraction, encapsulation, generalisation and inheritance working together in a single, coherent example.
Important
- In exams, students explain the principles.
- In projects, students apply them using code like this.
1) Abstraction — modelling only what matters
Scenario
A system manages staff members in a school. Only relevant information is included.
C# code
Why this is abstraction
- Only essential attributes are included
- Irrelevant real-world details are excluded
- The model fits the purpose of the software
2) Encapsulation — protect data and control access
Now we improve reliability by restricting direct access.
C# code
Why this is encapsulation
- Data and methods are bundled together
- Direct access to
staffIdis prevented - Changes only occur through methods
📌 Textbook link
Encapsulation improves data security and reliability.
3) Generalisation — identify shared features
Teachers and admin staff share common features, so we generalise them.
C# code
Why this is generalisation
- Common attributes and behaviours are placed in one class
- Duplication is reduced
- Maintenance is simplified
4) Inheritance — specialised classes reuse the general class
Now we create specialised staff types.
C# code
Why this is inheritance
Teacheris aStaffMemberAdminStaffis aStaffMember- Shared features are inherited automatically
- Specialised features are added where needed
5) FULL mini-program (all principles together)
This example shows:
- abstraction (relevant features only)
- encapsulation (controlled access)
- generalisation (shared class)
- inheritance (specialised classes)
Complete working program
using System;
class Program
{
static void Main()
{
Teacher t1 = new Teacher();
t1.Name = "Ms Lim";
t1.SetStaffId("T204");
t1.Subject = "Mathematics";
AdminStaff a1 = new AdminStaff();
a1.Name = "Mr Patel";
a1.SetStaffId("A118");
a1.OfficeLocation = "Office B3";
PrintStaffDetails(t1);
PrintStaffDetails(a1);
}
static void PrintStaffDetails(StaffMember staff)
{
Console.WriteLine("Name: " + staff.Name);
Console.WriteLine("ID: " + staff.GetStaffId());
Console.WriteLine();
}
}