Search This Blog

Monday, October 26, 2009

Basic concepts of OOPS

Objects:
Object is the basic unit of object-oriented programming. Objects are identified by its unique name. It is an instance of a Class. There can be more than one instance of class. Each instance of class can hold its own relevant data.
An Object is a collection of data members and associated member functions also known as methods or member function.
Classes:
Classes are data types based on which objects are created. Objects with similar properties and methods are grouped together to form a Class. Thus a Class represent a set of individual objects. Characteristics of an object are represented in a class as Properties. The actions that can be performed by objects becomes functions of the class and is referred to as Methods.

For example consider we have a Class of Vehicle under which car,bus, truck and van represents individual Objects. In this context each Vehicle Object will have its own, Model, Year of Manufacture, Colour, Top Speed, Engine Power etc., which form Properties of the Vehicle class and the associated actions i.e., object functions like Start, Move, Stop form the Methods of Vehicle Class.

No memory is allocated when a class is created. Memory is allocated only when an object is created, i.e., when an instance of a class is created.
Data Abstraction:
Data Abstraction increases the power of programming language by creating user defined data types. Data Abstraction also represents the needed information in the program without presenting the details.

Data Encapsulation:
Data Encapsulation combines data and functions into a single unit called Class. When using Data Encapsulation, data is not accessed directly; it is only accessible through the functions present inside the class. Data Encapsulation enables the important concept of data hiding possible.

Inheritance:
Inheritance is the process of forming a new class from an existing class or base class. The base class is also known as parent class or super class, The new class that is formed is called derived class. Derived class is also known as a child class or sub class. Inheritance helps in reducing the overall code size of the program, which is an important concept in object-oriented programming.

Polymorphism:
Polymorphism allows routines to use variables of different types at different times. An operator or function can be given different meanings or functions. Polymorphism refers to a single function or multi-functioning operator performing in different ways.

Overloading:
Overloading is one type of Polymorphism. It allows an object to have different meanings, depending on its context. When an exiting operator or function begins to operate on new data type, or class, it is understood to be overloaded.

About Constructor

A constructor is a function that initilizes the members of an object. A constructor only knows how to build an object of its own class.
Constructors aren't automatically inherited between base and derived classes. If you don't supply one in the derived class, a default will be provided but this may not do what you want.

If no constructor is supplied then a default one is created by the compiler without any parameters. There must always be a constructor, even if it is the default and empty. If you supply a constructor with parameters then a default will NOT be created.

Some points about constructors

Constructors are just functions with the same name as the class.
Constructors are intended to initialize the members of the class when an instance of that class is created.
Constructors are not called directly (except through initializer lists)
Constructors are never virtual.
Multiple constructors for the same class can be defined. They must have different parameters to distinguish them.

Sunday, October 25, 2009

Static Constructor in C# and their Usages

Static constructor is used to initialize static data members as soon as the class is referenced first time, whereas an instance constructor is used to create an instance of that class with keyword.

C# supports two types of constructor, a class constructor (static constructor) and an instance constructor (non-static constructor).

Static constructor is used to initialize static data members as soon as the class is referenced first time, whereas an instance constructor is used to create an instance of that class with keyword. A static constructor does not take access modifiers or have parameters and can't access any non-static data member of a class.

Since static constructor is a class constructor, they are guaranteed to be called as soon as we refer to that class or by creating an instance of that class.

You may say, why not initialize static data members where we declare them in the code. Like this:

private static int id = 10;
private static string name = "kant";

Usages:

Static data members can certainly be initialized at the time of their declaration but there are times when value of one static member may depend upon the value of another static member. In such cases we definitely need some mechanism to handle conditional initialization of static members. To handle such situation, C# provides static constructor.

Examples:

//File Name : Test.cs

using System;
namespace Constructor
{
class Test
{
//Declaration and initialization of static data member
private static int id = 5;
public static int Id
{
get
{
return id;
}
}
public static void print()
{
Console.WriteLine("Test.id = " + id);
}
static void Main(string[] args)
{
//Print the value of id
Test.print();
}
}
}

In the above example, static data member is declared and initialized in same line. So if you compile and run this program your output would look similar to this :

Test.id = 5

Lets create one more class similar to class Test but this time the value of its static data member would depend on the value of static data member of class Test.id.

//File Name: Test1.cs
using System;
namespace Constructor
{
class Test1
{
private static int id ;
//Static constructor, value of data member id is set conditionally here.
//This type of initialization is not possible at the time of declaration.
static Test1()
{
if( Test.Id < id =" 20;" color="blue">else
{
id = 100;
}
Console.WriteLine("Static Constructor for Class Test1 Called..");
}
public static void print()
{
Console.WriteLine("Test1.id = " + id);
}
static void Main(string[] args)
{
//Print the value of id
Test1.print();
}
}
}

As you can see in the above static constructor, static data member is initialized conditionally. This type of initialization is not possible at the time of declaration. This is where static constructor comes in picture. So if you compile and run this program your output would look similar to this:

Static Constructor for Class Test1 Called..
id = 20

Since in class Test was initialized with a value of 5, therefore in class Test1 got initialized to a value of 20.

Some important point regarding static constructor from C# Language Specification and C# Programmer's Reference:

1) The static constructor for a class executes before any instance of the class is created.
2) The static constructor for a class executes before any of the static members for the class are referenced.
3) The static constructor for a class executes after the static field initializers (if any) for the class.
4) The static constructor for a class executes at most one time during a single program instantiation
5) A static constructor does not take access modifiers or have parameters.
6) A static constructor is called automatically to initialize the class before the first instance is created or any static members are referenced.
7) A static constructor cannot be called directly.
8) The user has no control on when the static constructor is executed in the program.
9) A typical use of static constructors is when the class is using a log file and the constructor is used to write entries to this file.

Access Modifiers in C#


C# Access Modifier

Meaning

Public

Marks a member as accessible from an object variable as well as any derived classes.

Private (default)

Marks a method as accessible only by the class that has defined the method. In C#, all members are private by default.

Protected

Marks a method as usable by the defining class, as well as any derived classes. Protected methods, however, are not accessible from an object variable.

Internal

Defines a method that is accessible by any type in the same assembly, but not outside the assembly.

protected internal

Defines a method whose access is limited to the current assembly or types derived from the defining class in the current assembly.

Unmarked members are private by default in C#.

class SomeClass

{

// Accessible anywhere.

public void PublicMethod(){}

// Accessible only from SomeClass types.

private void PrivateMethod(){}

// Accessible from SomeClass and any descendent.

protected void ProtectedMethod(){}

// Accessible from within the same assembly.

internal void InternalMethod(){}

// Assembly-protected access.

protected internal void ProtectedInternalMethod(){}

// Unmarked members are private by default in C#.

void SomeMethod(){}

}