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: 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) { 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
{
} 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. Both overloading and overriding can be implemented in C# using the code shown below: class sampleClass
{ Output of this code will be: In sampleClass 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#.
|