Skip to main content

4 Pillars of Object-Oriented Programming Concept (OOP) in C# with Code Examples

 

4 Pillars of Object-Oriented Programming Concept (OOP) in C# with Code Examples by Ziggy Rafiq

Overview

In this post, I am writing about one of the most important aspects of C# which is the 4 core pillars of the Object-Oriented Programming Concept in C#.  I will explain these four pillars and also provide further links to the related subject area.

The four pillars of Object-Oriented Programming (OOP) concepts in C# provide a structured and organized approach to software development. They are fundamental principles that help developers to create efficient, modular, and maintainable code. By following these principles, developers can build complex software systems that are easy to understand, extend, and modify.

Encapsulation Inheritance Polymorphism Abstraction by Ziggy Rafiq


Encapsulation

Encapsulation can be achieved by defining data members as private or protected and providing public methods to access and modify them. This way, the data is hidden from outside interference, and you can manage the complexity of large software systems.  As the code example below.

Encapsulation can be achieved by defining data members as private or protected and providing public methods to access by Ziggy Rafiq

Click here to read further about Encapsulation

Inheritance

Inheritance can be achieved by creating a new class by inheriting from an existing class using the colon operator. The derived class inherits all the properties and methods of the base class, which helps in reusing the existing code and reducing code redundancy.  As the code example below.

Inheritance can be achieved by creating a new class by inheriting from an existing class using the colon operator. by Ziggy Rafiq

Polymorphism

Polymorphism can be achieved by creating methods with the same name but different parameters in the base and derived classes. This way, you can use an object of a derived class as an object of the base class, which helps in creating flexible and extensible code as in the code example below.



Polymorphism can be achieved by creating methods with the same name but different parameters in the base and derived classes by Ziggy Rafiq

Click here to read further about Polymorphism 

Abstraction

Abstraction can be achieved by creating abstract classes and interfaces. An abstract class cannot be instantiated and provides a template for its derived classes. An interface provides a contract for implementing classes. This way, you can represent the essential features of a class without including the background details, which helps in reducing the complexity of the code and provide a simplified interface to the user. As the code example below.

Abstraction can be achieved by creating abstract classes and interfaces.  by Ziggy Rafiq

Summary

using the four pillars of OOP concepts in C# helps in creating high-quality software systems that are efficient, modular, and maintainable" is a valid and accurate statement. By following the four pillars of OOP concepts, developers can create code that is organized, easy to understand, and can be easily modified to adapt to changing requirements. This results in code that is efficient, modular, and easy to maintain, which in turn leads to the development of high-quality software systems.



Comments

Most Viewed Ziggy Rafiq Blog Posts

A Complete Guide to Using GUIDs in C# with Code Examples

  Overview In this post, we are looking at GUIDs (Globally Unique Identifiers), which are widely used in C# for generating unique identifiers for objects, entities, and resources in a system. In this post, we'll explore the basics of GUIDs in C#, their advantages, and how to convert strings to GUIDs. In this post, we have used Guid Generator to create the GUID from the following URL Address https://guidgenerator.com/ What is GUID GUID (Globally Unique Identifier) in C# is a 128-bit value that is used to identify objects, entities, or resources in a unique manner across different systems and applications. It is also known as UUID (Universally Unique Identifier) in some other programming languages.   GUIDs are generated using a combination of unique factors such as the MAC address of the network adapter, the current time and date, and a random number. The resulting GUID is a string of 32 hexadecimal digits separated by hyphens, such as "b86f2096-237a-4059-8329-1bbcea72769b...

Primitives Data Types and None-Primitives Data Types in C# with Code Examples

  Overview I wrote this post to provide an explanation of primitive and non-primitive data types in C#. C# is a strongly typed programming language, where each variable and expression must have a specific data type. C# data types are categorized into two primary groups: primitive data types and non-primitive data types. Primitive data types are the simplest data types available in programming languages. They are typically pre-defined data types and can represent a single value, such as a boolean value, character, or integer. Examples of primitive data types include int, char, float, double, and boolean, which are common in programming languages like C++, C, and Java. Non-primitive data types are also referred to as composite data types or reference data types. They are constructed from primitive data types and are more complex than primitive data types. Non-primitive data types can hold multiple values and allow for the creation of more intricate data structures like tables, li...

ASP.net Core Services Lifetime Differences Between Using AddTransient, AddScoped and AddSingleton Services

  ASP.net Core Services Lifetime Differences Between Using AddTransient, AddScoped and AddSingleton Services Overview In this blog post, I will be explaining to you the ASP.net core Service Lifetime and will be explaining to you what is the difference between them and which one can be a risk to a memory leak if used incorrectly. AddTransient, AddScoped, and AddSingleton are dependency injection services in ASP.NET Core. The differences between them are as follows below. AddTransient: A new instance of the service is created every time it is requested. AddScoped: A single instance of the service is created per request within the same scope. AddSingleton: A single instance of the service is created for the entire application lifetime. In general, it's recommended to use AddScoped for services that are stateful and AddTransient for services that are stateless or have a short lifetime. AddSingleton is usually used for services that are expensive to create or only need to be crea...