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 🙂


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

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

  2. 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: Logo

You are commenting using your 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