www.MasterCsharp.com Logo  AksTech Ad
 Welcome to MasterCSharp.com - Master C#, the easy way... - by Saurabh Nandu

 

 

Quick Overview of Object Oriented Programming
   
 

 

[Rate this Article]
Introduction
I would like to start off this article with a disclaimer: This article is not meant to teach you object oriented programming. There are many books/sites dedicated to this topic, and I am certainly not the authority who can do justice to this deep subject. In this article Id like to refresh some of the core concepts of Object Oriented Programming and put them in a perspective that I understand best.
The goal is to give a small refresher on Object Oriented Programming before diving into learning C# which is an object oriented language.
So if you know Object Oriented Programming please skip this article.

Past, Present and Future (yeha right!) as I know it (which is not much)
Do I see eyes rolling on the mention of the word history? Well Ill make it short!
One of the goals of software programming has always been to take a problem and write a program what solves it. As the problems started getting complex, so did their solutions (programs written to solve them) also started getting complex.
Languages like C are procedural languages, they break down a problem into multiple functions which try to solve one part of the problem each. A C program contains a set of functions which ideally represent one part of the problem each to solve the complete problem.
It was realized that although this method worked (and still works) it really becomes very complex to understand and manage large C programs with functions as the unit of break down.

In order to solve this problem, one of the popular new ways of thinking that arose was Object Oriented Programming. One of the basic premises of this paradigm is that it modeled based on the real world, so it lets us break the problems down just as we observe in the real world. The idea behind using Object Oriented Programming is that that it helps break down the problem into simpler units that can be easily understood, maintained and reused; The keywords which every CTO looks to derive out of his solutions. An example could be suppose we want to describe the bicycle in an object oriented way. So we could say Bicycle is an object that is built of small objects like bicycle frame, handle, breaks, seat and tyres.

Although object oriented programming has been around for a long time its hardly used correctly, hence the benefits promised by the paradigm rarely derived. One of the reasons I see is that Object Oriented Programming is sensitive to context, the way the objects are designed really depends the context in which they get designed. This hampers the ability to reuse the same objects in a different context. Taking the same bicycle example someone else might think of a different context and try to describe the bicycle into the following objects: Transport Vehicle, Manual, Two Wheeler, Bicycle, bicycle frame, handle, breaks, seat, gears and tyres. This kind of classification as well as the one given above are two possible ways out of the many other ones that are possible. So the best way is to think the context of your program and design your objects based on that. With the understanding that if your objects need to participate in another context in future you will have to undertake some kind of refactoring.

As new problem solving techniques emerge object oriented programming is considered to be the base for a lot of these new techniques that are built to overcome the problems faced by programmers today.
I believe that object oriented thinking definitely helps the developer think clearer and design his solution better. And its essential for one to make the effort to learn this skill before getting into writing production code.

What is Object?
An object is a core packaging unit of state and behavior in object oriented programming, modeled after real world objects. The idea is to visualize the problem you are trying to solve in real world terms and trying to identity various real world objects and its components that would be required to define the solution set for the problem. For example if we were to create a software program to replicate the function of an electrical switch. We would identify a switch object with the mutually exclusive state of On or Off as well as a behavior to Turn On and Turn Off the switch.
As I mentioned above its very important to keep the context in mind while defining the objects. While trying to define solutions for complex problems more complex objects would need to be defined as well as sometimes you might need to define relationships between the objects.

What is a Class?
A Class is the software blueprint of an object containing the state as well as behavior of the object. Hence a Switch class would define a variable for the On/Off state as well as methods TurnOn and TurnOff to define the behavior of the object switch.

There is really a fine line to understand the difference between an object and a class. One way to understand is that a class is the software definition of an object. So when you instantiate a class, we say that you create an object. So, the Switch class defines the blueprint of a switch. But there could be many switch instances that you create from the Switch class, all of them have similar state and behavior but are independent. Each one of them could be called as objects of the switch class.

