21 Mar, 2009 · 5 minutes read
Object Oriented Programming is a methodology modeled on real life. It comprises of the basic unit, an object, being used in implementing a program. Think of all the objects around you – cars, buses, birds, trees, etc. You will find countless ones of them, everywhere you go. In order to tackle this huge number, we started classifying them, thus came the term Class.
A class is a composition of the theoretic characteristics (attributes and properties) of an object and the things it can do – behaviors, methods and features. Take the Car as an example. Its characteristics can include speed, color, number of gears, etc and it exhibits behaviors such as accelerate, brake and gear-change.
An object is an instance of a class. Consider the Car class above, the Aston Martin that was used in the movie – Die another Day is an object of the car class. One point to note is that each object has at least one attribute that makes it unique.
Objects do not exist in isolation. They interact with other objects. These interactions take place through messages. Message passing is the process by which an object (sender) sends data to another object (receiver) or asks the other object to invoke a method. Thus, each message has a sender and a receiver.
It is how an object acts and reacts, in terms of its state changes and message passing. For example, when the driver applies the break, the car comes to a stop.
This was all about the object oriented methodology. Now, let us take a look on how OOP is used in C#
We declare classes using the following syntax:-
class< Class Name >
{
data members…
member functions…
}
Here classis a keyword. Class name can be anything that follows the naming rules.
Conventions for naming classes
The conventions are not mandatory but best practices, abiding by which allows others to easily understand our code.
Data members are the variables or other objects declared inside the class while member functions are the functions declared in it.
Example Class:-
class Student
{
// Data Members
public string Name;
public int Age;
// Member Function
public void Display()
{
Console.WriteLine("Name {0}\n Age {1}", Name, Age);
}
}
The publicaccess specifier has been used in order to allow the data members & functions to be visible from outside the class. The access specifiers will be explained in the forthcoming tutorials.
Creating objects is similar to declaring variables and can be done using the syntax:-
Example: Student S1;
Before we start using the object, we need to instantiate it. Instantiation involves memory allocation and other basic initializations contained in the object’s constructor. We use the newkeyword to instantiate objects. The syntax for which is given below:-
Note: Constructor is a special member function of a class that is used to initialize the data members. It will be dealt with, in later tutorials.
Example Usage: S1 = new Student();
These two steps can be done in a single line of code as Student S1 = new Student();
We use the . (Dot) operator to access the members of the object. For example, to call the display method for the student class’s S1 object we write S1.Display();
Data members can be accessed in the similar fashion S1.Name = “Kenshin Himura”;
To release the memory occupied by the object we use S1 = null;
Example Program:-
using System;
namespace CSTutorials
{
class Student
{
// Data Members
public string Name;
public int Age;
// Member Function
public void Display()
{
Console.WriteLine("Name {0}\n Age {1}", Name, Age);
}
}
class Program
{
static void Main(string[] args)
{
Student S1; // Declaration
S1 = new Student(); // Instantiation
Student S2 = new Student(); // Single line Declaration & Instantiation
// Assigning value to member variables
S1.Name = "Michael";
S1.Age = 37;
S2.Name = "Kimi";
S2.Age = 25;
// Invoking member function
S1.Display();
S2.Display();
Console.ReadLine();
}
}
}