WebTrainingRoom Online Courses
Learn C# programming Language, Free C# Tutorial for Beginners, C# Development Guide

OOPs in C# Example

Object Oriented Programming in C#.Net

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

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

Abstraction in C# .net 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 abstract class code sample 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();
    }
}

Encapsulation in C# .Net 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

Inheritance in .Net C# 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 in C#.Net 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

Comment
Name Email Website
Subscribe
 
OOPs in C#
Learn C# Online Tutorial



Learn C# .Net Programming Online
Consulting