What is State?
Every object demonstrates some state/property for example a switch can have On/Off state, but there could be more states of the switch like its color, style, manufacturer etc. In real world all objects demonstrate some state, like cars can be of certain kind, color, model etc. All these define the state of the object. In software terms, State of the object is represented by variables (fields) and properties. It is important to note that not all state is visible from outside the object.
For example, from outside you might be able to know the speed of a car by looking at its speed property, but you might not be able to determine the position of the piston of the engine. But, the engine would defiantly be tracking the position of the piston but it does not choose to expose that state to the outside world. Similarly in software an object can have both public and private state and its the object creators decision to only expose some of the states of the object to the outside world while hide some other state which is internal to it. This is achieved using access modifiers on variables and properties. The ability to hide some internal state of the object is also referred as Data Hiding.

What is behavior?
Just as every object has a state it also exposes some behaviors. Like a car can be accelerated, a switch can be turned on/off all these are behaviors of the respective objects. In software terms behaviors are represented by methods of a class. Similar to state, even behavior can be internal to the object or external. Its the object creators responsibility to define which behaviors he wants to expose to the outside world and which behaviors he wants to keep internal to the object. Just like in case of state, there are access modifiers on methods that help to define the access to the methods.

What is Encapsulation?
The packaging of state and behavior within a class is called encapsulation. The reason it is important is that now its the class that explicitly defines the state and behavior it want to expose to the outside world at the same time protecting access to its internal state and behavior. Hence the class really becomes a container that is in charge of all the state and behavior of an object and makes sure that its always in consistent state and behavior.

What is Inheritance
When we look at relationships amongst objects in real world we can often clearly see a commonality amongst objects of a particular type. Most of the times these common features are due to an explicit parent-child kind of relationship amongst objects. For example all trees even though they are different have a common features of having leaves, trunk and roots. So we could say that a particular banyan tree by virtue of being a tree inherits the properties of having leaves, trunk and roots. Although, at the same time the size, texture, color would differ from other trees. Hence while each tree has its own identity, by virtue of it being a child of Tree it has some basic common properties.
The same concept gets applied in software, where we try to identify the common state and behaviors demonstrated by various objects and then create a class that acts as the base (parent) class of the objects. So in a banking system the Accounts class would be the parent class of the various other types of account classes like Savings Account, Current Account etc.

Conclusion
In this article I have briefly brushed some of the core Object Oriented Programming concepts. I would definitely recommend that refer to a good book if you have no idea of Object Oriented Concepts.

 

  
Saurabh Nandu - 14 February 2005


Your Ratings / Comments
     
 

[Go to Top]

How many cups of coffee is this article worth??

Rating (Bad)-(Excellent)

Your Name
Your E-mail  
Your Message (Optional)

