Back to Basics: OOP and C# – Part 3 (Polymorphism)

In Part 2, we went into the definition of Inheritance and have shown example of it’s implementation. This week, we’ll be covering one of the OOP Principles that I mentioned in previous posts which is polymorphism.

Also, I realized that there had been a lot of great tutorials in the web already, and since I am delivering this to the Sophomore Class I will be teaching to in a couple of days, I decided to do this a little differently. I am going to ‘experiment’ and mixed some local dialects to it so it would be better understood here in the Philippines (a.k.a. TaglishTagalog/English).

Kung titingnan nyo yung nakaraang posts, makikita nyo dun yung project na ginamit at itutuloy lang natin dito. Pero bago yun, ibigay muna natin yung kahulugan ng Polymorphism sa Taglish:

Mula sa MSDN:

Polymorphism is a Greek word that means “many-shaped” and it has two distinct aspects:

  1. At run time, objects of a derived class may be treated as objects of a base class in places such as method parameters and collections or arrays. When this occurs, the object’s declared type is no longer identical to its run-time type.
  2. Base classes may define and implement virtual methods, and derived classes can override them, which means they provide their own definition and implementation. At run-time, when client code calls the method, the CLR looks up the run-time type of the object, and invokes that override of the virtual method. Thus in your source code you can call a method on a base class, and cause a derived class’s version of the method to be executed.

Ang sabi dito, yung mga objects sa derived class, ay pwedeng ituring na bahagi ng base class sa loob ng method parameters, collections, o sa mga arrays kapag pinatakbo yung program. Pag nangyari yun, iba na yung type ng object sa declaration kumpara kapag pinatakbo mo na sya. Halimbawa (pansinin nyo na itutuloy lang natin yung nakaraang project):

Buksan nyo yung Instructor class, tapos dagdagan nyo ng isang virtual method. (Yun pa yung isang konsepto nya, kapag gumawa ka ng virtual method sa base class, naka deklara na yung pangalan nya at implementasyon, pero pwede mong gamitin yung pangalan sa derived class at magbigay ng panibagong implementasyon (gamit yung override).

Overridable virtual method

Overridable virtual method

Ayun, tapos magdagdag tayo ng tatlo pang derived class na gamit yung Instructor bilang base class. Halimbawa:

Override method on derived class

Override method on derived class

Pansinin nyo, kakalagay ko palang nung override na pangalan tapos lumabas na kaagad na rekomendasyon sa intellisense yung Teach na method.

Ayun, kapag nilagay natin yung kumpleto (pinagsama ko nalang sa isang file yung tatlong class):

Derrived classes with Teach method

Derived classes with Teach method

Ngayon, ilagay naman natin sya sa main program para masubukan natin kung paano sya tatawagin… ang intention nito ay para maipakita na pwedeng magamit yung Programming, SoftwareDesign, at DatabaseDesign kahit saan na ginamit yung Instructor. Hindi narin natin kailangang mag cast sa tamang type kasi kakayanan ng gawin yun nung paggamit ng base class papuntang derived class.

Polymorphism at work

Polymorphism at work

Ang isa pang mapupuna natin sa halimbawa ay yung pagtawag sa method na Teach, na ang implementasyon nila ay hango sa derived class, pero nilagay din nating halimbawa yung pagtawag sa base method ng Teach dun sa Programming class, kaya parehas natin makikita sa resulta yung ginawa ng Teach sa base class at Teach pag tumakbo na yung sa linya ng Programming na class

Polymorphism at runtime

Polymorphism at runtime

Sya nga pala, yung ‘var’ na nakikita nyo, pag-compile nyan, Instructor din na type ang kalalabasan nyan. Kung sinadya ko sya sa code, ganito lang din sya — foreach (Instructor subject in teaches) — malinaw na ba? I hope this work well for you, hanggang sa muli!   (just realized that this is the only Taglish part, and most of it were in Tagalog, hahe) read it, love it, live it 🙂


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.


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 😉