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
- public class Customer
- {
-
- }
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 - class Counter
- {
- private int Count;
-
- public Counter()
- {
- Count = 0;
- }
-
- public void setCount(int newVal)
- {
- Count = newVal;
- }
-
- public int getCount()
- {
- return Count;
- }
- }
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 - public abstract class ShapesClass
- {
- abstract public int Area();
- }
- public class Square : ShapesClass
- {
- int side = 0;
-
- public Square(int n)
- {
- side = n;
- }
-
-
- public override int Area()
- {
- return side * side;
- }
-
- static void Main()
- {
- Square sq = new Square(12);
- Console.WriteLine("Area of the square = {0}", sq.Area());
- }
- }
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 - public class Animal
- {
- public virtual void Greet()
- {
- Console.WriteLine("This is an animal");
- }
- }
-
- public class Dog : Animal
- {
- public override void Greet()
- {
- Console.WriteLine("This is a dog!");
- }
- }
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 - class Shape
- {
- protected int width, height;
- public Shape(int a = 0, int b = 0)
- {
- width = a;
- height = b;
- }
- public virtual int area()
- {
- Console.WriteLine("Parent class area :");
- return 0;
- }
- }
- class Rectangle : Shape
- {
- public Rectangle(int a = 0, int b = 0)
- : base(a, b)
- {
-
- }
- public override int area()
- {
- Console.WriteLine("Rectangle class area :");
- return (width * height);
- }
- }
Read more about
polymorphism Hope you enjoyed this article.