Sunday, February 20, 2011

c# OOP ( C# OOP tutorial )

The need for Object oriented programming (OOP)

A long long time ago (up to the late 1950's) a computer program was essentially a long series of sequential commands. But as software applications got more and more complex, it started getting increasingly difficult to modify or fix an existing software program.
    Consider this example. You are working as an intern for an organization. Suppose all their documents are stacked on top of each other forming a pile of a million documents. And now, they ask you to find the documents among these million documents, that explains the dress code and modify it to include denim as an acceptable office wear. Wouldn't life have been a lot easier if these documents were properly organized in separate drawers in file cabinets, as opposed to being stacked on top of each other? That's what OOP does when it comes to programming. It helps in breaking down the code into many manageable parts.

What are Objects?

  To simplify things, you can think of it this way. When you write a program, you write it in blocks of code called objects. These blocks of code provide functionalities in two ways.
1>Properties:  This object or block of code can have a parameter (or property) that can be read from other code. For example I write a piece of code where I set a property pi=3.142. You can write code where you can get the value of pi from my code.
2>Methods (Functions): I can have a method in my code that takes an input and throws out an output. For example I can write a piece of code (a method) that takes two numbers as input, and spits out their sum as the output. So if you need the sum of two numbers you can pass it into the method I wrote.

What’s explained above is a very simplified version. But it’s a good start.

OOP Concepts

Now let’s delve a little deeper into the world of object oriented programming in C#. Instead of getting into the code, my focus here would be to give you a better understanding of the underlying concepts.

If nature used C# to create the world, then she would have done something like this.

Object Oriented Programming (OOP)

In the diagram look at abstract class Living Being. An abstract class is something that provides a framework to build something. You cannot have an instance of an abstract class. Here the abstract class Living being , tells classes human, elephant and whale that hey, if you want to be alive, you need to implement living being functions such and eating, breathing etc. If you don't implement all of them, you are not a living being.

Coming down the diagram, the black, white and brown races inherit from human race. What that means here is, all these races have features of being a human such as walking upright on two feet, talking, being intelligent and so on. In addition to the features of being a human, they add their own feature, skin color.

Further down, we see that nature has implemented every race. For example, nature has implemented black human race in two instances, Oprah and Mandela.

The reader would notice that the classes elephant and whale, besides inheriting from the abstract class living being, they also inherit from the interface Massive. Like abstract classes, interfaces only provide a framework.  You cannot have an instance of interfaces.

So now the reader may ask, what’s the difference between abstract and interface. The answer is simple. Abstract class can be thought of something that defines what you are, where as interface can be thought of merely a feature. For the same reason, you can derive from multiple interfaces but you can derive from only one abstract class (or any class for that matter).

Before I end this tutorial, let me touch one more concept in OOP. That is polymorphism. In the diagram above, Gandhi, can be converted into an instance of Human. The reason being that Gandhi is an instance of Brown race and the Brown race is derived from Human race. Any instance can be converted to a type of a class which is upstream in its chain of inheritance. (The other way is not true. C# does not support down casting, unless that instance was originally  a subclass to begin with, that was upcasted into the parent). The fact that an instance of a class can take different forms is called polymorphism.

Further Reading:

Object-Oriented Programming (C# and Visual Basic)

Object Oriented Programming Fundamentals (An msdn video tutorial)


  1. That was so clear and simple! Keep up the good work!
    -Dan Rinaldo

  2. Still, classes does not inherit from interfaces.. they implement them

  3. Nice Article ..


Comments will appear once they have been approved by the moderator