RajendraYerra

Tuesday, May 02, 2006

Object Oriented Programming in c#

Object Oriented Programming in C#

C# is designed for the efficient building of object-oriented software components.
Software components built in C# are built using classes. Key class constructs
Including properties, methods, events, and interfaces are defined natively in the C# language and are supported directly by the .NET platform. Now we look at how object-oriented programming is implemented in C#

Object-Oriented Features of C#

C# is, foremost, an Object-Oriented Programming language. The exact
Definition of what constitutes an object-oriented language is rather diverse. At its
Core, however, an object-oriented language is one that can merge both data and
Functions into one “black box” known as an object.
In this, C# implements an Object-Oriented Programming to the fullest extent. In C#,
Everything from the simplest to the most complex data type is an object. This is
Because everything derives from System.Object (everything except System.Object,
That is). In fact, if you write a C# class and don’t specify any base class, the resulting
Object still derives from System.Object. In other words, declaring class Employee
Without the explicit base class, as in:

class Employee
{
...
}

Is exactly the same as declaring the same class with the System.Object
Specified in:

class Employee : System.Object
{
...
}

Object-oriented principals are used to such an extreme that even hard-coded
Integers support the methods found on System.Object. For example, in C#, you can
Code 5.ToString( ) and it returns a value. In this case, the string is simply “5” but,
Nonetheless, the code does compile.

Object-Oriented Programming Features

Inheritance
Abstraction
Polymorphism
Encapsulation


Now let us start our discussion on all these Concepts



Inheritance

Inheritance represents the relationship between two objects—A and
B— such that B is an A. For example, if object A was an Employee and object B was a Doctor, then you could create a class hierarchy, such that B derived from A
Because a doctor is a type of employee. The key in the inheritance relationship is
That by deriving object B from object A, object B inherits all the characteristics of
A. This is known as extending the base class. For example, if object A has a public
function called Name( ), then object B would also have this function.

// build command: csc employee1.csc
//
public class Employee
{
public void Name()
{
//put the code that retrieves the employee name here
System.Console.WriteLine ("employee name");
}
}
public class Doctor : Employee
{
}
public class EntryPoint
{
public static void Main()
{
Employee e = new Employee();
Doctor d = new Doctor();
e.Name();
d.Name();
}
}

.NET does not support an advanced form of inheritance called multiple inheritance.
At least .NET doesn’t support multiple inheritance between classes. With a class,
Objects cannot be derived directly from more than one class. For example, because
a hospital employee could be both an employee and a patient, it could potentially
be derived from the two classes Employee and Patient. However, this data
Structure isn’t possible with either C# or .NET. Chapter 5 includes a full
Discussion of this potential short fall. But, for the moment, we can consider that
.NET does support multiple inheritance with interfaces, even if not with classes.

NOTE: There is no class multiple inheritance in C#.

Abstraction

To understand the value of Interfaces, which is another key part of the C# language,
Consider another object-oriented principal, Abstraction. The purpose of abstraction is to Separate the behavior of an object from the implementation of that object. Consider, for Example, a function Call( ) applied to an employee. The implementation for how to call May be reasonably specific to the type of employee being called. For example, to call a doctor, a special answering service is required. This is different from a nurse, which might have a direct-dial hospital extension. Both of these implementations of Call( ) could be different from the janitor, however. In general, all employees have the function Call( ), but there is a difference in how this function is implemented within each employee type. The concept of abstraction separates out the fact that an object has a particular method (Call( )) from the implementation of exactly how that method is performed.

In the .NET world, as was the case with the COM paradigm, abstraction is
implemented through the use of abstract classes and interfaces.
The defining characteristic of an abstract class is that it cannot be instantiated.
Therefore, you cannot use the new operator on an abstract class. Abstract classes,
instead, define the methods and properties a deriving object must implement and the
deriving object is what can be instantiated. Abstract classes can support both public
and private methods, as well as properties. In addition, an abstract class can include
implementation code for its methods, exactly as a normal class does. Just like a
normal class, abstract classes don’t support multiple inheritance.

An interface is a description of the services an object provides to its client.
Because there is not much use in exposing services in private methods that clients
cannot access, all methods and properties on interfaces are defined as public. The
interface defines a contract between the client and the server. Both must abide by its
provisions for the client to use the server. If the client doesn’t use the same function
signatures that the server publicly declares, then the contract between the client and
the server is nullified and the call fails. Interfaces are a type of abstract class
(although not all abstract classes are interfaces). That is, interfaces are classes that
cannot be instantiated directly.


NOTE: All methods and properties on interfaces are defined as public automatically because their purpose is to expose the behavior of the object.

