Wednesday 3 July 2013

OOPS Concepts in asp.net


Class:

 It is a collection of objects.

Object:

It is a real time entity.
An object can be considered a "thing" that can perform a set of related activities. The set of activities that the object performs defines the object's behavior.

Example :
public class Parent
{
}
Parent objParent=new Parent ();



the above sample we can say that Parent object, named objParent, has created out of the Parent class.

Abstraction:

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

Example  1: A Laptop consists of many things such as processor, motherboard, RAM, keyboard, LCD screen, wireless antenna, web camera, usb ports, battery, speakers etc. To use it, you don't need to know how internally LCD screens, keyboard, web camera, battery, wireless antenna, speaker’s works.  You just need to know how to operate the laptop by switching it on. Think about if you would have to call to the engineer who knows all internal details of the laptop before operating it. This would have highly expensive as well as not easy to use everywhere by everyone.

Encapsulation :

Encapsulation is one of the fundamental principles of object-oriented programming.
Encapsulation is a process of hiding all the internal details of an object from the outside world
Encapsulation is the ability to hide its data and methods from outside the world and only expose data and methods that are required
Encapsulation is a protective barrier that prevents the code and data being randomly accessed by other code or by outside the class
Encapsulation gives us maintainability, flexibility and extensibility to our code.
Encapsulation makes implementation inaccessible to other parts of the program and protect from whatever actions might be taken outside the function or class.
Encapsulation provides a way to protect data from accidental corruption
Encapsulation hides information within an object
Encapsulation is the technique or process of making the fields in a class private and providing access to the fields using public methods
Encapsulation gives you the ability to validate the values before the object user change or obtain the value
Encapsulation allows us to create a "black box" and protects an objects internal state from corruption by its clients.

Inheritance:

Inheritance is a process of deriving the new class from already existing class
C# is a complete object oriented programming language. Inheritance is one of the primary concepts of object-oriented programming. It allows you to reuse existing code. Through effective use of inheritance, you can save lot of time in your programming and also reduce errors, which in turn will increase the quality of work and productivity. A simple example to understand inheritance in C#.


Using System;
Public class BaseClass
{
    Public BaseClass ()
    {
        Console.WriteLine ("Base Class Constructor executed");
    }
                               
    Public void Write ()
    {
        Console.WriteLine ("Write method in Base Class executed");
    }
}
                               
Public class ChildClass: BaseClass
{
                               
    Public ChildClass ()
    {
        Console.WriteLine("Child Class Constructor executed");
    }
 
    Public static void Main ()
    {
        ChildClass CC = new ChildClass ();
        CC.Write ();
    }
}

In the Main () method in ChildClass we create an instance of childclass. Then we call the write () method. If you observe the ChildClass does not have a write() method in it. This write () method has been inherited from the parent BaseClass.

The output of the above program is

Output:
  Base Class Constructor executed
  Child Class Constructor executed
  Write method in Base Class executed

this output proves that when we create an instance of a child class, the base class constructor will automatically be called before the child class constructor. So in general Base classes are automatically instantiated before derived classes.

In C# the syntax for specifying BaseClass and ChildClass relationship is shown below. The base class is specified by adding a colon, ":", after the derived class identifier and then specifying the base class name.

Syntax:  class ChildClassName: BaseClass
              {
                   //Body
              }

C# supports single class inheritance only. What this means is, your class can inherit from only one base class at a time. In the code snippet below, class C is trying to inherit from Class A and B at the same time. This is not allowed in C#. This will lead to a compile time
error: Class 'C' cannot have multiple base classes: 'A' and 'B'.

public class A
{
}
public class B
{
}
public class C : A, B
{
}

In C# Multi-Level inheritance is possible. Code snippet below demonstrates mlti-level inheritance. Class B is derived from Class A. Class C is derived from Class B. So class C, will have access to all members present in both Class A and Class B. As a result of multi-level inheritance Class has access to A_Method(),B_Method() and C_Method().

Note: Classes can inherit from multiple interfaces at the same time. Interview Question: How can you implement multiple inheritance in C#? Ans : Using Interfaces. We will talk about interfaces in our later article.

Using System;
Public class A
{
    Public void A_Method ()
    {
        Console.WriteLine ("Class A Method Called");
    }
}
Public class B: A
{
    Public void B_Method ()
    {
        Console.WriteLine ("Class A Method Called");
    }
}
Public class C: B
{
    Public void C_Method ()
    {
        Console.WriteLine ("Class A Method Called");
    }
                 
    Public static void Main ()
    {
        C C1 = new C ();
        C1.A_Method ();
        C1.B_Method ();
        C1.C_Method ();
    }
}

Polymorphism :


Polymorphism means the ability to take more than one form.
An operation may exhibit different behaviors in different instances.
The behavior depends on the data types used in the operation.
Polymorphism is extensively used in implementing Inheritance.
It allows you to invoke methods of derived class through base class reference during runtime.
It has the ability for classes to provide different implementations of methods that are called through the same name.

public class Customer
{
    public virtual void CustomerType()
    {
        Console.WriteLine("I am a customer");
    }
}
public class CorporateCustomer : Customer
{
    public override void CustomerType()
    {
        Console.WriteLine("I am a corporate customer");
    }
}
public class PersonalCustomer : Customer
{
    public override void CustomerType()
    {
        Console.WriteLine("I am a personal customer");
    }
}
public class MainClass
{
    public static void Main()
    {
        Customer[] C = new Customer[3];
        C[0] = new CorporateCustomer();
        C[1] = new PersonalCustomer();
        C[2] = new Customer();
        foreach (Customer CustomerObject in C)
        {
            CustomerObject.CustomerType();
        }
    }
}

Output:
I am a corporate customer
I am a personal customer
I am a customer
Method Overloading ( Compile Time Polymorphism):
Method with same name but with different arguments is called method overloading.
Method Overloading forms compile-time polymorphism.
Example of Method Overloading:

      class A1
        {
            void hello()
            {
                Console.WriteLine("Hello");
            }

            void hello(string s)
            {
                Console.WriteLine("Hello {0}", s);
            }
        }

     

Method Overriding (Run Time Polymorphism) :
Method overriding occurs when child class declares a method that has the same type arguments as a method declared by one of its superclass.
Method overriding forms Run-time polymorphism.
Note: By default functions are not virtual in C# and so you need to write “virtual” explicitly. While by default in Java each function are virtual.
Example of Method Overriding:

        class parent
        {
            virtual void hello()
            {
                Console.WriteLine("Hello from Parent");
            }
        }

        class child : parent
        {
            override void hello()
            {
                Console.WriteLine("Hello from Child");
            }
        }

        static void main()
        {
            parent objParent = new child();
            objParent.hello();
        }


No comments:

Post a Comment