Object Oriented Programming In C#

Introduction

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

Pillars of Object Oriented Programming

Nowadays, 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 object oriented programming language which are used to make instance or object. Class is a template which is necessary to make instances or objects. If the class is not declared as static, coder can use class to create objects and methods. There are four types of classes which are given below:

  • 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 understand  instead of written steps
Steps to follow:

  1. Use right click to see option
  2. The click on the add option
  3. After it click on class option.

    class

  4. And give name to the class with “.cs” extension,

    extension

    extension

Types of classes

Static Class

Static classes are those classes which have no objects and it works without an object. It is also 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 we can make static method in a non-static class.

Example

  1. static class Calculation  
  2.  {  
  3. public static long GetProduct(int x, int y) //static methods  
  4.         {  
  5.             return x * y;  
  6.         }  
  7.   
  8.  }  
  9.   
  10. class Program  
  11.     {  
  12.         static void Main(string[] args)  
  13.         {   
  14.             Calculation.GetProduct(12, 33);  
  15. //static method call  
  16.         }  
  17.     }  
Abstract Class

Abstract classes are those classes which cannot make objects. An abstract class could have abstract and non-abstract methods at a time but abstract method can only be made in an abstract class. In abstract class we can only define 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 that define it in a class which inherit an abstract class.

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

Example
  1. abstract class Human  
  2.     {  
  3.         abstract public void FirstAbstractMethod();  
  4.         abstract public void SecondAbstractMethod ();  
  5.         public void NonAbstractMethod()  
  6.         {  
  7.             Console.WriteLine("This is an non abstract method in a abstract class");  
  8.         }  
  9.     }  
  10.   
  11. class Man: Human  
  12.     {  
  13.         public override void FirstAbstractMethod ()  
  14.         {  
  15.             Console.WriteLine("This is an first abstract method’s body which is define in a non-abstract class which inherit abstract class ");  
  16.         }  
  17.         public override void SecondAbstractMethod ()  
  18.         {  
  19.             Console.WriteLine("This is an second abstract method’s body which is define in a non-abstract class which inherit abstract class ");  
  20.         }  
  21.           
  22.     }  
  23.   
  24. class Program  
  25.     {  
  26.         static void Main(string[] args)  
  27.         {  
  28.               
  29.             Man ali = new Man();  
  30.         ali. FirstAbstractMethod();  
  31.     ali. SecondAbstractMethod();  
  32.       
  33.        
  34.         }  
  35.     }  
Sealed Class

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

Example
  1. sealed class example  
  2.     {  
  3.         public int SealedMethod()  
  4.         {  
  5.             int x = 2;  
  6.             return x;  
  7.         }  
  8.   
  9.     }  
  10.   
  11. class Program  
  12.     {  
  13.         static void Main(string[] args)  
  14.         {  
  15.             example Obj = new example();  
  16.             Obj.SealedMethod();  
  17.   
  18.         }  
  19.     }  
Partial Class

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

Example
  1. partial class Employee  
  2. {  
  3.     public void methodA()  
  4.     { }  
  5.     public void methodB()  
  6.     { }  
  7. }  
  8.   
  9. class Finance  
  10.     {  
  11.     }  
  12.   
  13.     partial class Employee  
  14.     {  
  15.         public void methodX()  
  16.         { }  
  17.         public void methodY()  
  18.         { }  
  19.     }  
Objects

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

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

The format of object is given below:
  1. className objectName = new classConstructor();  
Now I am using the previous example of class and making its object.
  1. ClassName obj = new ClassName();   
Methods

Objects are used to call methods. Method are action of the class. Method used to give input, process and output. All methods must be public and local variables only accessible for method it contain

There are four types of methods,

A method which takes input from user and returns to him/her the output. Means it gets  input and gives output and it also returns “Return type”.

Example
  1. // using class which we made above  
  2.   
  3. class ClassName   
  4. {  
  5. // making method  
  6.   
  7.         public int addition(int x, int y)  
  8.           
  9. {  
  10.             return x + y;  
  11.         }   
  12. }  
Second method is that which only takes input but does not give output and it only returns void.

Example
  1. // using class which we made above  
  2.   
  3. class ClassName  
  4.     {  
  5.         // making method  
  6.   
  7.         public void addition(int x, int y)  
  8.         {  
  9.             Console.WriteLine("Your sum is equal to {0}", x + y);  
  10.             Console.ReadLine();  
  11.         }  
  12.     }  
Third method does not take input but gives output. It also returns the return type.

Example
  1. // using class which we made above  
  2.   
  3. class ClassName  
  4.     {  
  5.         // making method  
  6.   
  7.         public int addition()  
  8.         {  
  9.             int x = 30;  
  10.             int y = 20;  
  11.             return x + y;  
  12.         }  
  13.     }  
Fourth method only processes instead of inputting and outputting.

