---
title: "what is OOPs in C#"  
description: "describe concept of object oriented programming in C# and explain each concept we the help of program ."  
author: "Ravi Vishwakarma"  
published: 2021-07-07  
canonical: https://www.mindstick.com/articles/327250/what-is-oops-in-c-sharp  
category: ".net"  
tags: ["c#", ".net", "java", "c++"]  
reading_time: 11 minutes  

---

# what is OOPs in C#

## Oops in C#

Oops (Object [Oriented Programming](https://www.mindstick.com/interview/23571/what-is-the-difference-between-object-oriented-programming-and-object-based-programming)) in a programming model where program are write on object and data. The OOPs programming is depend on [class and object](https://answers.mindstick.com/qa/82400/what-is-difference-between-class-and-object-in-c-sharp). Class is a core of OOPs programming. In OOPs mandatory to create the class for representing the data and for accessing create the object. A class a blue print of object that contain the variable for storing the data and function for performing operation on data.

## OOPs properties

1. Object

2. Class

3. Encapsulation

4. Inheritance

5. Abstraction

6. Polymorphism

## Object

An object is instance of class. An object in oops nothing but everything. Because this object access the all class member [variable and function](https://www.mindstick.com/forum/157170/what-is-the-naming-convention-in-python-for-variable-and-function). This is basically use for accessing the class data. In C# programming use “new” keyword for creating the object of class. For example – color, car, bag, palce_name, college and etc.

Syntax –

ClassName onject_name=new ClassName();

## Class

A Class in object oriented programming is a blueprint or prototype that defines the variables and the methods (functions) common to all C# Objects of a certain kind. A class is not occupy any memory space but we create the object using “new” operator then this will contain the space in heap memory and this object is called instance of class. And starting address on object is store in stack memory. If don’t use “new” operator for creating the object that’s not contain address in heap memory and object don’t store in stack in other word object contain “null” value. If object contain the null value then object not access the class member.

Syntax –

class class_name{

// member variable

[modifier] datatype variable-name;

// member function

[modifier] datatype function-name()

{

//statement

}

}

Example of class and object.

using System;

namespace ConsoleApplication1

{

class ClassDemo

{

// member [variable declaration](https://www.mindstick.com/forum/161394/what-is-the-difference-between-var-and-for-variable-declaration)

private String name;

// constructor

public ClassDemo()

{

this.name = 'name';

}

// member function declaration

public void setName(String name)

{

this.name = name;

}

static void Main()

{

// creating an object of class

ClassDemo demo = new ClassDemo();

// access the member function

demo.setName('mind stick');

}

}

}

## Encapsulation in C#

Wrapping data and method in single unit is called encapsulation. Encapsulation is like a capsule that is enclosing the related operation and data to an object. It use to hiding object [related information](https://answers.mindstick.com/qa/93486/do-we-get-any-notification-related-information-from-mindstick). This technique used to protect the information in an object from another object. It use hide the data for security such as making variable as private.

Example –

In mobile manufacturing company made the mobile using hardware and software then you sale the mobile to user, user doesn’t need to know how it work internally.

## Inheritance

A class provide his property to another property this is called inheritance. Inheritance is process of reusability of object in OOPs. Another way to achieve abstraction in C#, is with interfaces. An interface is a completely abstract class, which can only contain abstract methods and properties (with empty bodies). Acquiring the properties of one class in another class is called the inheritance. Inheritance provide reusability of code allowing extend an existing class and reduce the complexity of code. By default member of an interface are abstract and public. Interface can contain methods and properties but not fields. Interface must be inherited by another class to [implement an interface](https://www.mindstick.com/forum/161272/how-do-you-implement-an-interface-in-c-sharp) using colon (:) symbol. After implementing the inheritance method must be override. At the time of declaration of methods in interface, doesn’t contain the method body. The interface is created with interface keyword.

NOTE. Doesn’t use the override keyword at time of override the method

Syntax of inheritance.

Interface interface-name {

// declare methods

[access specifier] [abstract] return-type function-name( [arguments] ) ;

[access specifier] [abstract] return-type function-name( [arguments] ) ;

// declare properties

}

Syntax for implementing the interface

Class class-name : interface-name{

}

This colon say that implement an interface in class. If implement the interface then override the interface methods.

Example of declaration of interface

// interface declaration

Interface Vehicle {

// this is a methods in inheritance

public void applyBreaks();

}

## There are many type of interface

1. Single interface

2. Multiple interface

## Single interface

using System;

class HelloWorld {

static void Main() {

Bike bike=new Bike();

bike.printRecord();

bike.changeGear(2);

bike.printRecord();

bike.speedUp(10);

bike.printRecord();

bike.applyBreak();

bike.printRecord();

Vahicle bike=new Bike();

bike.printRecord();

bike.changeGear(2);

bike.printRecord();

bike.speedUp(10);

bike.printRecord();

bike.applyBreak();

bike.printRecord();

}

}

// declaring interface

interface Vahicle{

// declaring the methods in interface

void changeGear(int gear);

void applyBreak();

void speedUp(int up);

void printRecord();

}

// implementing the vehicle interface

class Bike : Vahicle{

private int gear;

private int up;

public Bike(){

}

public void changeGear(int gear){

this.gear=gear;

}

public void applyBreak(){

up--;

}

public void speedUp(int up){

this.up=up;

}

public void printRecord(){

Console.WriteLine('Gear '+this.gear+' Speed '+this.up);

}

}

Output :

Gear 0 Speed 0

Gear 2 Speed 0

Gear 2 Speed 10

Gear 2 Speed 9

Gear 0 Speed 0

Gear 2 Speed 0

Gear 2 Speed 10

Gear 2 Speed 9

## [Multiple inheritance](https://www.mindstick.com/forum/2519/java-multiple-inheritance)

using System;

class HelloWorld {

static void Main() {

Bike bike=new Bike();

bike.paintVahicle('\'red\'');

bike.printRecord();

bike.changeGear(2);

bike.printRecord();

bike.speedUp(10);

bike.printRecord();

bike.applyBreak();

bike.printRecord();

Car car=new Car() ;

car.paintVahicle('\'pink\'');

car.printRecord();

car.changeGear(5);

car.printRecord();

car.speedUp(40);

car.printRecord();

car.applyBreak();

car.printRecord();

}

}

// declaring vahicle interface

interface Vahicle{

// declaring the methods in interface

void changeGear(int gear);

void applyBreak();

void speedUp(int up);

void printRecord();

}

// declaring penter interface

interface Penter {

// declaring the new methods in penter interface

void paintVahicle(String color);

}

class Bike : Vahicle , Penter {

private int gear;

private int up;

private String paintColor ;

public Bike(){

}

public void changeGear(int gear){

this.gear=gear;

}

public void applyBreak(){

up--;

}

public void speedUp(int up){

this.up=up;

}

public void paintVahicle (String color)

{

this.paintColor=color;

}

public void printRecord(){

Console.WriteLine('Your bike color is '+this.paintColor+' Gear '+this.gear+' Speed '+this.up);

}

}

class Car : Vahicle , Penter {

private int gear;

private int up;

private String paintColor ;

public Car(){

}

public void changeGear(int gear){

this.gear=gear;

}

public void applyBreak(){

up=up-10;

}

public void speedUp(int up){

this.up=up;

}

public void paintVahicle (String color)

{

this.paintColor=color;

}

public void printRecord(){

Console.WriteLine('Your car color is '+this.paintColor+' Gear '+this.gear+' Speed '+this.up);

}

}

## Abstraction

Abstraction is a process of hiding the implementation details and showing the user related data. In other word hiding the critical data to use and showing only user related data, for example we are sending an email then type massage and send it doesn’t see how it is work. We do not know internal processing. This technique achieve by abstract class. If we want to make a class is abstract then using abstract keyword before class declaration. In this class have two type of methods

1. Abstract method
2. Non-abstract method

• An abstract class must declare with abstract keyword

• It can have abstract and non-abstract method

• Doesn’t create object of this class

• This class can extended

• Every abstract method must override is derived class

• It have final method

Syntax of declaring an abstract class

abstract class class-name{

member-variable 1 -----

member-variable 2 -----

member-variable n -----

return-type fuction_name()

{

}

// abstract method

abstract return-type function_name(); //

}

Example of abstract class

abstract class Person{

//variables of person class

private String personName;

private String personeAddress;

private int personeAge;

// creating constructor

Person(){

// [default constructor](https://www.mindstick.com/forum/34531/default-constructor)

}

Person(String personName,String personeAddress,int personeAge)

{

// parameterized constructor

this.personName=personName;

this.personeAddress=personeAddress;

this.personeAge=personeAge;

}

// methods of person class

public void setPersoneName(String personName)

{

this.personName=personName;

}

public String getPersonName()

{

return this.personName;

}

public void setPersoneAddress(String personeAddress)

{

this.personeAddress=personeAddress;

}

public String getPersonAddress()

{

return this.personeAddress;

}

public void setPersoneAge(int personeAge)

{

this.personeAge=personeAge;

}

public int getPersonAge()

{

return this.personeAge;

}

@Override

public String toString()

{

return this.personName+' is belongs to '+personeAddress+' and age is '+personeAge+' year';

}

// declare the abstract methods for datahiding

abstract public String getGoingTime();

abstract public String getReadingTime();

}

//declare a student class an dextende the persone class

class Student extends Person{

private String goingTime;

private String readingTime;

// parameterized constructor

Student(String studentName,String studentAddress, int age){

super(studentName,studentAddress,age);

}

Student(String studentName,String studentAddress, int age, String goingTime,String readingTime){

super(studentName,studentAddress,age);

this.goingTime=goingTime;

this.readingTime=readingTime;

}

@Override

public String getGoingTime()

{

if(goingTime==null)

return 'This student does't study.';

return 'This student going daily for study at '+goingTime+' AM';

}

@Override

public String getReadingTime()

{

if(readingTime==null)

return 'This student does't reading books ';

return 'This student daily reading for '+readingTime+' Hours';

}

}

//declare a NormalMan class an dextende the persone class

class NormalMan extends Person{

private String goingTime;

private String readingTime;

// parameterized constructor

NormalMan(String studentName,String studentAddress, int age){

super(studentName,studentAddress,age);

}

NormalMan(String studentName,String studentAddress, int age, String goingTime,String readingTime){

super(studentName,studentAddress,age);

this.goingTime=goingTime;

this.readingTime=readingTime;

}

@Override

public String getGoingTime()

{

if(goingTime==null)

return 'This person does't go office.';

return 'This person daily go office at '+goingTime+' AM';

}

@Override

public String getReadingTime()

{

if(readingTime==null)

return 'This perone is not reading a books. ';

return 'This person reading books for '+readingTime+' Hours ';

}

}

public class Main

{

public static void main (String[]args)

{

Person student=new Student('Ravi vishwakarma','Babhaniyav',22);

System.out.println(student);

System.out.println(student.getReadingTime());

System.out.println(student.getGoingTime()+'\n');

Person student1=new Student('Ravi sankar','Agra',18,'8:00','4');

System.out.println(student1);

System.out.println(student1.getReadingTime());

System.out.println(student1.getGoingTime()+'\n');

Person man=new NormalMan('Ravi vishwakarma','Babhaniyav',22);

System.out.println(man);

System.out.println(man.getReadingTime());

System.out.println(man.getGoingTime()+'\n');

Person man1=new NormalMan('Ravi sankar','Agra',18,'8:00','4');

System.out.println(man1);

System.out.println(man1.getReadingTime());

System.out.println(man1.getGoingTime()+'\n');

}

}

## Polymorphism

Polymorphism mean one name many forms. Means in a class have a method but behaves in different forms. There are two type of [polymorphism in C#](https://answers.mindstick.com/qa/82404/what-is-polymorphism-in-c-sharp).

1. Static or compile time

• Method overloading

• Operator overloading

2. Dynamic or run time

• Virtual / overriding method

Example –

A person behave the student in college but in company behave the employ.

## Static or compile time

It is also known as early binding. Method overloading is an example of static polymorphism. In method overloading, the method or function name are same but the signature (argument / parameter) is different because this method made at compile time. In C# after methods calling, compiler check no of parameter is match from the related method or not, if match then working, if no matching show an error.

Example of method overloading –

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Data.SqlClient;

namespace ConsoleApplication1 {

public class Program {

static void Main(string[] args)

{

FileDemo fd = new FileDemo();

Console.WriteLine('Tree arguments : '+fd.add(25, 12, 45));

Console.WriteLine('Two arguments : '+fd.add(25, 12));

Console.ReadKey();

}

}

//craeting the file demo class in namepace

class FileDemo

{

public int add(int a, int b, int c)

{

return a + b + c;

}

public int add(int a, int b)

{

return a + b;

}

}

}

Output

\

## Dynamic or run time

Dynamic / runtime polymorphism is also known as late binding. Here, the method name and the method signature (number of parameters and parameter type must be the same and may have a different implementation). Method overriding is an example of dynamic polymorphism. Method overriding can be done using inheritance. With method overriding it is possible for the base [class and derived](https://answers.mindstick.com/qa/30686/what-is-a-base-class-and-derived-class) class to have the same method name and same something. The compiler would not be aware of the method available for overriding the functionality, so the compiler does not throw an error at compile time. The compiler will decide which method to call at runtime and if no method is found then it throws an error.

Example –

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Data.SqlClient;

namespace ConsoleApplication1 {

public class Drawing

{

public virtual double Area()

{

return 0;

}

}

public class Circle : Drawing

{

public double Radius;

public Circle()

{

Radius = 5;

}

public override double Area()

{

return (3.14) * Math.Pow(Radius, 2);

}

}

public class Square : Drawing

{

public double Length;

public Square()

{

Length = 6;

}

public override double Area()

{

return Math.Pow(Length, 2);

}

}

public class Rectangle : Drawing

{

public double Height;

public double Width;

public Rectangle()

{

Height = 5.3;

Width = 3.4;

}

public override double Area()

{

return Height * Width;

}

}

class Program

{

static void Main(string[] args)

{

Drawing circle = new Circle();

Console.WriteLine('Circle Area :' + circle.Area());

Drawing square = new Square();

Console.WriteLine('Square Area :' + square.Area());

Drawing rectangle = new Rectangle();

Console.WriteLine('Rectangle Area :' + rectangle.Area());

Console.ReadKey();

}

}

}

Output –

\

---

Original Source: https://www.mindstick.com/articles/327250/what-is-oops-in-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
