Friday, March 04, 2016

OOPS Concepts in C#

Friday, March 04, 2016 Posted by Sandeep Kumar Jha
Object Oriented Programming (OOP) is a programming model where programs are organized around objects and data rather than action and logic.

OOP has the following important features.

Class

A class is the core of any modern Object Oriented Programming language such as C#.

In OOP languages it is mandatory to create a class for representing data.

A class is a blueprint of an object that contains variables for storing data and functions to perform operations on the data.

A class will not occupy any memory space and hence it is only a logical representation of data.

To create a class, you simply use the keyword "class" followed by the class name:

class Employee
{

}

Object

Objects are the basic run-time entities of an object oriented system. They may represent a person, a place or any item that the program must handle.

"An object is an instance of a class"


Example

class Employee
{

}

Syntax to create an object of class Employee:

Employee objEmp = new Employee();

Abstraction

Abstraction is "To represent the essential feature without representing the background details."

Abstraction lets you focus on what the object does instead of how it does it.

Abstraction provides you a generalized view of your classes or objects by providing relevant information.

Abstraction is the process of hiding the working style of an object, and showing the information of an object in an understandable manner.




Real-world Example of Abstraction

Suppose you have an object Mobile Phone.

Suppose you have 3 mobile phones as in the following:

Nokia 1400 (Features: Calling, SMS)
Nokia 2700 (Features: Calling, SMS, FM Radio, MP3, Camera)
Black Berry (Features:Calling, SMS, FM Radio, MP3, Camera, Video Recording, Reading E-mails)

Abstract information (necessary and common information) for the object "Mobile Phone" is that it makes a call to any number and can send SMS.

So that, for a mobile phone object you will have the abstract class as in the following:

   abstract class MobilePhone
    {
        public void Calling();
        public void SendSMS();
    }

    public class Nokia1400 : MobilePhone
    {
    }

    public class Nokia2700 : MobilePhone
    {
        public void FMRadio();
        public void MP3();
        public void Camera();
    }

    public class BlackBerry : MobilePhone
    {
        public void FMRadio();
        public void MP3();
        public void Camera();
        public void Recording();
        public void ReadAndSendEmails();
    }

Encapsulation

Wrapping up a data member and a method together into a single unit (in other words class) is called Encapsulation.

Encapsulation is like enclosing in a capsule. That is enclosing the related operations and data related to an object into that object.

Encapsulation is like your bag in which you can keep your pen, book etcetera. It means this is the property of encapsulating members and functions.

    class Bag
    {
        book;
        pen;
        ReadBook();
    }

Example

    




Inheritance

When a class includes a property of another class it is known as inheritance.

Inheritance is a process of object reusability.

For example


Output


Polymorphism

Polymorphism means one name, many forms.

One function behaves in different forms.

In other words, "Many forms of a single object is called Polymorphism."

Real-world Example of Polymorphism

Example


Output


Constructor

A special method of the class that will be automatically invoked when an instance of the class is created is called a constructor. The main use of constructors is to initialize private fields of the class while creating an instance for the class. When you have not created a constructor in the class, the compiler will automatically create a default constructor in the class. The default constructor initializes all numeric fields in the class to zero and all string and object fields to null. 

Some of the key points regarding the Constructor are:

  • A class can have any number of constructors.

  • A constructor doesn't have any return type, not even void.

  • A static constructor can not be a parametrized constructor.

  • Within a class you can create only one static constructor. 

Constructors can be divided into 5 types:

  1. Default Constructor

  1. Parametrized Constructor

  1. Copy Constructor

  1. Static Constructor

  1. Private Constructor 

Example


Output


Destructors

As we all know, ‘Destructors’ are used to destruct instances of classes. When we are using destructors in C#, we have to keep in mind the following things:

  • A class can only have one destructor.

  • Destructors cannot be inherited or overloaded.

  • Destructors cannot be called. They are invoked automatically.

  • A destructor does not take modifiers or have parameters.

The following is a declaration of a destructor for the class MyClass:

~ MyClass()
{
   // Cleaning up code goes here
}