What is the Oops Concept? | Oops Concepts With C# – Devstringx

7 min read

What Is C#?

C# pronounce as “C Sharp”. It is a programming language that is close to other popular languages like C, C++, and Java. C# supports object-oriented programming and it was created by Microsoft in 2002 that runs on the .NET Framework. C# is used for Mobile Applications, Web Applications, Desktop Applications, Games, etc.

What is the OOPs Concept?

Object Oriented Programming (OOP) is a programming technique that totally depends on the concept of classes and objects. It uses to design a software program into simple, easy, and reusable pieces of code blueprints (usually known as classes), which are used to create individual instances of objects. There are many object-oriented programming languages including JavaScript, Java, C#, C++, Python, etc.

1) What Is a Class?

A class in OOPs is a prototype or a blueprint of data members and functions that are used to create objects. Class acts as a reference type for objects so it doesn’t occupy any space in memory until there is an object created from that class.

2) What Is an Object?

The object is an instance of a class. An object is generally used to access various data members, properties, and methods of a class. We cannot create an object until and unless a class is instantiated. When an object has been created the space for that object is allocated in heap memory and the reference address of that object is stored in stack memory.

3) Example to Illustrate Classes and Objects

So let us discuss classes and objects with the help of an example. Suppose we create a class “Dog” and then we create an object “bulldog” of class Dog. In class “Dog” we have created two data members named “breed” and “color” and a method named “bark”. Let’s see the implementation in C# programming.

using System;
 
namespace ClassObject{
 
//creating class Dog
 
class Dog{
 
string breed;
 
string color;
 
public void bark(){
 
Console.WriteLine("Bark Bark!!!");
 
}
 
public static void Main(string[] args){
 
//creating object of Dog class
 
Dog bullDog= new Dog();
 
//Accessing breed and color of Dog
 
bullDog.breed= "Bull Dog";
 
bullDog.color= "Fawn";
 
Console.WriteLine(bullDog.breed);
 
Console.WriteLine(bullDog.color);
 
//access method of the Dog
 
bullDog.bark();
 
Console.ReadLine();
 
}
 
}
 
}

After execution of the above program the output on the console screen is:

  • Bull Dog
  • Fawn
  • Bark Bark!!!
Four Pillars of Object-Oriented Programming

There are four pillars or principles of Object-oriented programming that helps in writing simple, clean, and reusable code:

  • Encapsulation
  • Inheritance
  • Abstraction
  • Polymorphism

1. Encapsulation

Encapsulation is a path to hide the properties and characteristics of an object to prevent the data from unwanted access by binding the code and data in a single unit called an object. We can achieve data hiding through private access modifiers. The access modifiers are keywords that use to set the access level/visibility for classes, fields, methods, and properties. Let us discuss access modifiers available in C#:

  • Public: code that is accessible for all classes.
  • Private: code that is accessible within the same class
  • Protect: code that is accessible within the same class and the class that is inherited from that class
  • Internal: code that is accessible only within its own assembly

Let Us Consider The Programming Example:

using System;
 
public class Dog{
 
//private variable declared can be accessed by public methods of class private String breed;
 
//public variables can be accessed in any class without the need for accessors public String color;
//using accessors to get and set the value of private variable breed public String Breed{
get{ return breed;}
 
set{ breed= value;}
 
}
 
}
 
//Driver Class
 
class DriverClass{
 
//main Method
 
static public void Main()
 
{
 
Dog obj= new Dog();// creating object
 
//calls set accessor of the property Breed and pass ""Bulldog" as the value of the standard field "value"
obj.Breed="BullDog";
 
obj.color="fawn";
 
//Displaying values of the variables
 
Console.WriteLine("Breed: "+ obj.Breed);
 
Console.WriteLine("Color: "+ obj.color);
 
}
 
}

The output after execution of the above program is:

  • Breed: BullDog
  • Color: fawn

2) Inheritance

