Search This Blog

Saturday, June 14, 2014

Object Oriented Programming (OOPS) Concepts in C#

Class:
  1. A grouping of related objects.
  2. A blueprint used for producing objects having a similar set of attributes & behavior, but which can be uniquely identified.
  3. A user defined data type.
public class Employee
{
    protected int empID;
    protected string name;
    protected string address;
      
      public void display()
      {
         Console.WriteLine("Employee Class");
     }
}


Object:

  1. A real world entity that is identified by simulating the human thought process.
  2. A Real world entity that has
  •   State
  •   Identity &
  •   Behavior
Employee emp new Employee();

"emp" is the object of "Employee" class.




OOPS concepts:

1) Abstraction:
  1. Abstraction is
    • What you expose to the end user
    • What an Object does
  2. Abstraction refers to the description of externally visible behavior of an Object.
  3. Abstraction is used to describe what an Object does rather than how it does it.
  4. Abstraction is a process of hiding the implementation details and displaying the essential features.
Example: Consider an ATM machine. 
We are exposed only the screen with various options through which we can do many kind of transactions like check balance, withdraw amount, etc.
We don't need to know how it internally processes this complex transactions by checking the Total amount available, withdrawal amount not more than available amount, Net amount left is not less than minimum balance limit, etc.
So, the user friendly screen with various options is exposed to the end user and this refers to abstraction.


2) Encapsulation:


  1. Encapsulation is
    • What you hide from the end user
    • How an Object does what it does
  2. Encapsulation is a description of the internal data structure & implementation that gives rise to the externally visible behaviour.
  3. Encapsulation hides the details of the implementation of an Object.
Example: Consider an ATM machine. 
We are exposed only the screen with various options through which we can do many kind of transactions like check balance, withdraw amount, etc.
We don't need to know how it internally processes this complex transactions by checking the Total amount available, withdrawal amount not more than available amount, Net amount left is not less than minimum balance limit, etc.
So, the complex transactions done by the ATM internally is hidden from the end user and this refers to encapsulation.



3) Inheritance:

  1. Extend rather than modifying
  2. Do not break what is already working
  3. Reuse rather then re-invent
  4. Inheritance enables you to create a new class that reuses, extends, and modifies the behavior that is defined in another class.
Public class Parent
{
    Public Parent()
    {
        Console.WriteLine ("Parent class");
    }
                                 
    Public void Display()
    {
        Console.WriteLine ("Parent method");
    }
}
                                 
Public class ChildParent
{                                 
    Public Child()
    {
        Console.WriteLine("Child Class");
    }
   
    Public static void Main ()
    {
        Child obj = new Child();
        obj.Display();
    }
}

Output:
  Base Class
  Child Class
  
Parent method


3) Polymorphism:
  1. One form – multiple behaviour.
  2. Enabling the client to select which code block to execute dynamically without breaking the client.
  3. Enables the client to dynamically replace the object to be operated upon without the breaking the client.
  4. Classes provide different implementations of methods having the same name. 
  5. Polymorphism is of two types:
    • Method Overloading (Compile time polymorphism)
    • Method Overriding (Runtime polymorphism)


No comments:

Post a Comment