Back to Basics: OOP and C# – Part 2 (Inheritance)

In Part 1, I did a quick review of Class and Objects with Visual Studio 2012 Express for Desktop on a C# Console Project. To see the Person Class that we will be referencing to continue this series, you can read back here.

Now that you probably have a working Person class if you followed on with the first part, let us begin adding a new class file and name it “Instructor”.

Instructor class

Instructor class

To have Instructor class derived from Person class, you will need to add using the format  “: {base class}” next to the class declaration.

Instructor inheriting from Person

i.e. Instructor inheriting from Person

this process enabled the derived class Instructor to reuse, extend and modify properties and methods that the base class Person had with the exception of the constructor.

For instance, we wanted to add a List of Subjects the Instructor teaches, a method that Adds a subject into the list; and creating another derived class Student that will have the Year Level property:

Derived classes (Instructor and Student) of Person

Derived classes (Instructor and Student) of Person

Note: In Visual Studio, you can perform common refactoring routines with a set of shortcut keys. i.e. to Encapsulate a field, you may hold Ctrl<key> then press ‘R’ then ‘E’ (Ctrl + R + E) while the cursor is in the same line and a dialog will appear (or you may also right click the property to find the context menu for Refactoring).

Each of the derived classes Instructor and Student now represents two different specializations of the Person class.

Further,

From MSDN:A derived class can have only one direct base class. However, inheritance is transitive. If ClassC is derived from ClassB, and ClassB is derived from ClassA, ClassC inherits the members declared in ClassB and ClassA.

to illustrate, let us extend the Student class to have Grades.Transitive Inheritance (Person > Student > StudentGrades)

Noticed how YearLevels enum which is of Student was used, and how Lastname and Firstname of Person was used. Below is a visual representation of the hierarchy:

Inheritance Class Diagram

Inheritance Class Diagram

That covers some basics of Inheritance. There are more which includes Abstract and Virtual Methods which is really the basis for polymorphism. I am going to cover that shortly.

Another terminology used is Abstract base class. This is done if you want to prevent direct instantiation with the new keyword so it can only be used to derived class. An example is when you see me use the Person class from Program.cs in Part 1. If the Person class has been declared as Abstract instead, it can only be used within Student which is a derived class.

I will be covering the rest of implementation when I get to the later part of the series including Interfaces.

Till next… Read it, love it, live it 😉

Advertisements

3 thoughts on “Back to Basics: OOP and C# – Part 2 (Inheritance)

  1. Pingback: Back to Basics: OOP and C# – Part 3 (Polymorphism) | michael corpuz

  2. Pingback: Back to Basics: OOP and C# (Encapsulation) – Taglish | michael corpuz

  3. Pingback: Back to Basics: OOP and Layered Application Design (Fundamentals to N-Tiered Architecture) | michael corpuz

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s