Inheritance is one of the vital features of OOPs. It allows us to create a new class from the existing classes and is hence helpful in the reusability of code. In simple words, Inheritance creates a parent-child relationship among various classes.

C# Parent Class: In Inheritance, the class whose features inherit by another class is known as the parent class (or a superclass, or base class).

C# Child Class: The class that inherits the existing class is known as the child class (or derived class, extended class, or subclass).

Let’s See A Programming Example:

using System;
 
namespace Inheritance {
 
// base class
 
class Animal {
 
public string name;
 
public void show() {
 
Console.WriteLine("This is an animal");
 
}
 
}
 
// derived class of base class "Animal"
 
class Dog: Animal {
 
public void getName() {
 
Console.WriteLine("Name is " + name);
 
}
 
}
 
class DriverClass {
 
static void Main(string[] args) {
 
Dog bullDog = new Dog(); //creating object of derived class
 
// access field and method of base class bullDog.name = "Tommy"; bullDog.show();
// access method from own class bullDog.getName(); Console.ReadLine();
}
 
}
 
}

The output after execution of the above program is:

  • This is an animal
  • Name is Tommy

There are various examples of Inheritance:-

  • Single Inheritance
  • Multiple Inheritance
  • Multilevel Inheritance
  • Hierarchical Inheritance
  • Hybrid Inheritance

3) Abstraction

Abstraction refers to the process of visualizing only the relevant and essential data to the user without showing irrelevant information. Abstraction in C# can achieve by creating abstract classes. Abstract classes create by using abstract keywords. Abstract classes are generally used at the time of inheritance. Let’s have an example:

using System;
 
namespace Abstraction
 
{
 
abstract class Animal // Abstract class
 
{
 
public abstract void sound(); // Abstract method (does not have a body)
 
public void sleep()
 
{
 
Console.WriteLine("sleeping: Zzz");
 
}
 
}
 
class Dog : Animal // Derived class (inherit from Animal)
 
{
 
public override void sound()
 
{
 
// The body of sound() is provided here Console.WriteLine("The dog Bark");
 
}
 
}
 
class DriverClass
 
{
 
static void Main(string[] args)
 
{
 
Dog bullDog = new Dog(); // Create a Pig object
 
bullDog.sound();
 
bullDog.sleep();
 
}
 
}
 
}

The output of the above code is:

  • The dog Bark
  • sleeping: Zzz
4) Polymorphism

Polymorphism defines a process where a method behaves differently under different conditions by taking different types of parameters. Let us see an example:

using System;
 
namespace Polymorphism
 
{
 
public class Calculator
 
{
 
// Overloaded method: Method those have the same name but different signatures public int Add(int a, int b)
 
{
 
return a + b;
 
}
 
public int Add(int a, int b, int c)
 
{
 
return a + b + c;
 
}
 
}
 
class Program
 
{
 
static void Main(string[] args)
 
{
 
Calculator calculator = new Calculator();
 
// Here the compiler will understand automatically which methods need to be called at the compile-time based on the signature of the method.
Console.WriteLine(calculator.Add(10, 20)); Console.WriteLine(calculator.Add(10, 20,30));
}
 
}
 
}

The output of the above code is:

  • 30
  • 60

In the above example, there are two methods of the same name i.e. Add. But both have different numbers of parameters that changed their behavior too. Add method having two parameters will work only for the addition of two numbers and Add method having three parameters will work for adding three numbers. So whenever the user wants to perform addition, the compiler will automatically call the appropriate Add method according to the number of parameters entered. There are two types of polymorphism:

  1. Compile-time polymorphism (also known as Static polymorphism, Overloading, or Early binding)
  2. Runtime polymorphism (also known as Dynamic polymorphism, Overriding or late binding)

Original Source: Click here

In case you have found a mistake in the text, please send a message to the author by selecting the mistake and pressing Ctrl-Enter.
Comments (0)

    No comments yet

You must be logged in to comment.

Sign In