The one other defining characteristic of an interface—and the one that separates it
from an abstract class—is that interfaces cannot include any implementation code. In
this manner, interfaces only define the metadata, the properties and methods, to be
required from any deriving classes.

NOTE: Interfaces cannot have any implementation code. They only contain the property and function signatures that deriving classes require.

Let’s return briefly to the concept of inheritance and combine it now with interface
Abstraction. In .NET, abstraction can be achieved via interfaces from which classes
derive to support the behavior that the interface defines. For example, you can define
an interface, such as IEmployee, and two classes that derive from this interface, such
as Nurse and Doctor. By placing a method called call( )on the IEmployee interface,
you can force the Nurse and Doctor classes to implement the method.

// Build command: csc /t:library Interface1.cs
//
interface Iemployee
{
string call();
}
class Nurse : IEmployee
{
public string call()
{
}
}
class Doctor : IEmployee
{
public string call()
{
}
}

Deriving classes from interfaces is no different than deriving classes from other
base classes, except for one important characteristic. In the .NET world, multiple
inheritance, although not supported between classes, is supported at the interface level.

In other words, it’s possible to derive a class from two different interfaces. Taking our
earlier hospital employee example, we could create two interfaces—IEmployee and
ISalary—and have the Doctor class (or an IDoctor interface) derive from both of
them. The C# code is shown in the following code listing.


// build command: csc interface2.csc
//
namespace HospitalEmployees
{
using System;
interface IEmployee
{
string call();
}
interface ISalary
{
void CalculatePay();
}
class Nurse : IEmployee, ISalary
{
public string call()
{
}
public void CalculatePay()
{
Console.WriteLine("Pay the nurse");
}
}
class Doctor : IEmployee, ISalary
{
public string call()
{
}
public void CalculatePay()
{
Console.WriteLine("Pay the doctor");
}
}
}
As already discussed, the example wouldn’t compile if both IEmployee and
ISalary were declared as classes, rather than interfaces.


Polymorphism

A third key concept in object-oriented programming is known as Polymorphism.
Breaking the word down yields the meaning “many forms.” Essentially, polymorphism
is the capability of a single class to behave in multiple ways. Polymorphism can be
applied in two ways—inclusion polymorphism and operation polymorphism— both of which are supported by .NET.

Inclusion polymorphism is directly implemented via interface inheritance or through virtual function overrides. For interface inheritance, given two objects A and B that are both derived from interface C, casting either A or B to an object of type C, and then calling a method on C, is possible. In this way, variables of type C may refer at run time to two different classes. The call is polymorphic because the exact implementation of the method changes, depending on whether the object cast to C is of type A or B. Let’s return to the hospital for another example to help clarify the concept. In this example, we again have the two employee types—doctors and nurses—and both are derived from the interface IEmployee. The
Code follows:

// build command: csc Interface3.cs
//
using System;

namespace HospitalEmployees
{
interface IEmployee
{
string call();
}
interface ISalary
{
void CalculatePay();
}
class Nurse : IEmployee, ISalary
{
public string call()
{
}
public void CalculatePay()
{
Console.WriteLine("Pay the nurse");
}
}
class Doctor : IEmployee, ISalary
{
public string call()
{
}
public void CalculatePay()
{
Console.WriteLine("Pay the doctor");
}
}
class EntryPoint
{
static void SendMessage(IEmployee employee)
{
employee.Call();
}
public static void Main()
{
Nurse nurse = new Nurse();
Doctor doctor = new Doctor();
SendMessage(nurse);
SendMessage(doctor);
}
}
}
In this code, both the Doctor and the Nurse are passed to the function
EntryPoint.SendMessage( ). This casts them each to IEmployee. However, when
SendMessage( ) is called, the outputs for each function are different because the
implementation is different. This is one way in which C# achieves inclusion
polymorphism.

Inclusion polymorphism can also be achieved through the use of virtual functions.
Given class C, which implements a virtual method V, classes that derive from C, A,
and B can override the virtual function to perform object-specific actions. When A
or B is cast to C, calls to V from C are dispatched to either A or B. The following
example achieves the same inclusive polymorphism result as the previous example,
except it uses a virtual method.
// build command: csc Employee2.cs
//
namespace HospitalEmployees
{
using System;
class Employee
{
public virtual void Call()
{
Console.WriteLine("calling employee");
}
}
class Nurse : Employee
{
public override void Call()
{
Console.WriteLine("calling the nurse");
}
}
class Doctor : Employee
{
public override void Call()
{
Console.WriteLine("calling the doctor");
}
}
class EntryPoint
{
public static void Main()
{
Employee e = new Employee();
e = new Nurse();
e.Call();
e = new Doctor();
e.Call();
}
}
}