Viewer Ratings/Comments
Rating Description
4 - asad naeem on 7/4/2005 12:16:00 AM
4 - mary on 8/3/2005 12:59:00 AM
1 asdfsdfsadf - fsdafsd on 10/10/2005 10:44:00 PM
5 - wilson on 12/14/2005 8:47:00 AM
5 Good article. Compacted information, written with a mature language. - masken on 8/20/2006 12:45:00 PM
4 - Tim Gondek on 9/2/2006 10:25:00 AM
2 ACCEPTABLE ONLY ..NEED TO ORDER MORE CUP OF KOPI! - SUPER C# PROGRAMMMER on 9/14/2006 10:08:00 PM
5 It is a good article But including some pictures might have made the article more interactive - Thiagarajan on 11/24/2006 3:08:00 AM
4 thanking you. sir i need more practical tutorial. - bappa sarkar on 11/29/2006 6:14:00 AM
5 very good for beginers - Lokesh on 12/11/2006 1:53:00 AM
4 Very helpful, but grammer mistake: The reason it is important is that now its the class that explicitly defines the state and behavior it want to expose to the outside world at the same time protecting access to its internal state and behavior should be "The reason it is important is that now is the class that explicitly defines the state and behavior it wants to expose to the outside world also protects access to its internal state and behavior." In the conclusion, you are missing the word "you." "I would defintely recommend that YOU refer..." - Tiff on 12/15/2006 6:38:00 AM
5 Useful for beginners - Rengaseshan V on 12/20/2006 10:59:00 PM
5 I love this stuff. - Raghvendra Sharma on 2/12/2007 4:01:00 AM
5 This is a very good artical i realy like the language you have used.. its just like a teacher taking a personal class.. with max of his attention. - Umesh Todkar on 3/8/2007 1:56:00 AM
3 Good effort in putting the things in luicid style. in conclusion you would have included some link which points to comprehensive coverage of Object Oriented Programming. Thx for your effort - Srinivas Pemmaraju on 3/31/2007 6:57:00 PM
4 The material given is really very informative. - Jyoti Ranjan on 4/17/2007 8:34:00 AM
5 good into - bmy on 6/14/2007 4:29:00 PM
5 Every useful information.. I am new to c# and find this page to be a great help. In fact this is one of the best pages I've found to teach me about the stucture of c#. Very Good Job!! Im printing this 4 sure ;) P.s. I agree with "Tiff" about the grammer mistakes, those need to be fixed. - Chris on 7/9/2007 5:28:00 PM
4 Very good metaphors for describing the abstract concepts of OOP. Would be nice to see a few code examples too make the metaphors even more concrete. - David Dockhorn on 8/7/2007 2:02:00 PM
5 Nice article written very clearly for the beginner. - Aelantha on 11/27/2007 10:36:00 AM
4 very helpful for beginners - Bhargavi on 1/23/2008 1:42:00 AM
3 First bit was ok, but then I lost it when it talked about classes variables etc - Shaun Baird on 1/28/2008 9:25:00 AM
5 Short and Sweet Information - Ankit on 2/13/2008 3:06:00 AM
5 I'm still new with programming and I'm going to become one still. I'm trying to understand the basic of programming and you have helped me a lot already, thanks - willem on 2/27/2008 9:08:00 PM
5 good article to brush up - jersy on 8/13/2008 6:29:00 AM
5 Good article - Mushari on 10/6/2008 2:13:00 AM
2 Very very simple! not exhaustive. Good for beginners - Krishnakumar S on 11/30/2008 11:13:00 PM
5 Thanks ,it's miraculous - Mourhaf on 12/21/2008 2:13:00 AM
5 U try well to exploit those such terms in OOP but more realistically i m going to say that More easy defination of Object is it is the reference of a class Class always call By referance so it is store in heap.Class is the Structure of the associated object(s) .every Behavior and its properties written down in class an Modifier used for encapsulation or the scope of the Method and as well as properties. - Ahsan Fraz on 1/14/2009 4:45:00 AM
5 very nice article maximum no of programmers does not know how execution process takes in .net application internally by this site it will help for all those guys - sumendar on 1/19/2009 1:32:00 AM
5 Thanks for the comparisons. It definitely helps understand the concept. - Clint Erdman on 3/10/2009 8:21:00 PM
1 ok - shag on 4/14/2009 7:50:00 AM
4 Good info ! - ash on 6/5/2009 2:34:00 PM
5 Very comprehensive article. :-D Congratulation!!! - LengIeng on 9/26/2009 9:38:00 PM
5 I am mainframe programmer. Your article helped me a lot. Thank you. - Mark on 1/7/2010 12:22:00 PM
5 xCLnt - ram on 1/29/2010 4:12:00 AM
4 good article, but wouldn't logic dictate that the more cups of coffee you spend reading an article the worse it is? :) - newbie22 on 2/5/2010 7:34:00 AM
4 good article.. - Shehbaz on 3/16/2010 7:14:00 AM

[Go to Top]


 
 
  Copyright © 2002 - 2004 MasterCSharp.com. All rights are reserved.

  Presenting MasterCSharp.com in association with AksTech Solutions - .NET Solutions Development and Consulting. 

  Best Viewed in IE 4.0+ and 800x600 Resolution