0% found this document useful (0 votes)
7 views

Inheritance

The document introduces the concept of inheritance in C#, a key aspect of object-oriented programming that allows the creation of new classes based on existing ones. It provides a detailed example of a school management system, illustrating how a base class 'Person' can be inherited by derived classes 'Student' and 'Teacher', each with their own unique properties and methods. Additionally, it suggests extending the system by adding a 'Principal' class, further demonstrating the flexibility and reusability of inheritance in programming.

Uploaded by

baizhiyuki
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Inheritance

The document introduces the concept of inheritance in C#, a key aspect of object-oriented programming that allows the creation of new classes based on existing ones. It provides a detailed example of a school management system, illustrating how a base class 'Person' can be inherited by derived classes 'Student' and 'Teacher', each with their own unique properties and methods. Additionally, it suggests extending the system by adding a 'Principal' class, further demonstrating the flexibility and reusability of inheritance in programming.

Uploaded by

baizhiyuki
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

University of Caloocan City November 12, 2024

Computer Studies Department


_____________________________________________________________________________________________________________________

Introduction to C# Inheritance

Inheritance is a fundamental concept in object-oriented programming (OOP) that allows you to define a
new class based on an existing class. In C#, inheritance enables you to create classes that share common
properties and behaviors, allowing for code reuse, logical hierarchy, and easier maintenance.

When a class is created through inheritance:

• The existing class is called the "base class" (or "parent" class), containing shared attributes and
methods.

• The new class is called the "derived class" (or "child" class), which inherits the base class's
properties and methods but can also introduce additional features that are specific to it.

Inheritance is useful when different types of objects share similar properties but also need to add unique
features. This setup allows us to write code in a structured, modular way.

In C#, inheritance is achieved by using a colon (:) followed by the name of the base class. For example, if
a Student class inherits from a Person class, it would be written as class Student : Person.

Example: School Management System

Let’s explore how inheritance works through a real-world example: building a simple school
management system. In this example:

• We start with a Person base class, which holds common attributes and behaviors for any person
in the school.

• From Person, we will create two specialized classes: Student and Teacher.

o Each derived class inherits the properties of Person but also includes unique attributes
and behaviors relevant to students and teachers.

Step 1: Create the Base Class (Person)

1. Define the Person Class:

o Properties like Name and Age are common to all people.

o A method Introduce() allows each person to introduce themselves.


public class Person
{
public string Name { get; set; }
public int Age { get; set; }

public void Introduce()


{
Console.WriteLine($"Hello, my name is {Name} and I am {Age} years old.");
}
}

C#: Object Oriented Programming Page 1 of 3


University of Caloocan City November 12, 2024
Computer Studies Department
_____________________________________________________________________________________________________________________

Step 2: Create the Student Class

1. Define the Student Class:

o Inherits from Person using : Person.

o Adds a property for GradeLevel and a method to display the grade.

public class Student : Person


{
public string GradeLevel { get; set; }

public void ShowGradeLevel()


{
Console.WriteLine($"I am in grade {GradeLevel}.");
}
}

Step 3: Create the Teacher Class

1. Define the Teacher Class:

o Inherits from Person.

o Adds a property for Subject and a method to teach that subject.

public class Teacher : Person


{
public string Subject { get; set; }

public void Teach()


{
Console.WriteLine($"I teach {Subject}.");
}
}

C#: Object Oriented Programming Page 2 of 3


University of Caloocan City November 12, 2024
Computer Studies Department
_____________________________________________________________________________________________________________________

Step 4: Testing the Classes

Task: Create instances of Student and Teacher to test their properties and methods.

1. Create a Student Object:

Student student = new Student();


student.Name = "Alice";
student.Age = 14;
student.GradeLevel = "8th Grade";

2. Create a Teacher Object:

Teacher teacher = new Teacher();


teacher.Name = "Mr. Davidson";
teacher.Age = 23;
teacher.Subject = "Object Oriented Programming";

3. Call the Methods:

o For student, call Introduce() and ShowGradeLevel().

o For teacher, call Introduce() and Teach().

Expected Output

Hello, my name is Alice and I am 14 years old.


I am in grade 8th Grade.
Hello, my name is Mr. Davidson and I am 23 years old.
I teach Object Oriented Programming.

Inheritance Activity – Group by maximum of 4-5 members only:

Extend the School Management System with New Roles and Properties:

• Add a Principal class that represents the school head. The Principal class should:

o Inherit from Person.

o Include a property YearsInService (an integer representing how long the principal has been in the
role).

o Add a method Announce() that prints an announcement like "Welcome! Let's have a great day at
school!".

o Add Principal Object and call the Introduce() and Annouce() methods.

C#: Object Oriented Programming Page 3 of 3

You might also like