C# oops concepts with example

To become a successful software developer you must understand the object oriented concept, this will help you to understand what is an object? How to define properties with different access modifiers, how to create relations among objects and what are the best practices. In this tutorial you will learn object oriented programming concepts using C# with real-time application developing scenario, still the concept will remain the same.

Before we learn OOPS, we must be familiar with Class, Object, Property in C#, I am assuming you have already learned those basics.

Now we learn four pillars of oops, also known as 4 principles of object oriented programming, they are Abstraction, Encapsulation, Inheritance, Polymorphism

Object Oriented Programming in C#

Abstraction C# Example

Abstraction is one principle, It is used to display only necessary and essential features of an object to ouside the world.

Let's look at the c# oops abstract class example below

abstract class Car                
{
    public abstract string  BrandType { get; set; }
    public abstract string Mileage { get; set; }
    public virtual void TestDrive(){}
}

Now we look at implementation

class Maruti : Car
{
    public override string BrandType
    {          
        get;
        set;             
    }
    public override string Mileage
    {
        get;
        set;
    }
    // this is optional
    public override void TestDrive()
    {
        base.TestDrive();
    }
}
C# Encapsulation Example

Encapsulation means ability to hide data and behavior within object. Encapsulation enables a group of properties, methods and other members to be considered a single unit or object.

Now let's take a look at the example below

class CarBase                
{
    public CarBase()
    {
    }
                
    public CarBase(string carkey)
    {
    Key = carkey;
    Start();
    }
                
    public string Key { get; set; }
                
    public void Start()
    {
    StartEngine();
    }
                
    private void StartEngine()
    {
    }
}

No if we want to start engine from outside different class, we cannot access the "StartEngine()" directly , but can start the engine by calling "Start()";

encapsulation example

C# Inheritance example

Inheritance is the ability to inherit members from base classs, when any class inherits the members of the base class is called the derived class.

let’s look at following example, we are inheriting above "CarBase" class
class SUV : CarBase                
{
    public SUV()
    {
                
    }
}

inheritance example

Note: In C#.NET supports only single inheritance, but we can multiple inheritance using Interace like example below

class SUV : CarBase, ICar, IDesign
{
    public SUV()
    {
                
    }
}
Polymorphism C# Example

Polymorphism means multiple forms / shape, best way to express polymorphism is via an abstract base class (or interface).

Function Overloading is an example of static Polymorphism

Function Overriding is an example of dynamic Polymorphism
Let's see how we can implement Polymorphism in C#

abstract   class CarBase                
{
    public void ModifyTVScreen()
    {
    }
                
    public  void ModifyTVScreen(string screenType)
    { 
    } 
                 
    public virtual void ModifyColor(string color)
    {
    }                 
}
Now we see how to override the above function.
class SUV : CarBase                
{
    public override void ModifyColor(string color)
    {
        base.ModifyColor(color);
    }
                
}

Look at function overloading below

function overloading example

Hope you have understood Oops Concept, You can write any number of methods in form of function overloading, In any development language Object Oriented Programming (OOPs) provides the ability to simulate real-time business problem more effectively.

You can also look at object oriented programming in other language like OOPs in PHP

 
OOPs Concept in C#
.Net C# Programming Tutorials
Online Tutorials
C# .net Interview Questions Answers

Learn C# object oriented programming
.Net C# Examples | Join .Net C# Course