OOP (Object Oriented Programming) is a programming concept to design or write a program which works on the principal of object. It means OOP organized around Object only. Object would become the most important part of your program. Now a day, most widely used programming languages C++, JAVA, C#, Objective-C, Python etc. uses OOP concept. Following are the features that OOP provides,

  1. Class
  2. Object
  3. Inheritance
  4. Polymorphism
  5. Abstraction
  6. Encapsulation

Class

A Class is a blueprint or template or a set of instruction to build a specific type of object. In different words, Class is an entity which determines the behavior of an object and shows what object will contain. Sometimes Class is called a collection of Data Members (Variable) and Member Functions (Functions or Methods).

To create a class, you should simply have to use keyword “Class” followed by class name.

public  class Learners {

     /* Data Members and Member Functions */

}

As a programmer, you have already used a class. For example, when you use any control (such as Textbox, Label etc.) while creating website, software or mobile application, that controls has already defined as a class.

Object

An object is an instance of a class. It means Object is required to access Data Members and Member Function of a class. Object holds behavior of whole class. One class can have multiple objects.

Syntax to create an object of class Learner:

Learners objLearners = new Learners();

Inheritance

Inheritance is again a concept where one class can acquire a members and behavior of its parent class. It is a parent-child relationship between two classes, where child class can use members of parent class except the constructors. In single word, it is a concept of Reusability.

For Example:

public class InheritanceDemo

{

    public static void Main() ()

    {

        ChildClass objChild = new ChildClass();

        objChild.ChildMethod(); /* Child class method */

        objChild.ParentMethod(); /* Parent class method */

        Console.ReadLine();

    }

}

class ParentClass

{

    public void ParentMethod()

    {

        Console.WriteLine("I am Parent Method..!!");

    }

}

class ChildClass : ParentClass

{

    public void ChildMethod()

    {

        Console.WriteLine("I am Child Method..!!");

    }

}

Output

// I am Parent Method..!!

// I am Child Method..!!

Polymorphism

Polymorphism means one name, multiple forms. One function or method behaves in different forms. In OOP more than one function can have same name, but behavior of all methods are different according to their number of arguments or return type. Method Overloading and Method Overriding are an example of polymorphism.

Types of Polymorphism:

1. Compile-Time Polymorphism

The type of polymorphism that is implemented when the compiler compiles a program is called Compile-time polymorphism or Early Binding. Method Overloading is a type of Compile-Time Polymorphism.  In method overloading, more than one method having a same name, but their number of parameters (arguments) and return type should be change. Compiler will decide to call a method based on arguments and return type of called method while compilation.

Example:

public class OverloadingDemo

{

    public static void Main()

    {

        OverloadingDemo demo = new OverloadingDemo();

        demo.addition(10);

        demo.addition(10, 20);

        demo.addition(10, 20, 30);

        Console.ReadLine();

    }

    public void addition(int value)

    {

        Console.WriteLine("Single parameter addition called..." + (value + value));

    }

    public void addition(int value, int value2)

    {

        Console.WriteLine("Two parameter addition called..." + (value + value2));

    }

    public void addition(int value, int value2, int value3)

    {

        Console.WriteLine("Three parameter addition called..." + (value + value2 + value3));

    }

}

Output

// Single parameter addition called...20

// Two parameter addition called...30

// Three parameter addition called...60

2. Run-Time Polymorphism

The type of polymorphism that is implemented dynamically when a program being executed is called Run-Time Polymorphism or Late Binding. Method Overriding is a type of Run-Time Polymorphism. Method Overriding occurs when working with inheritance and a method with same name, parameter (argument) and return type has been declared in both Parent and Child class. C# makes use of virtual and overrides keywords to accomplish Method Overriding.

public class OverridingDemo

{

    public static void Main()

    {

        ParentClass p;

        p = new ParentClass();

        p.Display();

        p = new ChildClass();

        p.Display();

        Console.ReadLine();

    }

}

class ParentClass

{

    public void Display()

    {

        Console.WriteLine("I am Parent Method..!!");

    }

}

class ChildClass : ParentClass

{

    new public void Display()

    {

        Console.WriteLine("I am Child Method..!!");

    }

}

Output

// I am Parent Method..!!

// I am Parent Method..!!

