Powered by Blogger.

Saturday, March 8, 2014

Object Oriented Programming (OOP) concepts in .Net




In this article we will discuss about Object Oriented Programming (OOPs) concept in .Net.

Also you can check out:

- How to handle exception in sql server stored procedure?

- Tutorials on WCF in Asp.Net

- Working with enterprise library for data access in asp.net Part-3

Mainly there are 3 things in OOPs concept.

1- Inheritance
2- Abstraction
3- Polimorphism

1- Inheritance:
Inheritance is one of the key feature of oops concept and C# supports inheritance. Inheritance is a process of deriving the new class from already existing class. It allows you to reuse existing code.

A color (:) symbol is used for inheritance like below:
class ChildClass: ParentClass
              {
                   //Body
              }
Note: In .Net every class is inherit from the System.object class

There are different types of inheritance available in C#.Net like below:

- Single Inheritance
When a single derived class is created from a single base class then the inheritance is called as single inheritance.

- Hierarchical Inheritance
When more than one derived class are created from a single base class, then that inheritance is called as hierarchical inheritance.

- Multi Level Inheritance
When a derived class is created from another derived class, then that inheritance is called as multi level inheritance.

- Hybrid Inheritance
Any combination of single, hierarchical and multi level inheritances is called as hybrid inheritance.

- Multiple Inheritance
When a derived class is created from more than one base class then that inheritance is called as multiple inheritance. But multiple inheritance is not supported by .net using classes and can be done using interfaces.

2- Abstraction:
Abstraction is a process of hiding the implementation details and displaying the essential features.

For Abstraction .net provides different access specifiers like below:
Public: Accessible outside the class through object reference.

Private: Accessible inside the class only through member functions.

Protected: Just like private but Accessible in derived classes also through member functions.

Internal: Visible inside the assembly. Accessible through objects.

Protected Internal: Visible inside the assembly through objects and in derived classes outside the assembly through member functions.

3. Polymorphism:

Polymorphism means having more than one form. In C#.Net Overloading and overriding are used to implement polymorphism.

Polymorphism is classified into compile time polymorphism or early binding or static binding and Runtime polymorphism or late binding or dynamic binding.

Compile time Polymorphism or Early Binding:

The polymorphism in which compiler identifies which polymorphic form it has to execute at compile time it self is called as compile time polymorphism or early binding.

Runtime Polymorphism or Late Binding:

The polymorphism in which compiler identifies which polymorphic form to execute at runtime but not at compile time is called as runtime polymorphism or late binding.

Overloading Example:

class MethoOverLoadingDemo
    {

        public int Sum (int X, int Y)
        {
            return X + Y;
        }

        public float Sum(int X, float Y)
        {
            return X + Y;
        }
    }

Here Sum method has been overloaded.

Overriding Example:

class BaseClass
    {
        public virtual  string Hell0()
        {
            return "Hello !!!";
        }
    }

    class DerivedClass : BaseClass
    {
        public override string Hell0()
        {
            return "Hello !!! Welcome to AspDotNetHelp.com";
        }
    }



2 comments

santhosh kumar December 22, 2016 at 9:37 PM

The blog gave idea about oops concept in dotnet Thanks for sharing it
Dot Net Training in Chennai

vignesjoseph June 1, 2017 at 9:41 PM

We sharing the OPPs concepts using .Net for loop, inheritance, IF statement, Polymorphism, polymorphism in which compiler identifies method all concepts coding very useful to us.I'm a workout all example I've to learn for new ideas.
Selenium Training in Chennai
Dot Net Training in Chennai

Post a Comment