Learn about Object Oriented Programming in C#



In the previous article, we started dealing with C# as a true object oriented programming language. We briefly discussed the merits of this type of programming paradigm compared to the traditional procedural one.

The object is the basis of any OOP application, and, as mentioned in the previous article, it contains properties and methods that are used to define what it is and how it works. Properties and methods are often called object members. Access to those members is controlled by accessibility modifiers. This is a set of rules that organize reaching object members from code. They can be summarized as follows:

  • Private: members are available to only code within the same class
  • Protected: members are available to code in children classes (classes that are inheriting from this class)
  • Public: members are available to any code in the application

Now let’s have a closer look at how objects get created (instantiated).

Object construction
Every class has a method called constructor. This is where any initialization logic should be placed. For example, you may want to create an objStudent object from a Student class, and because all students in the application are – by default – members of the Computer Science department, you want the department property of the object to read “Computer Science” unless someone changes it.

In Visual Studio, right click on the application name in the Solution Explorer window (for example ConsoleApplication1) and click Add then Class, or just press SHIFT+ALT+C. Choose Student as the name of the class to add it to the application. Write the following code inside it:



A constructor method always bears the name of the class. So Student() is the constructor of the Student class. The constructor method gets called when you use the new keyword to instantiate the object. Now, if you run this code you will find that the department field of the objStudent object is holding “Computer Science” as a default value instead of an empty string. Yet this is not the only form of the object constructors, they can take arguments too. In fact, you can place more than one constructor in the same class and let the user choose how he wants the object to be instantiated. For example, perhaps you want to give the user the option to fill give default values to all the fields in the class before he starts using it. Let’s change the code in Student.cs to be as follows:

Here, we are giving the user the option to initialize all the fields of the object while it is instantiated. It is optional because you also placed an empty constructor. So, the class may be called using either of the methods. But if you want to force the user to initialize the fields, you would omit the default constructor Student().

Notice the use of the this keyword. It is a reference to the current class. Think of it as if you are saying: “I am referring to the firstname field that is a member of this class”.

Finally, you can prevent a class from being instantiated by declaring its constructor method as private. By definition, private methods are not available for any code outside their class, which means that the class will never get instantiated.

Static classes
Static in English means stagnant; something that does not move, and that’s exactly what static classes are: they provide their functionality without the need of instantiating objects from them. The main advantage of such a behavior is that their members are share. Compare this to the default behavior of non-static classes: the properties of each instance is specific to that instance only. To illustrate this, consider the following example:

If you run this code, the output will be:

objStudent1 and objStudent2, although instantiated from the same class Student, can bear different values for their member fields. However, the static class Teacher can have only value for its member field name.

You can also use static methods and fields both in a static as well as non-static classes. For example you can create a register() method in a student class and call it directly from the class like this:

But notice that static methods, by definition, do not have access to non-static fields. That’s straight forward because non-static fields are not shared, they are available only when an object gets instantiated.

Suppose you are creating an application that deals with cars. Any car must have drive(), reverse(), and stop() methods. So whether it is a truck or a van class it must expose those methods to any code that calls it.

An interface is a contract, or a promise that a class makes. You can define an interface the same way you define a class, just replace the class keyword with the interface one. You can also right click on the name of the application in the properties window, choose Add and select New Item, then from the list of available items choose interface. A car interface might look as follows:

Wait a minute, where are the curly braces and the method body? Did you just place a semicolon after the method name? Yes I did. And that’s how interfaces are defined: just the method names followed by semicolons as any normal C# statement. Let’s make some use of this interface. Create a new class and call it Truck. Place the following code inside it:

Notice the colon (:) after the class name followed by the interface name ICar. This colon instructs the class that is going to implement the ICar interface. That is it must expose all the methods contained in that interface. Failing to add any of the implemented methods will raise a compile time error and the code will not run.

And because methods in interfaces cannot have a body, it is the responsibility of the implementing class to provide the intended behavior for those methods. For example, a Truck must be able to pull over, but of course an eight-wheeled vehicle will use the brakes differently than a sports car. So the end result is the same, but the implementation is different.

Interfaces are useful when you are dealing with foreign code; code that you did not write. In our example, any developer who knows that Truck class implements the ICar interface, is assured that reverse(), stop(), and drive() methods are available and can be used without breaking the application.

Another useful aspect of interfaces is that a given class can implement more than one interface at the same time. A Truck class can implement an ICar and an IVehicle interface at the same time.

Ok, welcome to the amazing world of OOP. We’re just getting started! Today, you saw how an object gets instantiated using the new keyword and how the constructor method provides a handy way to place any startup code like initializing the member fields to some default values. Then, you learned that you don’t have to instantiate an object to be able to use a class, as static classes do not (and cannot) be instantiated, yet they offer the functionality of their member methods and fields if they are also created as static. Finally, we introduced interfaces as a way to promise users of your class that this class will always implement methods that are listed in a special class-like file called an interface. An interface contains just the method names and leaves assignment entirely to the class that implements them.

In the next article, you will see an important example of interfaces; the IDiposable. Then, you will discover other pillars of OOP: inheritance and polymorphism so stay tuned.


Please enter your comment!
Please enter your name here