OOPs Concepts C# — Object Oriented Programming

The better way to explain Object Oriented Programming is to think of the OOPs in real-time data objects. It is a way of structuring and designing code based on the idea of real-world objects and their interactions. In OOP, data and the methods that operate on that data are encapsulated together into objects.

Main Pillers of Object-Oriented Programming :

Abstraction: Show only the necessary things.

Polymorphism: More than one form: An object can behave differently at different levels.

Inheritance: Parent and Child Class relationships.

Encapsulation: Hides the Complexity.

Classes and Objects: A class is a type, or blueprint which contains the data members and methods , An Object is an instance.

Now let us understand each concept clearly with the help of code written in a programming language called c# .

we will consider the example of a Bank Management System.

Abstraction: using the abstract and interface features in c# , the abstraction is defined.

using abstract in c#

public class Customer
{
public string Name {get;set;}
public string CustomerId {get; set;}
public string Address {get; set;}

public void TotalAmount()
{
// this method calculate the total amount present in the customer account
}

}

In the above code , the TotalAmount() will come from different sources because customer can have different types of accounts like Savings, Current , Cheque accounts. so we need to show only the necessary things here . Below is the c# code for abstract class .

public abstract class Customer
{
public string Name {get; set;}
public string CustomerId {get; set;}
public string Address {get; set;}
public Decimal TotalAmount {get; set;}
public abstract void TotalAmount();

}

public class SavingsAccount : Customer
{
public override void TotalAmount()
{
//logic to calculate the amount here
}
}

public class CurrentAccount : Customer
{
public override void TotalAmount()
{
//logic to calculate the amount here
}
}

The Customer class is declared as abstract and only the method signature is applied without its implementation , the method implementation part is done in the class where the abstract class is derived , SavingsAccount and CurrentAccount classes inherited from the abstract and its methods are marked with override because by default the members inside the abstract class are virtual which are overridden by the child class members.

We can call the method according to our requirement , so here in the final Customer class we are showing only the required properties which are to be displayed to the end user , the calculation part he doesn't need what else things are applied in the backend business logic .

public static void Main()
{
Customer c1 \= new SavingsAccount();
c1.Calculate();

Customer c2 \= new CurrentAccount();
c2.Calculate();
}

The Abstract classes is used when we have a situation of inheritance between base and implementation classes , if common classes share the same behavior then we will go for the Interface concept where it is also a type of abstraction .

using Interface in c#:

Consider the same example — suppose we need to display the Customer branch information, in order to get the branch information in the backend some business logic is implemented to get the branch details.

Here also the un wanted details are hidden from the end user only what is required we are shown.

when to use the interface? interfaces are used when any common behavior is used across the other places in the code then it is used and when base and child class relation is present then we can use the abstract classes. It completely depends upon the project's requirement.

Like when a Library was developed the abstract is useful because it will be consumed or implemented in other parts of the code, interface is like a plug-and-play for segment-level tasks.

Below is the modified code where interface and abstract are implemented .

using System;
namespace C_sharpbasics
{
public abstract class Customer
{
public Customer()
{
}
public string Name { get; set; }
public string Address { get; set; }
public decimal TotalAmt { get; set; }
public abstract int CalTotAmount();
}

public class SavingsAccount : Customer, IBankBranch
{
public override int CalTotAmount()
{
//login to display amount
return 2000;
}
public string GetBranchAddress()
{
return "India";
}
}

public class CurrentAccount : Customer ,IBankBranch
{
public override int CalTotAmount()
{
//login to display amount
return 6000;
}
public string GetBranchAddress()
{
return "Japan";
}
}

}

Encapsulation :

Wrapping up data into a single unit. Encapsulation hides the complexity of the way the object was implemented . Encapsulation in object-oriented programming (OOP) is a mechanism of hiding the internal details (implementation) of an object from other objects and the outside world. It is one of the four fundamental principles of OOP, along with inheritance, polymorphism, and abstraction. Encapsulation allows an object to control access to its data and methods, which can improve the security and stability of the system. In OOP, encapsulation is typically achieved through the use of access modifiers, such as “private” and “protected,” which restrict access to certain members of a class.

mechanism to implement the abstraction is encapsulation

Inheritance (Derived and Base class)

Inheritance allows us to define a class in terms of another class, which makes it easier to create and maintain an application. This also provides an opportunity to reuse the code functionality and speeds up implementation time.

When creating a class, instead of writing completely new data members and member functions, the programmer can designate that the new class should inherit the members of an existing class. This existing class is called the base class, and the new class is referred to as the derived class.

The idea of inheritance implements the IS-A relationship. For example, mammal IS A animal, dog IS-A mammal hence dog IS-A animal as well, and so on.

public class Parent
{
public string Method1()
{

}
}
public class Child :Parent
{
public string Method2()
{

}
}

Now the child class can access the parent class method and its own method .

Polymorphism

Polymorphism means assigning a single name but there can be multiple behaviours. It means the name of the function is the same but its definitions are different.

Polymorphism means single name & multiple meaning & whenever we try to access that particular name there must be a binding between that call & between that function. So there are two types of binding to call that function.

Method Overloading — Compile time

Method Overriding — Runtime

C# access modifiers

Did you find this article valuable?

Support Dileep Sreepathi by becoming a sponsor. Any amount is appreciated!