Brief About OOPs Concepts In C#

Introduction

In the Object Oriented Programming [OOPs], we only talk about in terms of object. In programming language like C#, we use logically to store data as class, struct etc. but physically these are objects.

Here are the features of OOPs.

Class

Class is nothing but a blue print. It is a way to represent the data. It contains member variable to store the data, member functions to perform operation. It does occupy any space in memory whether it is logical representation of data. When we create instance of class using the new keyword, the object of class take the space in memory.

Example of Class

  1. public class Customer  
  2. {  
  3.    //Fields, properties, methods and events go here...  
  4. }  
You can learn in deep about the C# classes here.

Encapsulation

Encapsulations mean encapsulate the data or bind the data or we can also say wrap the data in one unit. In the OOPs programming we use the classes to store the variables, events, function, etc. So, basically here we are binding the data in one group which is called class. The process of wrapping up of data in the single unit is called data encapsulation.

Example of Encapsulation
  1. class Counter  
  2. {  
  3.     private int Count;  
  4.   
  5.     public Counter()  
  6.     {  
  7.         Count = 0;  
  8.     }  
  9.   
  10.     public void setCount(int newVal)  
  11.     {  
  12.         Count = newVal;  
  13.     }  
  14.   
  15.     public int getCount()  
  16.     {  
  17.         return Count;  
  18.     }  
  19. }  
You can learn in deep about Encapsulation.

Abstraction

Hiding the data to outer world is called Abstraction. It means sometimes it is required to hide some data to outer logic, here abstraction comes in demand. For example, if we want to hide a member variable and create it private, then it will not be accessible to the outer world. It is only accessible to class. So, this is called Abstraction. It is used to provide the security of data.

Example of Abstraction
  1. public abstract class ShapesClass  
  2. {  
  3.     abstract public int Area();  
  4. }  
  5. public class Square : ShapesClass  
  6. {  
  7.     int side = 0;  
  8.   
  9.     public Square(int n)  
  10.     {  
  11.         side = n;  
  12.     }  
  13.     // Area method is required to avoid  
  14.     // a compile-time error.  
  15.     public override int Area()  
  16.     {  
  17.         return side * side;  
  18.     }  
  19.   
  20.     static void Main()  
  21.     {  
  22.         Square sq = new Square(12);  
  23.         Console.WriteLine("Area of the square = {0}", sq.Area());  
  24.     }          
  25. }  
You can learn in deep about Abstraction.

Inheritance

Inheriting the features of existing parent is called Inheritance. In C#, when we create a new class from an existing class and inherit all the features of parent class in the child class, then we can say inheritance is happening.

Reusability is a main concern of inheritance. The class, whose functionality is going to inherit is called Parent class or base class or super class and the class which inherit the feature of base class is called child class, derived class.

Example of Inheritance
  1. public class Animal  
  2. {  
  3.     public virtual void Greet()  
  4.     {  
  5.         Console.WriteLine("This is an animal");  
  6.     }  
  7. }  
  8.   
  9. public class Dog : Animal  
  10. {  
  11.     public override void Greet()  
  12.     {  
  13.         Console.WriteLine("This is a dog!");  
  14.     }  
  15. }  
You can learn in deep about Inheritance.

Polymorphism

Polymorphism means one name, many forms. We can achieve Polymorphism with the the help of overloading and overriding concepts. There are two type of polymorphism, first one is compile time polymorphism and second one is run time polymorphism. We use virtual and override keyword to achieve polymorphism.

Example of polymorphism
  1. class Shape  
  2. {  
  3.     protected int width, height;  
  4.     public Shape(int a = 0, int b = 0)  
  5.     {  
  6.         width = a;  
  7.         height = b;  
  8.     }  
  9.     public virtual int area()  
  10.     {  
  11.         Console.WriteLine("Parent class area :");  
  12.         return 0;  
  13.     }  
  14. }  
  15. class Rectangle : Shape  
  16. {  
  17.     public Rectangle(int a = 0, int b = 0)  
  18.         : base(a, b)  
  19.     {  
  20.   
  21.     }  
  22.     public override int area()  
  23.     {  
  24.         Console.WriteLine("Rectangle class area :");  
  25.         return (width * height);  
  26.     }  
  27. }  
Read more about polymorphism

Hope you enjoyed this article.

 


Similar Articles