Introduction
In this article, we will learn about the pillars of OOPS. This is a common interview question that will be applicable to all the developers. I have explained in detail along with the example so that you can understand properly.
There are 4 modules which are part of the pillar of OOPS.
- Encapsulation
- Abstraction
- Polymorphism
- Inheritance
Encapsulation & Abstraction
- Encapsulation is the way to hide data, properties, and methods from outside of the world or outside of the class scope and expose only necessary things.
- Encapsulation hides unwanted data or private data from outside of a class.
- We can achieve encapsulation by using a private access specifier.
- Abstraction displays only important features of a class without going to the background details.
- We can achieve abstraction by using a public access specifier.
Real-time Example
Let's take an example of a television(TV).
Source Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
using System.Data.SqlClient;
namespace CsharpConcept
{
public class Program
{
static void Main(string[] args)
{
Television objtelevision = new Television();
objtelevision.TVKeys();
objtelevision.TVRemote();
Console.ReadLine();
}
}
public class Television
{
private void TVColorTube()
{
Console.WriteLine("Color Tube of Television class");
}
private void TVMotherBoard()
{
Console.WriteLine("Mother Board of Television class");
}
public void TVKeys()
{
Console.WriteLine("TVKeys of Television class");
}
public void TVRemote()
{
Console.WriteLine("Remote of Television class");
}
}
}
Output
Explanation
In the above example, we have taken the example of television which has been in day-to-day life. If you observe, we have created a Television class with 4 methods; out of 4 methods 2 methods with public access specifier & 2 methods with Private access specifier.
Now we will understand how the class has been co-related with real-time scenarios. In a day to day life, we are using the television. For a normal user, the basic knowledge we should have regarding the television is how to turn on the TV, use of Remote volume key to increase/decrease, uses of change the channel. So in the code, we have created the TVKeys() & TVRemote() methods with public access specifier so that both methods can be accessed anywhere, but for the other 2 methods i.e, TVColorTube() & TVMotherBoard(), we have mentioned them as a private access specifier so the scope of the method is within the class itself. We can’t access this method outside of the class. So a normal user who uses television does not necessarily know how the color tube & motherboard work. So, in this case, both the modules will be hidden from the user. So we can consider this module as an encapsulation & the other 2 methods, i.e, TVKeys() & TVRemote(), will be considered as an abstraction.
Polymorphism
Polymorphism means the ability to take more than one form. C# allows multiple methods to have the same name but different parameters.
There are two types of polymorphism.
- Method Overloading
- Method Overriding
Method Overloading
- Multiple declarations of a method in a class are known as Method Overloading.
- Each declaration differs from the previous declaration either in the number of arguments & type of the arguments.
Source Code: Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
using System.Data.SqlClient;
namespace CsharpConcept
{
public class Program
{
static void Main(string[] args)
{
Console.WriteLine("Method Overloading Demonstration");
Console.WriteLine("********************************");
MethodOverLoading objmethodoverload = new MethodOverLoading();
objmethodoverload.MyMethod();
objmethodoverload.MyMethod(10);
objmethodoverload.MyMethod(10, 20);
Console.ReadLine();
}
}
public class MethodOverLoading
{
public void MyMethod()
{
Console.WriteLine("Hello");
}
public void MyMethod(int a)
{
Console.WriteLine("Hi");
}
public void MyMethod(int a, int b)
{
Console.WriteLine("How are you!");
}
}
}
Output
Explanation
In the above code, we have created a class MethodoverLoading which will contain 3 methods with the same name, i.e MyMethod() with different parameters. Method Overloading allows us to create the same method with different parameters. 1st method with out any parameter, 2nd method with one integer parameter & the 3rd method with 2 integer parameter. So while calling the method MyMethod() with the help of the object of the class, i.e, objmethodoverload, it will suggest 3 overloaded versions with the help of Visual Studio intelligence. Based on the input parameter, the respective Mymethod() will be called & we will see the o/p, which has been mentioned inside the method body.
Note
- It is not possible to overload a method based on the different return types.
Method Overriding
- Changing the definition of the method of the base class in the derived class without changing the signature of the method is known as Method Overriding.
- To achieve method overriding, declare the method in the base class as virtual & declare the derive class method as override.
- We can achieve method overriding with the help of inheritance.
- Virtual and Override keywords are used to achieve method overriding.
Source Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
using System.Data.SqlClient;
namespace CsharpConcept
{
public class Program
{
static void Main(string[] args)
{
DeriveClass objderive = new DeriveClass();
objderive.MyMethod();
Console.ReadLine();
}
}
// Base Class
public class BaseClass
{
public virtual void MyMethod()
{
Console.WriteLine("Hello, I am from Base Class");
}
}
// Derived Class
public class DeriveClass : BaseClass
{
public override void MyMethod()
{
Console.WriteLine("Hello, I am from Derived Class");
}
}
}
Output
Explanation
In the above code, we have created a parent class called Baseclass which will be inherited by the child class DeriveClass. In this case, if you observe in the base class method MyMethod(), we have used the Virtual keyword & the derived class, which will be inheriting the base class, will override the base class method implementation with the help of the override keyword. So method overriding allows us to override the base class method implementation in the derive class.
So it is clear that we can modify the base class method implementation in the derive class.
Inheritance
- In C#, the class that inherits the members of another class is known as the derived class, and the class whose members are inherited is known as a base class.
- With the help of inheritance, we can achieve a parent-child relationship between the classes. The derived class inherits the fields and methods of the base class. This helps with the code reusability in C#.
Base/Parent Class: Child/Derive Class
Syntax
<acess-specifier> class <base_class> {
// Define members or methods here, if needed
}
class <derived_class> : <base_class> {
// Define members or methods here, if needed
}
Example
public class A
{
// Define members or methods here, if needed
}
public class B : A
{
// Consuming the member of base class A
}
Types of inheritance
In C#, there are 4 types of inheritance
- Single inheritance: A derived class that inherits from only one base class.
- Multi-level inheritance: A derived class that inherits from a base class, and the derived class itself becomes the base class for another derived class.
- Hierarchical inheritance: A base class that serves as a parent class for two or more derived classes.
- Multiple inheritance: A derived class that inherits from two or more base classes.
Note. C# will support multiple interface inheritance, but C# willn't support multiple class inheritance.
In this article, we have learned what are the pillars of OOPS with examples. Happy Learning.