In this example, a particular employee instance, whether it be a Doctor or a
Nurse, is cast to e, which is of type Employee. Later on in the WriteLine( ) code,
the Employee object e behaves according to the type it was assigned from.
Where the previous example becomes a little hairy is when you consider versioning
of the Employee. Using the previous example, let’s say another assembly, one you
don’t have source code access to, is providing the Employee base class. You decide
your derived classes should have a Shift( ) method to track the employee’s shift. So,
you place a Shift( ) method on your Nurse class. The original developer of the
Employee class might hear of this and decide Shift( ) would be a good thing for all
users of the Employee class to have, so they would add this new method to Employee.
Which Shift( ) method will be used by your classes derived from Employee? By
default, your Nurse class will hide the Shift( ) method in the base class. This is true,
regardless of the type of the member, be it a field, property, or other member type.
If you don’t recompile your code, and you use the new Employee library as is, your
method will hide the method in the base class and virtual dispatching won’t be
performed. If you rebuild your Employee-based components (with no further
modification to the code) using the new library, the C# compiler then generates a
warning, but still hides the base class. You can eliminate the warning by placing either
the new or the override modifier on your class. Using the new keyword continues to
hide the base method.

Overloading

The previous samples illustrate inclusion polymorphism. The second type of
polymorphism is known as operation polymorphism. With operation polymorphism,
no inheritance relationship exists between objects. The most common form of
operation polymorphism uses overloading. With overloading, multiple functions
with the same name are created; however, the parameters on each function vary.
The result is that a programmer can call the function using the function name and the
particular version of the function called is the one that matches the calling signature.
Consider the following example:
// Build command: csc Employee3.cs
//
namespace HospitalEmployees
{
using System;
class Nurse
{
}
class Doctor
{
}
class EntryPoint
{
static void Call(Nurse n)
{
Console.WriteLine("calling the nurse");
}
static void Call(Doctor d)
{
Console.WriteLine("calling the doctor");
}
public static void Main()
{
Nurse nurse = new Nurse();
Call(nurse);
Doctor doctor = new Doctor();
Call(doctor);
}
}
}
Notice that Nurse and Doctor no longer derive from Employee. Instead, two
different Call( ) functions have been created: one takes Nurse as a parameter and the
other takes Doctor. The result is Call(nurse) calls static the method void Call(Nurse
nurse) and Call(doctor) calls static the method void Call(Doctor doctor). With operation
polymorphism (overloading), the multiple forms occur at the function level, rather
than at the class level. The same function, defined by its name, takes multiple forms

Encapsulation

For the same reasons that Encapsulation is important to object-oriented programming,
it’s important to .NET components: Encapsulation provides a way to bind together
code and data, keeping both safe from outside interference or misuse. Through
Encapsulation, the user of a component need know only how to interact with the
Component, not how it works. The user of a component doesn’t need to know the
Format and layout of the data within the component and its algorithms. And the user
Needn’t know in what language the module was written. Only the proper method of
Interfacing with the component needs to be understood. Encapsulation hides the
Internal workings of a particular class while, at the same time, providing the capability
of appropriately exposing the methods and properties the developer chooses.
C# supports four scope modifiers that provide five different ways to declare the scope
of a method or property. First, public can be used to declare that a method or property
is available to all objects, regardless of their relationship to the class containing the
method or property. Second, private can be used to indicate that only the class that
owns the method or property can access the method or property. Third, protected is
used to scope a property or method, so only the class owning the method or property
or any of the classes’ decedents can access the property or method. Fourth, internal
is used to scope a property or method within an assembly only. Internal scope can be
used to hide classes from users of an assembly, yet still provide “public” access from
within the assembly. Last, internal protected methods and properties are only
available to the assembly or to derived classes. With the exception of internal, these
scope rules closely match those provided by C++, once again reflecting C#’s heritage.
NOTE
Encapsulation provides a way to bind together code and data, keeping both safe from outside interference or misuse.
In addition to control over scope, the developer also has control over whether a
given class can be derived from using the sealed modifier. Sealed applies to a class
only, and prevents a different class from inheriting it. While there aren’t many
instances in which it’s appropriate, sealed can be used to indicate that a class is
deprecated or that it provides a special static function.

Summary
In This Article we learned about OOP Concepts in C#.

About The Author:
Rajendra Kumar Yerra is working as programmer in a KST, Chennai. He completed his Engineering in Computer Science from JNTU, Hyderabad.











1 Comments:

  • At 7:02 AM, Anonymous Anonymous said…

    Excellent article. I am going through some of these issues as well.
    .

    My page: Anxiety Attack

     

Post a Comment

<< Home