Back to Basics: OOP and C# (Encapsulation) – Taglish

Ito ay ika-apat sa isang serye ng mga panulat tungkol sa Object Oriented Programming at C# at pangalawa na isinulat sa wikang Filipino at English. Sa unang bahagi, aking tinalakay ang Classes at Objects, sinundan ng Inheritance, at ito lamang nakaraan ay Polymorphism. Kung susundan mo sa unang bahagi, doon mo mapupuna ang mga ginamit na halimbawa ay nagpapakita na ng ilang pag-gamit ng Encapsulation na siyang paksa ng panulat na ito.

Sa MSDN, ito ay pinaliwanag na ganito:

“Naayon sa prinsipyo ng encapsulation, ang isang class o struct ay ang maglalarawan kung papaano mo nais magamit ang mga bahagi nito sa labas ng class o struct kung saan mo ito idiniklara.” Idinagdag pa dito na “Ano mang method at variable na hindi mo nais ipakita sa labas ng class o assembly ay maaari mo ring itago sa pamamagitan ng Encapsulation upang malimitahan ang hindi maayos na gamit.”

Ang nabanggit na Members
(o bahagi) sa itaas ay ang mga sumusunod:

Paalala: Ang bawat isa sa nabanggit ay link papunta sa kanilang kahulugan sa MSDN.

Ang mga Access Modifiers ang makapagsasabi kung ang ilan sa mga Members (o bahagi) na nabanggit ay maaring gamitin sa labas ng class o sa sarili loob lamang ng class kung saan ito naidiklara. Ang ginagamit sa salita upang maglarawan nito ay public, protected, internal, protected internal, at
private.

Upang ipakita ang isang halimbawa, balikan natin ang isang class na ating binuo (assuming na sinundan mo ang pagsulat nito sa unang tatlong bahagi ng serye):

Makikita natin dito na ang lahat ng miyembro ng class ay nakadiklara na public. Ito ay naglalarawan na ang sinumang gumamit ng class na ito, o gumamit ng assembly kung saan kabilang ang class na ito ay pwedeng gamitin sa kanilang code ang Grades, UpdateGrade at NameLastFirst.

Ilang bagay ang ating gagawin upang ipahiwatig ang intensiyon na ilarawan ang encapsulation na nababagay sa halimba sa itaas.

Una: dahil ang ‘Grades’ na property ay hindi natin nais ipagamit ng diretso, gagawin natin itong private;

Pangalawa: upang magamit ang ‘UpdateGrade’ sa derived classes at hindi sa iba pa, papalitan natin ang access modifier nito ng protected.

Pangatlo: ang pagpapakita ng laman ng Grades na koleksiyon sa parehas na assembly at derived classes ay maaring makamit sa pamamagitan ng protected internal.

Tingnan ang halimbawa sa ilalim:

Sa bagong halimbawa, hindi na makikita ang Grades at UpdateGrade kung ang class ay tatawagin sa isang instance.

Subalit maari paring makita sa isang derived class ang UpdateGrade, pero hindi ang Grades. Halimbawa:

At ang kalalabasan nito sa ating programa kapag pinatakbo ay:

read it, love it, live it!

Advertisements

Back to Basics: OOP and C#

My older brother has connected me to a student of InfoTech Institute of Arts and Sciences which required a resource speaker for their midterm requirements.

So I chatted with her a little bit and got to an agreement that we are covering ‘building a cloud based solution’ but we need to teach them the fundamentals as pre-requisites since we are bringing it to 2nd year students.

And the best place to start is to brush up their OOP Principles (with C#).

If you quickly pull up your favorite search engine, you’d come by 3 popular terminologies and there is a wealth of resources in the web to help you with its definition – and yes, I am talking about Encapsulation, Inheritance and Polymorphism. Since the intention of my next few series is for the attendees to have a place where they can review my upcoming talk, while this can still be useful to you, this will be a targeted discussion.

Part 1: A quick review on Class and Objects

Supposed we are to treat this activity (activity – referring to ongoing lecture) as a software, and everything we do is part of a computer program, how do we write it? One approach is to try and write a narrative of your observation inside this class.

For instance:

Michael Corpuz is teaching to the 2nd year students Principles of OOP in Room 100 at 9:00am during Tuesdays and Thursdays.

Identify verbs and nouns to form as your objects and methods. i.e. Michael Corpuz, 2nd Year Students, Principles of OOP, Room 100, teaching, 9:00am, Tuesdays and Thursdays.

In some cases, they would treat Instructor (Michael Corpuz) as Class separate to a Student (Students can also be a class that is a collection of Student) and Year Level (2nd year) as a Property. Others would go by defining a Person Class since that is common of an Instructor and Students and adding a property Person Type where you can define if that Person is a Student or an Instructor. Others would go as far as defining Person as a Base Class (note the change ‘base’ from above) and using that as a foundation for Instructor Class and Student Class. That is referred to as Inheritance. Before we dig deeper into that, let us now build the classes for purposes of comparison.

Open Visual Studio 2012 (or equivalent Express Edition) to create a C# Console Project. In my case, I pulled up Express 2012 for Windows Desktop – and click ‘New Project’ shown below:

New Project

New Project

Visual C# > Console Application

Visual C# > Console Application

For the purposes of this demonstration, we are using the 2nd approach above, we are not ready for Inheritance yet. From the ‘Solution Explorer’, right click the project you just created and add new Class and name it Person — by default, the file Person.cs will have a class declaration with the name Person under a namespace which is also by default the name of the project.

 

Person Class

Person Class

 

Quick Tip: typing ‘enum’, you will see the intellisense pull up a code snippet icon aside from the ‘enum’ type itself, hitting ‘tab’ key will use the snippet on your code view for faster construction of your codes. you can do the same for Property by typing ‘prop’ or ‘propa’ then hit ‘tab’ key twice.

Properties and Methods

 

Note that we did not use “Year Level” property yet, that will come in handy discussing Inheritance.

 

Person person = new Person();

 

 

Using the Person class from the main program.

Using the Person class from the main program.

 

 

Program output

Program output

 

Part 2: <<link to be posted here>>.