Thursday, November 28, 2013

C# OOPS CONCEPTS

FIRST READ EXAMPLE FOR OOPS CONCEPTS

Before going into the OOPS concepts I would like to give you one scenario based on that the oops concept will be well defined for you!

For example if you attending an interview, the company will get your resume and also they will give you a format of paper to be filled out. On the form it may contains the information you gave along with your resume and also other information required for the company.

here the company has some format and they not updating the information with a single format rather than they getting xerox copy of that and providing to everyone and receiving back the information they need.

                  From the above point of view, the single format of classified application form is a CLASS and number of xerox is said to be an OBJECT in the object oriented language programming.

                       If we need any alteration in the form we do not delete the entire form and create a new one, rather than we get the maximum contents of the previous form and create a new form with necessary columns in the new form, thus by inheriting the value and contents of the previous form is said to be a Inheritance.

                       In this scenario the people filling out the form cannot able to see the other form, is called Abstraction. Hiding of irrelevant data.

                     If the same form is distributed with various names for various different sections is called Polymorphism.
                    Take the above example as a reference only. Whenever you are about to learn any new technologies do not think too much at the initial as you will be in a hurry stage and many thing will confuse you take little time to learn day by day and you will understand once your known things are joined and give you the result.
Most importantly do not think, why it is use rather than think how it should be use!!!



Now we going to see what a oops language in .net environment, 

Class:

          A Class is a collection of object. A main template.

Objects:


         An object contains member attributes and member functions.

      Syntax:

       class  form
    {
           form objectname = new form();     
     }
  In asp.net the each time you drag any controls it actually creates a object.




Encapsulation:

Encapsulation is a process of binding the data members and member functions.

Example for encapsulation is class. A class can contain data structures and methods.
Consider the following class

public class Aperture
{
public Aperture ()
{
}
protected double height;
protected double width;
protected double thickness;
public double get volume()
{
Double volume=height * width * thickness;
if (volume<0)
return 0;
return volume;
}
}

In this example we encapsulate some data such as height, width, thickness and method Get Volume. Other methods or objects can interact with this object through methods that have public access modifier.


Abstraction , C# has five access Specifiers

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.


Inheritance:


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 ChildClassBaseClass
{
                                 
    Public ChildClass ()
    {
        Console.WriteLine("Child Class Constructor executed");
    }
   
    Public static void Main ()
    {
        ChildClass CC = new ChildClass ();
        CC.Write ();
    }
}

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.


Polymorphism:

When a message can be processed in different ways is called polymorphism. Polymorphism means many forms.
Polymorphism is one of the fundamental concepts of OOP.
Polymorphism provides following features: 

  • 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. 
  • Polymorphism is of two types:
    1. Compile time polymorphism/Overloading
    2. Runtime polymorphism/Overriding
    Compile Time Polymorphism
    Compile time polymorphism is method and operators overloading. It is also called early binding.
    In method overloading method performs the different task at the different input parameters.
    Runtime Time Polymorphism

  • Runtime time polymorphism is done using inheritance and virtual functions. Method overriding is called runtime polymorphism. It is also called late binding.
    When overriding a method, you change the behavior of the method for the derived class.  Overloadinga method simply involves having another method with the same prototype.
    Caution: Don't confused method overloading with method overriding, they are different, unrelated concepts. But they sound similar.
      In very simple manner u use the same method name for doing a addition function with different parameters is called overloading we use the same method name for different function is called overriding.

All the best.............................................................................









No comments:

Post a Comment