|
Home |
Object orientation (abbreviated as OO) paradigm has been a big time in
thing (or fad) in software industry for over last 7-8 years. So, I too had jumped
onto the object oriented bandwagon longtime back. However, not being too computer smart I
have not been able to really master object orientation even after taking several classes
and reading many books. The other day, I asked my mentor (my boss) some advice on how to master the fundamentals of object orientation. He started the same old blabber that innumerable people, in the past, have bored me with. He started explaining how cookie-cutter was a class and cookie was an object. He took another example to distinguish object-oriented programming from structured programming. If he were not my boss, I would have recited that example much before he could even start. He started drilling into my head same good old analogy - in structured programming you rip your entire car into smallest possible entities before you park and next time before you drive out you re-assemble them to get the car. In object oriented programming, you simply park the car and drive out. God knows, which object oriented guru came up with this stupid example to teach object orientation. If this example were to be correct and effective, then I would have learnt OO just using this example because I precisely do structured programming with my 1984 Nissan Stanza. When I park it in the evening, it dismantles itself and the next morning AAA guy reassembles it and gives it a jump start so that I can drive to work. My boss looked at my face to see if there was any visible indication of object-oriented enlightenment on my face. Certainly there was not any. Instead, object oriented ignorance was dripping all over from my CD (Confused Desi) face. Realizing that his efforts were not very effective but still caring for me, my boss started explaining the best way to master anything. He said that in order to master something you should be able to map new piece of information to already acquired knowledge. This correlation makes learning simple. He said that I should use such a topic to correlate, which held maximum fascination for me. He said that sex held maximum fascination for him and boasted that he learnt object orientation by correlating it with sex. In spite of being perpetually inactive, my brain said sex could probably be used to learn another confusing technology ActiveX (at least they rhyme and jell well) but not OO. I said to myself "No, thank you" to bosss suggestion to use sex to learn OO and started thinking about other saner and safer topics which I could use to map and learn object orientation. Being a sincere, devout, religious, tenacious, stubborn, fanatic and simple Brahmin with Brahminism holding maximum fascination, I started to explore if using Brahmin & etc. would help me to master the basics of object orientation. Two weeks after conceiving this idea, I guess I had developed a simple yet very effective way to learn (and teach) object orientation in simplest possible means using the analogy Brahmin & etc.. So, trash all those Learn C++ in 21 days sort of books which confuse more than they teach and join me in this enjoyable journey to object oriented heaven. Please bear in mind, before proceeding further, that term "Brahmin", its several manifestations, derivations and abbreviations are strictly used only for conceptual reinforcement of OO in this tutorial. No religious, communal, emotional or sentimental meanings are attached to the term "Brahmin". Please also read the small print at the end of the tutorial as well. Like any other tutorial, this tutorial is also loaded with abbreviations. So, please make sure that you remember them all through your journey to object oriented eternity. We will use Kannada, Marathi, Tamil and D- -(D minus minus like C++) languages to learn OO in this tutorial. For all practical purposes, it is enough if you know English as all non-English stuff is translated immediately after it is used wherever required. First principle of object orientation is inheritance. In simplest terms, you inherit genes genetically, assets (hope not liabilities) financially etc. from your parents. BRAHMIN is the reference point from where we start inheriting. For the sake of simplicity, lets call this BRA (everything is fair in love, war and OO). Here BRA strictly means only BRAhmin and nothing else. Those who are suspecting my motives to abbreviate BRAhmin to BRA will soon realize and repent when abbreviating BRAHMIN to BRA is justified as we inherit ferociously from BRAhmin. Have you ever seen an entity called BRAHMIN? Definitely not (please agree time being). There are variety of Brahmins like Smartha Brahmins, Vaishnava Brahmins, Madhwa Brahmins, Daivajna Brahmins, Vishwakarma Brahmins, Vaidiki Brahmins, Sanketi Brahmins, converted Brahmins and so on. But, there is no tangible entity that exists just as plain vanilla Brahmin. But, you still visualize something (varies from person to person) when I say BRAHMIN. Unknowingly but very luckily we have stumbled upon a very important OO concept - Abstract Class. Something that always haunts but never exists is an abstract class. God, vampire, devil, demon, happiness, green card, ration card, understanding wife, caring husband, obedient children etc. are all abstract classes. They all give an impression that they really exist but unfortunately they do not (except may be in dreams). As I told before pure and plain BRA, being abstract, cannot be seen, touched, felt or for that matter harassed. But, if I say MAHABRA (for MAHArastrian BRAhmin), many pictures start appearing in front you. When I visualized MAHABRA, two pictures emerged in front of me - Nathuram Godse and Bal Thakre. Depending on your mental condition your visualization of class MAHABRA may result in objects ranging from Guru Golwalkar to Veer Savarkar to Gopinath Munde to Ajit Agarkar to Sachin Tendulkar to Sunil Gavaskar to Anna Hazare to Amol Palekar to Pickle Patankar. However, do not start visualizing Sharad Pawar, Lalita Pawar etc. for MAHABRAs as they are not MAHABRAs. (COLA and MAHACOLA have nothing in common with BRA and MAHABRA. They were simply two sizes in which ThumsUp used to be available few years ago.) If not for our powerful abbreviation of BRAhmin to BRA, every time writing MAHABRA in full would have been such a pain. This is just the beginning. We have tons of more abbreviations to make. MAHABRA is a class. These Munde, Ghodse, Golwalkar, Savarkar, Karmarkar, Agarkar etc. are all objects. To recap, for example, Munde is an instance of MAHABRA inherited from BRA. Lets extend this analogy further to learn many more interesting OO concepts. From MAHABRA we can derive (inherit) two more classes namely DEBRA (not Deborah as in western world) and KOBRA (not COBRA). Can you guess what these classes stand for? DEBRA stands for DEshastha BRAhmin and KOBRA stands for KOnkanastha BRAhmin. There is absolutely no connection between hissing, venomous and life threatening reptile COBRA and not hissing, sweet and life making human KOBRA. For staunch Dharwadis, you can chose to use HOBRAs (HOsayellapur BRAhmins) in place of DEBRAs or KOBRAs. DEBRA and KOBRA have all the features of MAHABRA, which has all the features of BRA. This again reinforces inheritance. In addition to common features, they also have some different features, which sets them apart. Like KOBRAs normally inhabit coastal regions. DEBRAs are normally found inland. KOBRAs boast of famous and juicy Ratnagiri mangoes where as DEBRAs boast of famous (at least they think so) Nagpur Naarangis. (Naarangi means orange. Please do not confuse it with famous Dharwad term BORANGI, a colorful insect that is domesticated by school kids in matchboxes.) KOBRAs boast of beautiful Sahyadri Mountains and Western Ghats where as scenically deprived DEBRAs boast of famous jail in Nasik, even more famous inmates of the jail (like Charles Shobraj) and famous earthquake of Latoor. Like this objects pack their own data, which differentiates them. Polymorphism (as dreadful as polygraph) is a very important concept in OO. In simple words, it implies that same thing means two different things for two different objects. Lets suppose that both KOBRA and HOBRA support a property called MaterialForBreakfast (Material For Breakfast). We query this property to see what a particular person (of type KOBRA or HOBRA) has had for breakfast. Lets programmatically learn this using a pseudo language called D--. D is in respect to Dharwad. HOBRA Jayatheerthachaar Bindagi; // a HOBRA object in Hosayellapur KOBRA BaajiRao KaaLe; // a KOBRA object in Pune Material For Breakfast property on Jayatheerthachar Bindagi object returns vaNa Avalakki in Kannada. Same Material For Breakfast property on BaajiRao KaaLe returns sookha pavve in Marathi. Point to be noted is that both people (objects) have eaten same dry avalakki only for breakfast but their responses sound different. Since language factor is hidden (encapsulated), these objects(Bindagi and KaaLe) do not understand each others responses. There have been instances when HOBRAs have taken sookha pavve for something of a great Marathi delicacy and similarly KOBRAs have considered vaNa avalakki as some great Kaanadi delicacy. To clarify, Kaanadi in Marathi means anything pertaining to Kannada or Karnataka. For example, Kaanadi Mulgi means Kannada Girl, which every Marathi household in Mumbai wants as their daughter-in-law because maidservants are damn problematic there. Kaanadi Mulga means Kannada Boy, which no Marathi household in Mumbai wants as their son-in-law for reasons unknown. Similarly, Kaanadi Murgi and Kaanadi Murga stand for Kannada Hen and Kannada Cock respectively. This much explanation was necessary because I had mistakenly thought Kaanadi meant Kannadi (mirror). OK, before you pounce on me brandishing Lippmans C++ book yelling that I taught polymorphism wrongly, lets stop here as for as polymorphism is concerned. (Note: Do not worry about what happens if objects have not had any breakfast when you query Material For Breakfast property. In that case, a runtime error is thrown which can be caught. Only time this error can be triggered is if day equals to Ekadashi when eating is prohibited right at base class BRA level which in turn prohibits all derivations of BRA like MAHABRA, DEBRA, KOBRA, HOBRA etc. from eating anything.) Virtual functions are another very important concept in OO. In simple words, your dad tells directions to go from Belgaum Road to Kalghatgi Road to you and your bother and your dad himself takes his route to reach Kalghatgi Road at Toll Naka walking all along NH4. You override your dads advice and reach Kalghatgi Road via MaLmaddi from Belgaum Road. Your brother also overrides and reaches Kalghatgi road via Line Bazaar. You, your dad and your brother - all of you reached Kalghatgi Road from Belgaum Road in three different ways. When method is same and implementation is different, result can be same or different. This is the very essence of virtual functions. Right now please dont bother to recall why you, your dad and you brother concurrently landed at Belgaum road in the first place and then chose to march towards Kalghatgi Road. That can be figured out after you learn OO. If you did not like reference to "you" in the above example, feel free to substitute anybody you like. Irrespective of substitutions virtual functions will be what they are - virtual. Building on our BRA foundation, there is a beautiful way to show how to implement virtual functions. Let us assume that MAHABRA class has a generic method called SING. Default implementation is to sing Marathi stanza of patriotic song "Mile Sur Mera Tumara Sur Bane Hamara ". We know that DEBRA and KOBRA are derived from MAHABRA. So SING method also appears in them. Lets assume that MAHABRA class was lenient enough to make SING a virtual function. Lets learn virtual functions programmatically using D--. MAHABRA Generic Maharastrian Brahmin; // a generic MAHABRA object DEBRA Mamata Kulkarni; //film actress who is a DEshastha BRA KOBRA DaRa Bendre; // our beloved poet who was a KOnkanastha BRA I need to clarify why I chose Mamata Kulkarni for DEBRA as some people may have a better choice. I could not choose Madhuri Dixit because she is a CHIBRA (CHIttpawan BRAhmin or CKP). I could not choose Sonali Bendre because she is a KOBRA and for KOBRA I had made up my mind on our beloved DaRa Bendre who was also a KOBRA. I could not choose Varsha Usgaonkar (a forgotten small time actress) because she is a GOBRA (GOud saraswat BRAhmin or GSB). I could not choose Shirodkar sisters (Namrata or Shilpa) because Shirodkars are not BRAs at all. So, I had to settle with Mamata Kulkarni as my object of type DEBRA. If you dont like Mamata Kulkarni, try to check if Medha Patkar (of Narmada Valley Agitation) is a DEBRA. If yes, you can substitute her for MK. When you invoke SING method (remotely or locally) on Generic Maharastrian Brahmin object, we will end up listening to Marathi stanza of Mile Sur Mera Tumara When you invoke SING method on Mamata Kulkarni object, we will get to listen to enticing, inviting, exciting, heart throbbing and foot tapping number Bholi Bhali Ladki Khol Teri Dil Ki Pyaarwaali Khidhki Ho Ho.... This is one implementation. Mamata Kulkarni object may have a overloaded SING like say SING (Song Type = = Sexy) may play very sexy and chart buster number Koi Aaye to Le Jaaye Meri Laakh Duvaaye Paaye . Let us see what we get by calling SING method on DaRa Bendre object. We will get serene and well-known Kannada song "Paatargitti Pakka Nodiyena Akka " DaRa Bendre object is nothing less compared to Mamata Kulkarni; he also has an overloaded SING method. For example, invoking SING (Song Type = = Romantic) on DaRa Bendre object will play "Nee Heenga Noda Byada Nanna Nee Heenga Nodidara Nanna Tirugi Naa Hyange Nodale Ninna". Whatever we discussed above did not actually illustrate how virtual functions are used at runtime. From above example we just learnt how to implement virtual functions in inherited classes overriding base class implementations. Beauty of runtime use of virtual functions can be illustrated by another example. Study this example carefully, and you will definitely understand virtual functions from this example. Lets assume that Vellupillai Prabhakaran, supremo of LTTE (Liberation Tigers of Tamil Eelam), kidnaps both our Bendre and Mamata Kulkarni so that he can force Srilankan government to free some of his cadres in exchange for hostages. Prabhakaran being a Tamilian does not know anything about Kannada or Marathi. He just knows that both the people he has kidnapped are MAHABRAs. You may ask how Prabhakaran knows DaRa Bendre and Mamata Kulkarni are MAHABRAs. A militant who recently defected from Shivsena to LTTE has briefed Prabhakaran that both of them are MAHABRAs. They are not interested in finer granularity to check if the hostages are DEBRAs or KOBRAs and knowing that their hostages are indeed MAHABRAs serves their purpose well. Prabhakaran holds videoconference with Srilankan Prime Minister Chandrika Kumaratunge (or Ranatunge, or something) and produces his captives before the camera so that government is convinced that he is dead serious. Prime Minister is not too sure if both the captives are safe and are not harmed by LTTE. So, Prime Minister asks Prabhakaran to make both captives SING in front of the camera. As we know Prabhakaran knows that both his captives are MAHABRAs. So he yells in Tamil "Dey MAHABRAs, sing pandra (hey you MAHABRAs sing)". As usual DaRa Bendre object sings in trembling voice Pataragitti pakka Nodiyena Akka and using all her charms (trying to seduce Prabhakaran so that she can escape) Mamata Kulkarni object sings Bholi Bhali Ladki Prabhakaran is flabbergasted to see his captives sing in two different languages despite being objects of the same class (at least he thought so). He asks ex-Shivsena militant, who has learnt some Tamil by now " MaChCha, randu MAHABRA vyare vayre language sing pandra. Randu TAMBRA Tamil -E sing pandra (Saale, two same MAHABRAs sing in two different languages. If you take two TAMBRAs they will sing only in Tamil)". TAMBRA means TAMil BRAhmins. Ex-Shivsena militant who learnt some OO concepts at IIT-Bombay tries to explain to Prabhakaran the beauty of virtual functions and base class pointers. If you call a virtual function of a derived class using base class pointer then derived class implementations are used if they exist. Prabhakaran used base class pointer (MAHABRA) to access same method SING on two objects of derived classes DEBRA(Mamata Kulkarni) and KOBRA(Bendre) which implemented virtual function SING differently. This explains why Prabhakaran was astonished to see these two sing in two different languages. Ex-Shivasena militant is too eager to impart more details but Prabhakaran says in Tamil Ippo Vyanda, Vyanda (Not now please) and concentrates on negotiations. Ex-Shivasena militant feels totally screwed. He was going to explain how VTABLE is used at run time to resolve references to invoke virtual functions and so on but Prabhakaran did not let him do that. After understanding runtime invocation of virtual functions, a non-OO question still remains - Why did Prabhakaran kidnap two poor Indians to achieve his goals? First, DaRa Bendre was on an assignment to pen new national anthem for Srilanka in Kannada. Unable to resolve the difference over language for their national anthem between Sinhalese and Tamil, they chose neutral Kannada language for their national anthem to reach a resolution. Mamata Kulkarni was kidnapped because she was the cheerleader for Srilankan cricket team. As she performed her curvy gyrations to the tune of Koi Aaye to Le Jaaye Jayasurya hammered six after six and Srilankans always won with her glittering presence. Even a pure bowler like Muralidharan who never batted started amassing huge number of runs after Mamata made her explosive entry into Srilankan dressing room. All this tells us how important Mamata Kulkarni is for Srilankan team. Without Mamata at the grounds even for seemingly easy match against Kenya, Srilanka does not have any chance. You can realize how important Mamata Kulkarni and her gyrations are for Srilankan team. Hence, people kidnapped by LTTE are very important at least from Srilankan governments point of view. LTTE always targets right people. How do you compare two objects? It is simple to compare other data types like integers, floats etc. Is comparing objects that simple? Another person like Jasbeer Singh who does not know anything about any Bendre may think that DaRa Bendre and Sonali Bendre are one and the same. He has all reasons to arrive at that inference. He has never heard about DaRa Bendre or Sonali Bendre because only actor/poet/singer that Jasbeer Singh knows is Gurudas Mann who writes and sings(dances) Bhangra songs and acted in one Hindi movie (Ruhani Takat) to qualify as an actor. So, Gurudas Mann is all Jasbeer Singh has cared to know so far. But, Jasbeer Singh knows both Bendre objects are KOBRAs. Since last names are same, he postulates that DaRa and Sonali are two names of the same person, for example DaRa is the name before marriage and Sonali is the name after marriage. This postulation is strengthened by the fact that BRAhmins follow this tradition of changing names. Both of the Bendre objects are equally popular. DaRa Bendre is a renowned poet and Jnanapeetha laureate and Sonali Bendre has to her credit of being cyber-savvy heroine whose pictures make up largest chunk of cyber downloads of any actress. But, Jasbeer Singh really wants to check if DaRa Bendre is equal to Sonali Bendre. So, he writes a program. What can happen here ? If "= = " (equals to) operator is correctly overloaded in KOBRA class, then DaRa Bendre is not equal to Sonali Bendre as Jasbeer Singhs code to check if two Bendres are equal is executed. If some sleep-deprived Desi H1B erroneously coded the overloading of "= = " operator and ditched his body shopper soon after coding without testing then DaRa Bendre may end up being perfectly equal to Sonali Bendre thus resulting in blasphemy of the millennium. By this equality neither of the Bendre objects will be happy. So using a very generic BRAhmin concept we explored different aspects of object orientation. I learnt whatever I could (may be somewhat wrongly though) about object orientation using this methodology. I failed only when I tried to teach OO to new college hires in my group - Deborah Dumpster and Barbara Stillsingle. Every time I uttered DEBRA meaning DEshastha BRAhmin, Deborah told "Come on, you can call me just Deb" and every time I uttered BOBRA meaning BOmbay BRAhmins, Barbara objected "My name is Barbara not BOBRA as you pronounce. You orientals (Asians) have a funny accent. Dont you?" So, exercise caution while using this material to teach OO to people who have never heard of BRAhmins. Another emerging aspect of OO is patterns and anti-patterns. A cursory glance at patterns seemed that it also could be learnt using this methodology. Lets see if it really works that way. Good luck with learning other OO concepts using BRA, KOBRA, MAHABRA, DEBRA, BOBRA, HOBRA etc. |
| -Bra-H-Min (Bra_H_Min@hotmail.com) (Note: No claim is made for the correctness of OO principles. Like our constitution, which we amend indiscriminately rather than amending ourselves, I have taken liberty to amend OO principles to suit my blabber. All names, characters and situations are fictitious or cited just to serve as placeholders without meaning any resemblance to any living or dead. All sensitive terms including but not limited to Brahmin are used with all the due respect they command.) |