Building a Class-Based System in TypeScript.
Course Title: Mastering TypeScript: From Basics to Advanced Applications Section Title: Classes and Object-Oriented Programming Topic: Build a class-based system that demonstrates inheritance and interfaces.(Lab topic)
Introduction
In the previous topics, we have covered the basics of classes and object-oriented programming in TypeScript. We have learned about classes, constructors, inheritance, access modifiers, and interfaces. Now, it's time to put this knowledge into practice by building a class-based system that demonstrates inheritance and interfaces.
Lab Overview
In this lab, we will build a simple class-based system that models a university. We will define classes for University
, Department
, Course
, and Student
. We will use inheritance to create a hierarchical structure of classes, where Department
and Student
inherit from University
. We will also use interfaces to define a contract for classes that can be used to calculate the total number of students.
Step 1: Define Interfaces
First, let's define two interfaces, UniversityInterface
and StudentInterface
. These interfaces will define the properties and methods that must be implemented by classes that implement them.
// university-interface.ts
interface UniversityInterface {
name: string;
location: string;
}
// student-interface.ts
interface StudentInterface {
id: number;
name: string;
calculateGpa(): number;
}
Step 2: Define University Class
Next, let's define the University
class, which will inherit from UniversityInterface
.
// university.ts
class University implements UniversityInterface {
name: string;
location: string;
constructor(name: string, location: string) {
this.name = name;
this.location = location;
}
}
Step 3: Define Department Class
Now, let's define the Department
class, which will inherit from University
and implement the StudentInterface
.
// department.ts
class Department extends University implements StudentInterface {
departmentName: string;
students: Student[];
constructor(name: string, location: string, departmentName: string) {
super(name, location);
this.departmentName = departmentName;
this.students = [];
}
addStudent(student: Student) {
this.students.push(student);
}
}
Step 4: Define Course Class
Next, let's define the Course
class.
// course.ts
class Course {
courseName: string;
creditHours: number;
constructor(courseName: string, creditHours: number) {
this.courseName = courseName;
this.creditHours = creditHours;
}
}
Step 5: Define Student Class
Now, let's define the Student
class, which will implement the StudentInterface
.
// student.ts
class Student implements StudentInterface {
id: number;
name: string;
courses: Course[];
constructor(id: number, name: string) {
this.id = id;
this.name = name;
this.courses = [];
}
addCourse(course: Course) {
this.courses.push(course);
}
calculateGpa(): number {
// implement GPA calculation logic here
return 3.5;
}
}
Conclusion
In this lab, we have demonstrated the use of inheritance and interfaces in building a class-based system in TypeScript. We have defined classes for University
, Department
, Course
, and Student
, and used interfaces to define contracts for classes.
Key Takeaways
- Inheritance allows us to create a hierarchical structure of classes, where subclasses inherit properties and methods from parent classes.
- Interfaces define contracts for classes, specifying properties and methods that must be implemented.
- Interfaces can be used to ensure that classes implement specific functionality.
Further Learning
If you want to learn more about interfaces and inheritance in TypeScript, please visit the official TypeScript documentation at https://www.typescriptlang.org/docs/.
Do You Have Any Questions?
Please leave your questions or comments below.
Images

Comments