OOP IN DOT NET

1. What is Object Oriented Programming?

           Object oriented programming(OOP) is a standard programming model which is based on objects. If you see dot net framework, for example, it based fully on Object Oriented programming.  Dot net framework contains huge library of classes in the form of dll. We create the objects of these classes, executes various methods present in these classes with the help of these objects.

           Abstraction, Encapsulation, Polymorphism, Inheritance are the pillars of OOP. We will learn these terms in later session.

ADVANTAGES OF OOP
1.    Code is reusable and not redundant
2.    Faster execution of code
3.    Hide implementation details from real world(Other module, Clients, User of the code)
4.    Easy to maintain the code
5.    Efficient use of Memory.

2. What is a class?

A class is a basic unit of OOP. It encapsulates Data, fields, properties, methods(Operation/Procedure).
We can create any number of objects of the class and access the data, fields ,properties, methods of the class. 
click here for an example.

3. What is Encapsulation?

            Encapsulation means combining related data and functions into a single entity. The best example of encapsulation is class and what is a class? A class is nothing but a combination of Data, function, properties.

4. What is Abstraction?

            Abstraction means showing only essential functionality to the external world and hiding the complexity. What is that means to the external world?
            Lets take an example that we have in class definition above, the sqlConnection class where it provide ConnectionString property to us i.e. developer who uses this class and we refer here developer as external world. Now here we need to pass only the connection string of a database to which we want to connect and we don't have to worry about how to connect to a actual database. Dot net framework will work at background and connect to the database using the connection string that we passed. So here dot net framework hiding the complexity of connecting to the database and providing only simple property.
            How we can achieve the abstraction in a class? By using the access specifier. We will discuss different access specifier in later section but before that we must know what is inheritance which is explained in next section.

5. What is Inheritance?

           Dictionary definition of Inheritance means inheriting some properties of others. Same concept is used for classes in OOP. We have a base/parent class and we have other child classes derived from this base class, so these derived classes inherits some characteristics of the base class(Not private fields) along with its own characteristics. lets see the below example.

+
Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace Inheritance_by_example
{
    //Base class
    public class Company
    {
        public string companyName;
        public string companyLocation;
    }
 
    //Employee class which is derived from base class Company
    public class Employee : Company
    {
        public string employeeName;
        public string Designation;
    }
 
    public class getEmployees
    {
        Employee emp = new Employee();
 
        void setemployee()
        {
            //base class Fields
            emp.companyName = "Infosys";
            emp.companyLocation = "Mumbai";
 
            //Derived Employee class Fields
            emp.employeeName = "Amit";
            emp.Designation = "Senior Engineer";
        }
    }
}

           In the above example we have Company base class which has 2 fields declared. We have another class Employee which is derived from this base class. when we create the object of the Employee class  in getEmployees class, we can see that we can access the base class fields as well using this derived class object. So this derived class inherits the fields of the base class.
           In dot net framework also you find inheritance concept used everywhere. e.g. All the webpages that we include while developing web application are derived from base class Page, Windows forms that we include in our windows application are derived from base class Form.

6. What are the Access Specifier?

           Access specifiers are used to restrict access to the classes from external world(i.e. client, other developer)who are using these classes in their application.
           There are five access specifiers available in dot net framework and we can use these access specifiers on any class fields, methods or properties or even on class itself  (Class can have only public and internal access speicifier).

1. Public: Items declared with this access specifier are available in the class where it has declared as well as outside that class anywhere in any application.

2. Private : Only accessed within a class where it has declared and not outside of that class.

3. Protected : Only accessible within a class and a class which is derived from this class.

4. Internal : These are like public access specifier, but are only available to any class in same assembly(dll) only and not outside the assembly(dll).

5. Protected Internal : These are available to any class in same assembly and available to only derived class outside the assembly.

Click here for Example.

7. What is Polymorphism?

           Poly means 'many' and morphism means forms. If we look at dot net framework, we can see that many classes contains the same method declared multiple times but it has different parameters and that is nothing but polymorphism where we can declare multiple methods with same name but with different parameters and we call it as method overloading.
          Let us see below example of polymorphism by method overloading where i have created one console application and in that i have one class called polymorphism_Example which contains Add method used 5 times with different parameters.

+
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int result;
 
            polymorphism_Example polyEx = new polymorphism_Example();
            result = polyEx.add(2,2);
 
            Console.WriteLine(result);
            Console.ReadLine();
        }
    }
 
    public class polymorphism_Example
    {
        public int add(int i, int j)
        {
            return i + j;
        }

        public float add(int i, float j)
        {
            return i + j;
        }
 
        public float add(float i, int j)
        {
            return i + j;
        }
 
        public string add(string a, string b)
        {
            return a + "" + b;
        }
 
        public string add(string a, string b, out int i)
        {
            i = 3;
            return a + "" + b;
        }
    }
}


When I access this method in my main method in above code sample when I type object name following method name and gives open bracket i can see 5 overload as circled red in below screenshot. Depending on the parameters you pass to this function the corresponding function will get executed. You can try above example with different values.







Laptop Repair Made Easy - Hd Video Series..Click Here!

1 comment :

Anonymous said...

Good Way it Describes the OOP Concept in Dot Net