What Object Oriented Principles can be incorporated in .NET Application?

Each developer will have different coding style and they might follow different standards. In such cases, when a developer tries to maintain code of another developer then it might be very difficult. Is there a common set of principles which when followed ensures maintainability, reusability, efficiency and integrity of an application? Yes, Object Oriented Programming provides such set of principles. If these principles are followed in your coding then no doubt that your project will be easily maintainable and reusable.

.NET programming languages like C# are object oriented. Hence your .NET application can incorporate all object oriented concepts. Given below are different object oriented principles along with C# examples to demonstrate how these principles can be incorporated in .NET application:

Encapsulation: Covering up of data and methods using that data into a single unit is known as encapsulation. Encapsulation is achieved in .NET application using classes. Here is an example:
class sampleAdd {
int num1, num2;
public void setData(int num1, int num2) {
this.num1 = num1;
this.num2 = num2;
public int Add() {
return (num1 + num2);

In this example, you have the data members (num1, num2) and the methods that access them (setData, Add) inside a single unit (class sampleAdd). Thus you have performed encapsulation.

Abstraction: Aim of object oriented programming is to represent all real world entities into objects. While representing real world entities as objects, you specify only the necessary attributes for your application and hide all other attributes and background details. This is known abstraction. Using abstraction you can represent any real world entity (how much ever complex it is) as an object that is simple to understand and implement.

For example, C# can provide abstraction of real world entity “color” using the color class which is predefined in the C# library and you can color an image using any different color by creating an object of color class. Here is an example to demonstrate it:

public static Bitmap colorImage(Bitmap sampleImage) {
Bitmap sampleBitmap = new Bitmap(sampleImage.Width, sampleImage.Height);
for (int indexWidth = 0; indexWidth < sampleImage.Width; indexWidth++) {
for (int indexHeight = 0; indexHeight < sampleImage.Height; indexHeight++) {
Color sampleColor = Color.FromArgb(110,40,250,120);
sampleBitmap.SetPixel(indexWidth, indexHeight, sampleColor);
return sampleBitmap;

In this example, you can also use colors like Color.Red, Color.Blue directly. Here the object sampleColor models the color of real world in an abstract way.

Inheritance: Inheritance is where a real world entity extends the behavior of another real world entity. For example, think about the real world entiry Car. Car is a generalized entity. When you think about using a car, you will be using different models of the car namely indica, toyota, logon and many others. All of them have the features of Car and are specialized in their own way. Instead of repeating car features in each of the car, you will define car features in a class and you inherit the class into multiple other classes each specifying a particular type of car.

This inheritance is achieved in C# using <derived class>:<base class> syntax as shown below:

class car { … }
class toyota:car { … }
class logon:car { … }

Polymorphism: When an object can take more than one form, then polymorphism is said to occur. There are two types of polymorphism namely static polymorphism and dynamic polymorphism. Both of them can be implemented in your .NET application using the following:

• Static Polymorphism is achieved in .NET using Overloading. Overloading means that same method can have multiple definitions. Number and type of arguments should vary for each definition.
• Dynamic Polymorphism is achieved in .NET using Overriding where in the method in base class can be inherited and overridden by the derived class.

Both overloading and overriding can be implemented in C# using the code shown below:

class sampleClass {
public void displayMsg() {
Console.WriteLine(“In sampleClass”);
public void displayMsg(string data) {
Console.WriteLine(“In sampleClass:” + data);
class derivedClass:sampleClass {
public override void displayMsg() {
Console.WriteLine(‘In derivedClass”);
class testClass {
public static void Main() {
sampleClass obj = new sampleClass();
obj = new derivedClass();

Output of this code will be:

In sampleClass
In sampleClass:Overloading
In derivedClass

In this example, you have a class called sampleClass. In this class, the method displayMsg is overloaded with two different definitions. This is an example of static polymorphism. Then the sampleClass is inherited by derivedClass and in derivedClass you have overridden displayMsg method. This is an example of dynamic polymorphism.

Message Communication: In object oriented programming, you map all real world entities into objects. But how do you establish communication among objects? That is done in C# using the code: <object name>.<method name>(<parameters>). Consider the above example; inside testClass you access method of sampleClass using the following line of code: obj.displayMsg(“Overloading”). This is how you establish message communication in C#.


Add to My Yahoo!

FREE Subscription

Subscribe to our mailing list and receive new articles
through email. Keep yourself updated with latest
developments in the industry.


Note : We never rent, trade, or sell my email lists to
anyone. We assure that your privacy is respected
and protected.

Visit .NET Programming Tutorial Homepage


Recommended Resource

| How do you prevent a class from overriding in .NET? | How are classes related to objects in .NET Application | How are Delegates different from Events in .NET? | How are system exceptions different from application exceptions in .NET? | How are Value Types different from Reference Types in .NET? | How can a finalize method be suppressed in .NET? | How can you call Stored Procedure in ADO.NET? | How can you force Dispose method to be called automatically in .NET? | How do you call a Base Class Constructor from Derived Class Constructor in .NET? | How do you connect your VB.NET application to SQL Server? | How do you implement Cloning in .NET? | How do you implement Façade Design Pattern in .NET? | How do you implement MVC Pattern in ASP.NET? | How do you install .NET Assembly in GAC? | How is shadowing different from overriding in .NET? | How to prevent a particular .NET DLL from being decompiled? | Illustrate Delay Signing Process of an Assembly in .NET? | What are Reference Types in .NET? | What are the advantages of C#? | What are the advantages of VB.NET? | What are the differences between Namespace and Assembly in .NET? | What are the similar features between class and structure in .NET? | What are Value Types in .NET? | What do you mean by mixed mode authentication in .NET? | What do you mean by Satellite Assembly in .NET? | What do you mean by shadowing in .NET? | What is CTS in .NET? | What is ILDASM in .NET? | What is Managed Code in .NET? | What is Manifest in .NET? | What is MSIL in .NET Framework? | What is the importance of finalize method in .NET? | What is the need for Visitor Pattern in C#? | What is the purpose of bindingRedirect tag in web.config file of .NET? | What is the purpose of CodeDom in .NET? | What is the purpose of dispose method in .NET? | What is the purpose of Ngen.exe in .NET? | What is the purpose of Strong Name in COM Components of .NET? | What is the purpose of virtual keyword in .NET? | What Object Oriented Principles can be incorporated in .NET Application? |

| Privacy Policy for www.dotnet-guide.com | Disclosure | Contact |

Copyright - © 2004 - 2017 - All Rights Reserved.