What Is Object-oriented Programing in C#?

CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

By Laraib Ather.

Introduction

This article is about object-oriented programming; I will try my best to explain the correct concept easily. An object-oriented programming language is the basics of any C# program and also is the first step for a developer to move toward the programming world. It is necessary for any developer to understand the concept of object-oriented programming. The initiative of understanding the object oriented programming is given below:

Pillars of Object-oriented Programming

Nowdays, according to S.E.I, there are only five pillars in object-oriented programming. The latest pillar, “abstract,” was added in 2007. Pillars are as follows:

  • Class
  • Object
  • Inheritance
  • Polymorphism
  • Abstraction

Classes

Classes are the first pillar of an object-oriented programming language and are used to make an instance or object. A class is a template that is necessary to make instances or objects. If the class is not declared as static, a coder can use the class to create objects and methods. There are four types of classes, which are as described here:

  • Static Classes
  • Abstract Classes
  • Sealed Classes
  • Partial Classes

Given below is the procedure of how to make a simple class. This will help you better instead of reading written steps.

Steps to Follow

  1. Use right-click to see options.
  2. Click the add option.
  3. After that, click class option.
  4. Give a name to the class with a “.cs” extension.

OOP1
Figure 1: Choosing the “add” option

OOP2
Figure 2: Clicking the “class” option

OOP3
Figure 3: Viewing the code

Types of Classes

Static Class

Static classes are those classes that have no objects and work without an object. They also are known as a template of a class. We can call static methods directly instead of making objects. In static classes, we can make non-static methods and static methods both at a time as well as make a static method in a non-static class.

Example

static class Calculation
{
   // static methods
   public static long GetProduct(int x, int y)
   {
      return x * y;
   }

}

class Program
{
   static void Main(string[] args)
   {
      Calculation.GetProduct(12, 33);
      // static method call
   }
}

Abstract Class

Abstract classes are those classes that cannot make an object. An abstract class could have abstract and non-abstract methods at a time, but the abstract method only can be made in an abstract class. In an abstract class, we can define only the signature of the abstract method because it is not allowed to define the body of the abstract method in an abstract class. The only way to define the body of the abstract method is to define it in a class that inherits an abstract class.

We used to write an “abstract” keyword with abstract classes and also with the abstract methods that define its signature in abstract classes. Also, we used to write an “override” keyword with the abstract methods when we defined their body in another class thath inherits an abstract class. An abstract class defines abstraction.

Example

