OOP is one of the most important programming methodologies nowadays. The whole concept depends on four main ideas, which are known as the pillars of OOP. These four pillars are as follows:
Abstraction: If something is abstract, it means that it doesn’t have an instance in reality but does exist as an idea or concept. In programming, we use this technique to organize our thoughts. This is one of the pillars of OOP. In C#, we have abstract classes, which implement the concept of abstraction. Abstract classes are classes that don’t have any instances, classes that implement the abstract class will implement the properties and methods of that abstract class.
Encapsulation: Encapsulation means hiding or covering. In C#, encapsulation is achieved by access modifiers. The access modifiers that are available in C# are the following:
- Internal protected
Encapsulation is when you want to control other classes’ access to a certain class. For security reasons, it isn’t a good idea to make that class accessible to all classes. You can also limit access to the properties and variables of a class. Like variables and properties, you can also use access specifiers for methods. Encapsulation is a very important part of OOP as it gives us control over code
Inheritance: The word inheritance means receiving or deriving something from something else. In real life, we might talk about a child inheriting a house from his or her parents. In that case, the child has the same power over the house that his parents had. This concept of inheritance is one of the pillars of OOP. In programming, when one class is derived from another class, this is called inheritance. This means that the derived class will have the same properties as the parent class. In programming terminology, the class from which another class is derived is called the parent class, while the classes that inherit from these are called child classes.
Polymorphism: The word polymorph means many forms. To understand the concept of polymorphism properly, let’s work with an example. Let’s think about a person, such as Bill Gates. We all know that Bill Gates is a great software developer, business man, philanthropist, and also a great human being. He is one individual, but he has different roles and performs different tasks. This is polymorphism. When Bill Gates was developing software, he was playing the role of a software developer. He was thinking about the code he was writing. Later, when he became the CEO of Microsoft, he started managing people and thinking about growing the business. He’s the same person, but with different roles and different responsibilities.
In C#, there are two kind of polymorphism: static polymorphism and dynamic polymorphism. Static polymorphism is a kind of polymorphism where the role of a method is determined at compilation time, whereas, in dynamic polymorphism, the role of a method is determined at runtime. Examples of static polymorphism include method overloading and operator overloading. Writing a method with the same name as another method, but with different parameters, is called method overloading. This is a kind of polymorphism. Like method overloading, operator overloading is also a static polymorphism.
Dynamic polymorphism refers to the use of the abstract class. When you write an abstract class, no instance can be created from that abstract class. When any other class uses or implements that abstract class, the class also has to implement the abstract methods of that abstract class. As different classes can implement the abstract class and can have different implementations of abstract methods, polymorphic behavior is achieved. In this case, we have methods with the same name but different implementations.
In the next code, show an example of abstraction, encapsulation, inheritance and polymorphism.