Above example shows overriding of Display() method. In above example first I have created object p of ParentClass  and calling its method Display(). Then I have used same object p to create object of type ChildClass and calling child class method Display(). But every time I get same output as given above, means Display() method of parent class invoked for both call. Since object p contains a reference to object of type ChildClass, one can expect the function Display() of ChildClass get executed. But output is different than expectation. To achieve this we need to use virtual and override keywords.

Overriding with virtual and override keyword

public class OverridingDemo

{

    public static void Main()

    {

        ParentClass p;

        p = new ParentClass();

        p.Display();

        p = new ChildClass();

        p.Display();

        Console.ReadLine();

    }

}

class ParentClass

{

    public virtual void Display()

    {

        Console.WriteLine("I am Parent Method..!!");

    }

}

class ChildClass : ParentClass

{

    public override void Display()

    {

        Console.WriteLine("I am Child Method..!!");

    }

}

Output

// I am Parent Method..!!

// I am Child Method..!!

As seen in above example, Display() method in ParentClass has been declared with “virtual” and in ChildClass Display() method has been declared with “override” keyword. By using virtual and override keywords, object p of ParentClass call Display() method of ParentClass and object p of ChildClass call Display() method of ChildClass.

Abstraction

Dictionary meaning for Abstraction is “The quality of dealing with ideas rather than events”.

In software engineering Abstraction means to represent the essential feature without representing the background details. In simple words, Abstraction means hiding the internal details of class (implementation details) and showing its functionality to user through object in understandable manner.  Abstraction can be achieved using Abstract Class and Abstract Method.

Abstract Class

A class that is declared with “abstract” keyword is called Abstract Class. Abstract class cannot be instantiated but it can be inherited. This class can have abstract method (Method with an abstract keyword) and concrete method (Method without an abstract keyword).

The purpose of abstract class is to provide non-implemented method for multiple derived/child classes to implement them according to their usage. For example, if multiple classes have same method/signature to implement with different definition (body), then the method can be declared in abstract class with abstract keyword, inherited with multiple classes and programmer can write their own implementation code.

Abstract Method

A method without body or without implementation is call abstract method. These methods are declared with abstract keyword and must be declared in an Abstract Class. Abstract method doesn’t have implementation, but when abstract class having abstract method is inherited by other class, that child class must have to implement abstract method.

Rules for Abstract Method

  • Abstract method declared with abstract keyword and it doesn’t contain body (implementation).
  • If any class has abstract method then class must be declared with abstract keyword. But abstract class can have both abstract and non abstract methods.
  • If any class inherits abstract class, then child class must have to implement all abstract method of parent class.
  • “override” keyword is used while overriding abstract method.

A sample program to explain abstract class and method

public class Abstraction

{

    public static void Main()

    {

        Square objSquare = new Square();

        objSquare.draw();

        Rectangle objRectangle = new Rectangle();

        objRectangle.draw();

        Console.ReadLine();

    }

}

abstract class Shape

{

    public abstract void draw();

}

class Square : Shape

{

    public override void draw()

    {

        Console.WriteLine("I am drawing Square..!!");

    }

}

class Rectangle : Shape

{

    public override void draw()

    {

        Console.WriteLine("I am drawing Rectangle..!!");

    }

}

Output

// I am drawing Square..!!

// I am drawing Rectangle..!!

Encapsulation

Wrapping up or binding data members and member functions into one unit is called Encapsulation. Class is a best example for Encapsulation. It is also used hiding data and protecting data from the object of other classes.

Abstraction and Encapsulation is advanced features of OOPs. Reading at first place, it seems like both are same, but both are different.  The difference is, Encapsulation is used to hiding class members from outside class and Abstraction is used to show only relevant or essential features.

Encapsulation can be achieved by “Access Specifiers”. An Access Specifiers are use to define scope and visibility of class members. Following are access specifiers,

  • Public
  • Private
  • Protected
  • Internal
  • Protected internal

Encapsulation example,

public class EncapsulationDemo

{

    public static void Main()

    {

        Learners objDept = new Learners();

        objDept.setLearnerName("Mitesh Bhavsar");

        string Name = objDept.getLearnerName();

        Console.WriteLine("Learner's Name is " + Name);

        Console.ReadLine();

    }

}

public class Learners

{

    private string _LearnerName;

    public void setLearnerName(string LearnerName)

    {

        _LearnerName = LearnerName;

    }

    public string getLearnerName()

    {

        return _LearnerName;

    }

}

Output

// Learner's Name is Mitesh Bhavsar