Object Oriented Programming in C#

(Object-Oriented Programming) OOP concept is a must to be a good programmer. This is the important design principle, In coders’ life. OOP is all about how to write a code in an organized code structure. This design principle gives you a good knowledge of coding skills. Rather than going through technical terms, I am going to help you to understand this concept through real-life examples.

 Principle OOP concepts are
  • Class
  • Object
  • Abstraction
  • Inheritance
  • Polymorphism
  • Encapsulation
Let me explain one by one.

Class

Class is a blueprint of an object. This is a mandatory concept in OOP. If you consider the real world, a class is like our school bag. We filled it with books, water bottle, geometry box, pencil box, and so on. Class is a collection of objects. Class does not contain specific memory allocation.

public class Registration{
}

Object

The object is an entity or model of a class. It may be a chair, table, or a person. Technically an object is an instance of a class. When the new object is created, the memory will be allocated on top of the heap for a class.




public class Registration{

}
public static void Main(string args []){    
Text Box: This the way we create an object

      Registration regi = new Registration();

}

Abstraction

Abstraction is a class that is used to hide the basic functionality of the user. Let us consider our image sharing process through Bluetooth. What we need is to share our desired images with our friend’s phone. Here we do not care how to share those images through Bluetooth. That information is hidden from the user. We used the abstract keyword to create the Abstract class. We cannot create instances from the Abstract class as we create an instance to Class. This is the main difference between Class and Abstract Class.

Abstract class BlueTooth {
    void sendImages(){
        Console.WriteLine("Successfully shared images");
    }
    void sendVideos(){
        Console.WriteLine("Successfully shared videos");
    }
}

So, like this way we create an Abstract class in c#. As I said we cannot create an instance for Abstract class. Therefore, instead of creating an instance, we used to inherit the Abstract class to another class.

Abstract class BlueTooth {
    void sendImages(){
        Console.WriteLine("Successfully shared images");
    }
    void sendVideos(){
        Console.WriteLine("Successfully shared videos");
    }
}
class SumsungGalaxy : BlueTooth {
    void takePhoto();
    void internetConnection();

    static void Main(string args []){
        SumsungGalaxy smg = new SumsungGalaxy();
        smg.sendImages();
        smg.sendVideos();
        smg.takePhoto();
    }
}

Consider the Console.WriteLine() in C#. It contains lots of methods and operations. But, we are not aware of those functions. We just use that method to print the data. In this way, Abstraction is hiding critical and irrelevant information and provides relevant information to the user. We just focus on the performance of the object instead of how it does the task. This is how Abstraction is used in OOP. Abstraction used to solve the problem at the design level. It provides an outer layout of the design.

Inheritance

When a class uses another class’s property like object, methods, it is simply defined as Inheritance. Let us consider our family; we are transferring some genetic characteristics throughout ancestry like skin color, hair type, face features and etc. In that way, the child class uses all the properties from the parent class. So we group inheritance concepts as Base class (parent class) and Derived class (child class).

public class MobilePhone {
    public void call(){
        Console.WriteLine("You have called Successfully");
    }
    public void sms(){
        Console.WriteLine("You have sent a message Successfully");
    }
}
public class SumsungGalaxyS3 : MobilePhone {

    void takePhoto();
    void playGame();
    void videoCall();

    public static void Main(string args []){
        SumsungGalaxy smg = new SumsungGalaxy();
smg.call();
        smg.sms();

        smg.takePhoto();
        smg.playGame();
        smg.videoCall();
    }
}
I hope through this example you clearly understand the concept of Inheritance. So, let us move on to the next concept which is Polymorphism.

Polymorphism

If one function is performed in so many different ways, it is simply defined as Polymorphism. Let assume that you are a business analyst. Think, how will you explain your product’s functionalities to your customer? You are supposed to explain through videos, animation, PowerPoint, and so on. Isn't it?.  Here, explaining your idea is the main function, but you explain in so many different ways. This is called Polymorphism
Polymorphism having two types in C#
  1. Static Polymorphism (Compile time polymorphism)
  2. Dynamic Polymorphism (Runtime polymorphism)
Let us check that one by one.

01. Static or Compile Time Polymorphism

This is a concept of method overloading. If a class has methods or functions which are having the same name but different signatures (parameters), called method overloading. During the compile time, it decides which method or function should be called. Therefore, it is referred to as compile-time polymorphism.  

public class FindArea {
    public int area(int a, int b){
        return (a * b);
    }
    public int area(int radius){
        return (3.14* Math.Pow(radius,2));
    }
}
public class Program {
    public static void Main(string args []){
        FindArea a = new FindArea();
        int rectangle = a.area(5,4);
        double circle = a.area(2);

        Console.WriteLine(rectangle);
        Console.WriteLine(circle);
    }
}

02. Dynamic or Runtime Polymorphism

This is the concept of method overriding. Here method’s name or function’s name and its parameters (signatures) are the same but it contains different implementations. Through this we are going to use the same concept of Inheritance, but in a different way.

It has base and derived classes. Derived class has the same name and same parameter as base class. While compiling time, the compiler is not aware of the implementation. It’s just check the method’s name. Therefore, compiler never throws an error message. During the runtime only the compiler will decide which method should be called. If that method does not exist, then it will throw an error message. Due to this functionality, it is called runtime polymorphism.
public class ProductFeature{
    public virtual void explainFeature(){
        Console.WriteLine("This is the new feature which we added recently!");
    }
}
public class Animation : ProductFeature {
    public override void explainFeature(){
        Console.WriteLine("Explain feature through animation");
    }
}
public class PowerPoint : ProductFeature {
    public override void explainFeature(){
        Console.WriteLine("Explain feature through Powerpoint slides");
    }
}
public class Videos : ProductFeature {
    public override void explainFeature(){
        Console.WriteLine("Explain feature through videos");
    }
}
public class Program{
    public static void Main(string args []){
        ProductFeature exp = new ProductFeature();
        ProductFeature ani = new Animation();
        ProductFeature ppt = new PowerPoint();
        ProductFeature video = new Videos();

        exp.explainFeature();
        ani.explainFeature();
        ppt.explainFeature();
        video.explainFeature();
    }
}
Through this above code, you can notify I used keywords such as virtual and override. A virtual keyword is allowed to modify the method or function of a base class and to be overridden by a derived class. Override keyword is allowed to modify or extend the virtual method of the base class.

Encapsulation.

This is the next concept of OOP. It is wrapping the data and methods as a single unit. We can simply say that it is hiding the internal details of an object.

Let's consider our DVD player. It is encapsulated with a cover. We never operate the DVD by opening the back cover. We just operate through the remote controller. So like this way encapsulation is working on the OOP concept. Technically, encapsulation is a trick to protect the information from one object to another object.

public class DVDPlayer {
    changeVolume();
    changeChannel();
}
If you want to hide the complex function and provide the security to your code, you can use the encapsulation. It is used at the implementation level. It provides the inner layout to the implementation. For example, How the phone connects with another phone once we press the dial button. Throughout the process, it will cross so many internal and external firewall boundaries. But, we are never aware of those processes. But, we just utilize that facility. Isn’t it? In this way, encapsulation performs.

Comments

Popular posts from this blog

Easy understanding of MVC design architecture

A / B Testing

Firewall