Example
  1. // using class which we made above  
  2.   
  3. class ClassName  
  4.     {  
  5.         // making method  
  6.   
  7.         public void addition()  
  8.         {  
  9.             int x = 30;  
  10.             int y = 20;  
  11.             Console.WriteLine("Your sum is equal to : {0}", x + y);  
  12.             Console.ReadLine();  
  13.         }  
Whereas instance method and non-instance method are also categories of methods which are given below: 
  • Instance Method

    A method which can call through objects is known as instance method. One can also call it non static method.

    Example
    1. class Mathematics //   
    2. {  
    3.   public int GetSum(int x, int y)//instanced method  
    4.         {  
    5.             return x + y;  
    6.         }  
    7. }  
    8.   
    9. static void Main(string[] args) //main program  
    10.   
    11.         {  
    12.               
    13. Mathematics m = new Mathematics();  
    14. m.GetSum(12, 45); //instanced method call  
    15.           
    16.          }  
  • Non-Instance Method

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

    Example
    1. class Mathematics //class  
    2. {  //static methods  
    3.  public static long GetProduct(int x, int y)   
    4.         {  
    5.             return x * y;  
    6.         }  
    7. }  
    8.   
    9. static void Main(string[] args)  
    10. {              
    11. Mathematics.GetProduct(12, 33);// non-Instanced method call or static method call  
    12. }  

Inheritance

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

But multi-level inheritance is allowed in C#.

Syntax

  1. Class firstClassName : secondClassName  
  2. {  
  3.     //body  
  4. }  
Example

These are two different classes called “TeachingStaff” and “NonTeachingStaff” which are given below:
  1. class NonTeachingStaff // first class or parent class  
  2.     {  
  3.         public void fees collection()  
  4.         {  
  5.             Console.WriteLine("Used to collect fees from students");  
  6.         }  
  7.   
  8.         public void Annoucement()  
  9.         {     
  10.             Console.WriteLine("Used to make annoucement");  
  11.         }  
  12.   
  13. class TeachingStaff // second class or child class  
  14.     {  
  15.         public void Subject()  
  16.         {  
  17.             Console.WriteLine("Computer,English,Sceince");  
  18.         }  
  19. }  
After Inheritance
  1. class Teaching:NonTeachingStaff // inheritance  
  2.  {  
  3.   public void subject()  
  4.   
  5.         {  
  6.                 Console.WriteLine("Computer,English,Sceince");  
  7.   
  8.         }  
  9.   
  10.  }  
Polymorphism

Getting multiple ways of an action is known as polymorphism. Polymorphism provide different varieties of methods. There are two types of polymorphism, 
  1. Static polymorphism
  2. 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 same names 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 same name in same class, for this we change data type of parameters if all methods have same number of parameter and name or we change number of parameter if all the methods have same data types and same name.

Example

  1. class Mathematics  
  2.     {  
  3.         public int GetSum(int x, int y)  
  4.         {  
  5.             return x + y;  
  6.         }  
  7.   
  8.         public float GetSum(float x, float y) //GetSum(2 float)  
  9.         {  
  10.             return x + y;  
  11.         }  
  12.   
  13.         public float GetSum(float a, float b, float c) // GetSum(3 float)  
  14.         {  
  15.             return a + b + c;  
  16.         }  
  17.     }  
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 same methods then after inheritance, the compiler got confused  about implementation of method. The solution is to make parent’s method “virtual” and child’s method “override” this method is called method overriding or dynamic polymorphism.

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

Example
  1. class Finance  
  2.     {  
  3.         public virtual void check()  
  4.         {  
  5.             Console.WriteLine("Virtual method example");  
  6.         }  
  7.         
  8.     }  
  9.   
  10.   
  11.   class Male:Finance  
  12.     {  
  13.         public override void check()  
  14.         {  
  15.             Console.WriteLine("override method example");  
  16.         }  
  17.     }  
  18.   
  19.   
  20. class Female:Finance  
  21.     {  
  22.         public override void Speak()  
  23.         {  
  24.             Console.WriteLine("override method example");  
  25.         }  
  26.     }  
Abstraction

Abstract classes are those classes which provide templates to other classes and abstract classes do not allow making objects. Abstract classes could have both abstract and non-abstract methods at a time.

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

Example 
  1. abstract class Parent  
  2.     {  
  3.         abstract public void AnFirstAbstractMethod();  
  4.         abstract public void AnSecondAbstractMethod ();  
  5.         public void NonAbstract()  
  6.         {  
  7.             Console.WriteLine("This is an non abstract method in a abstract class");  
  8.         }  
  9.     }    
  10.   
  11. class Child: Parent  
  12.     {  
  13.         public override void AnFirstAbstractMethod ()  
  14.         {  
  15.             Console.WriteLine("This is an first abstract method’s body which is define in a non-abstract class which inherit abstract class ");  
  16.         }  
  17.         public override void AnSecondAbstractMethod ()  
  18.         {  
  19.             Console.WriteLine("This is an second abstract method’s body which is define in a non-abstract class which inherit abstract class ");  
  20.         }  
  21.           
  22.     }    
  23.   
  24. class Program  
  25.     {  
  26.         static void Main(string[] args)  
  27.         {  
  28.               
  29.             Child obj = new Child();  
  30.             Console.ReadLine();   
  31.         }  
  32.     }