abstract class Human
{
   abstract public void FirstAbstractMethod();
   abstract public void SecondAbstractMethod();
   public void NonAbstractMethod()
   {
      Console.WriteLine("This is an non-abstract method
         in an abstract class");
   }
}

class Man: Human
{
   public override void FirstAbstractMethod()
   {
      Console.WriteLine("This is a first abstract method's
         body which is defined in a non-abstract class
         which inherits abstract class ");
   }
   public override void SecondAbstractMethod()
   {
      Console.WriteLine("This is a second abstract method's
         body which is defined in a non-abstract class
         which inherits abstract class ");
   }

}

class Program
{
   static void Main(string[] args)
   {

      Man ali = new Man();
      ali. FirstAbstractMethod();
      ali. SecondAbstractMethod();


   }
}

Sealed Class

Sealed classes are those classes that do not allow inheritance. If you want to stop inheritance on the class level, use the keyword “sealed” with a class.

Example

sealed class example
{
   public int SealedMethod()
   {
      int x = 2;
      return x;
   }

}

class Program
{
   static void Main(string[] args)
   {
      example Obj = new example();
      Obj.SealedMethod();

   }
}

Partial Class

Actually, partial classes are the pieces of the same classes that can be used by two or more developers separately and easily, but for the compiler it would be remain one class. We use a “partial” keyword with all pieces of that class.

Example

partial class Employee
{
   public void methodA()
   { }
   public void methodB()
   { }
}



class Finance
{
}

partial class Employee
{
   public void methodX()
   { }
   public void methodY()
   { }
}

Objects

Objects are the second pillar of an object-oriented programming language. Objects always have actions (method) and properties.

We can understand it by a real world example. Suppose we have a pet, a cat of 3kg weight, blue coloured eyes, and one year of age. So, in this example, cat is an object and 3kg weight, blue coloured eyes, and one year age are its properties; the cat’s meow, jump, sight, and listening power are its actions. Keep in mind that an object has only one class but a class can contain multiple objects at a time.

The format of object is given below:

className objectName = new classConstructor();

Now, I am using the previous example of a class and making its object.

ClassName obj = new ClassName();

Methods

Objects are used to call methods. Methods are actions of the class. Methods are used to give input, process, and output. All methods must be public and local variables accessible only for the method(s) it contains.

There are four types of methods.

A method that takes input from the user and returns his/her output. This means it get input and give output and it also return a “Return type.”

Example

// using class that we made above

class ClassName
{
   // making method

   public int addition(int x, int y)

   {
      return x + y;
   }
}

The second method is that which only take input, but not give output and it returns only void.

Example

// using class that we made above

class ClassName
{
   // making the method

   public void addition(int x, int y)
   {
      Console.WriteLine("Your sum is equal to
         {0}", x + y);
      Console.ReadLine();
   }
}

The third method does not take input, but gives output. It also returns the return type.

Example

// using class that we made above

class ClassName
{
   // making the method

   public int addition()
   {
      int x = 30;
      int y = 20;
      return x + y;
   }
}

The fourth method only processes instead of generating input and output.

Example

// using class that we made above

class ClassName
{
   // making the method

   public void addition()
   {
      int x = 30;
      int y = 20;
      Console.WriteLine("Your sum is equal to :
         {0}", x + y);
      Console.ReadLine();
   }
}

Whereas, the instance method and non-instance method also are categories of methods that are given below:

Instance Method

A method that can call through objects is known as an instance method. One also can call it a non-static method.

Example

class Mathematics   //class
{
   public int GetSum(int x, int y)   // instanced method
   {
      return x + y;
   }
}

static void Main(string[] args)   //main program

{

   Mathematics m = new Mathematics();
   m.GetSum(12, 45);   // instanced method call

}

Non-instance Method

A method that can call directly without making any object is known as a non-instance method. Static members can call only static members. It is also known as a static method.

Example

class Mathematics   //class
{   //static methods
   public static long GetProduct(int x, int y)
   {
      return x * y;
   }
}

static void Main(string[] args)
{
   // non-Instanced method call or
   // static method call
   Mathematics.GetProduct(12, 33);
}

Inheritance

Inheritance is a way to achieve functionality between two different classes. We can use methods of a parent class in a child class but we can inherit only a single class at a time. This is because multi-inheritance is not allowed in C#. But, multi-level inheritance is allowed in C#.

Syntax

Class firstClassName : secondClassName
{
   // body
}

Example

These are two different classes, called “TeachingStaff” and “NonTeachingStaff,” which are given below:

class NonTeachingStaff   // first class or parent class
{
   public void fees collection()
   {
      Console.WriteLine("Used to collect fees from students");
   }

   public void Annoucement()
   {
      Console.WriteLine("Used to make annoucement");
   }

   class TeachingStaff   // second class or child class
   {
      public void Subject()
      {
         Console.WriteLine("Computer,English,Science");
      }
   }
}

After Inheritance

class Teaching:NonTeachingStaff   // inheritance
{
   public void subject()

   {
      Console.WriteLine("Computer,English,Science");

   }
}

Polymorphism

Getting multiple ways of performing an action is known as polymorphism. Polymorphism provides different varieties of methods. There are two types of polymorphism:

  • Static polymorphism
  • Dynamic polymorphism

Static Polymorphism

Static polymorphism uses its own method without using inheritance. Or, we can also say that having two or more methods of the same name but different signatures is known as static polymorphism. Static polymorphism is also known as method overloading.

We use method overloading when we have two or more than two methods of the same name in the same class. For this, we change the data type of parameters if all methods have the same number of parameters and names, or we change the number of parameters if all the methods have the same data types and the same name.

Example

class Mathematics
{
   public int GetSum(int x, int y)
   {
      return x + y;
   }

   //GetSum(2 float)
   public float GetSum(float x, float y)
   {
      return x + y;
   }

   // GetSum(3 float)
   public float GetSum(float a, float b, float c)
   {
      return a + b + c;
   }
}

Dynamic Polymorphism

Dynamic polymorphism uses its own method with using inheritance. Method overriding defines the dynamic polymorphism so that we can also say that when we have two classes having the same methods, after inheritance the compiler got confused about the implementation of the method. The solution is to make the parent’s method “virtual” andthe child’s method “override.” This method is called method overriding or dynamic polymorphism.

We use method overriding when we have the same names of methods in different classes. For this, the “virtual” keyword means to hide the method, and “override” means to use this method. We can use only the “virtual” keyword with one method, but the “override” keyword can be used with multiple methods.

Example

class Finance
{
   public virtual void check()
   {
      Console.WriteLine("Virtual method example");
   }

}


class Male:Finance
{
   public override void check()
   {
      Console.WriteLine("override method example");
   }
}


class Female:Finance
{
   public override void Speak()
   {
      Console.WriteLine("override method example");
   }
}

Abstraction

Abstract classes are those classes that provide a template to other classes and abstract classes that do not allow you to make objects. Abstract classes could have both abstract and non-abstract methods at a time.

We used to write an “abstract” keyword with abstract classes and also with the abstract methods that define its signature in abstract classes. We used to write an “override” keyword with the abstract methods when we define their body in another class that inherits an abstract class.

Example

abstract class Parent
{
   abstract public void AnFirstAbstractMethod();
   abstract public void AnSecondAbstractMethod();
   public void NonAbstract()
   {
      Console.WriteLine("This is an non-abstract method
         in an abstract class");
   }
}




class Child: Parent
{
   public override void AnFirstAbstractMethod ()
   {
      Console.WriteLine("This is a first abstract method's
         body which is defined in a non-abstract class which
         inherits abstract class ");
   }
   public override void AnSecondAbstractMethod ()
   {
      Console.WriteLine("This is a second abstract method's
         body which is defined in a non-abstract class which
         inherits abstract class ");
   }

}



class Program
{
   static void Main(string[] args)
   {

      Child obj = new Child();
      Console.ReadLine();
   }
}

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read