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:
- Use right click to see option
- The click on the add option
- After it click on class option.
- And give name to the class with “.cs” 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
- static class Calculation
- {
- public static long GetProduct(int x, int y)
- {
- return x * y;
- }
-
- }
-
- class Program
- {
- static void Main(string[] args)
- {
- Calculation.GetProduct(12, 33);
-
- }
- }
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
- abstract class Human
- {
- abstract public void FirstAbstractMethod();
- abstract public void SecondAbstractMethod ();
- public void NonAbstractMethod()
- {
- Console.WriteLine("This is an non abstract method in a abstract class");
- }
- }
-
- class Man: Human
- {
- public override void FirstAbstractMethod ()
- {
- Console.WriteLine("This is an first abstract method’s body which is define in a non-abstract class which inherit abstract class ");
- }
- public override void SecondAbstractMethod ()
- {
- Console.WriteLine("This is an second abstract method’s body which is define in a non-abstract class which inherit abstract class ");
- }
-
- }
-
- class Program
- {
- static void Main(string[] args)
- {
-
- Man ali = new Man();
- ali. FirstAbstractMethod();
- ali. SecondAbstractMethod();
-
-
- }
- }
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
- 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 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
- 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 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:
- className objectName = new classConstructor();
Now I am using the previous example of class and making its object.
- 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
-
-
- class ClassName
- {
-
-
- public int addition(int x, int y)
-
- {
- return x + y;
- }
- }
Second method is that which only takes input but does not give output and it only returns void.
Example -
-
- class ClassName
- {
-
-
- public void addition(int x, int y)
- {
- Console.WriteLine("Your sum is equal to {0}", x + y);
- Console.ReadLine();
- }
- }
Third method does not take input but gives output. It also returns the return type.
Example -
-
- class ClassName
- {
-
-
- public int addition()
- {
- int x = 30;
- int y = 20;
- return x + y;
- }
- }
Fourth method only processes instead of inputting and outputting.
Example -
-
- class ClassName
- {
-
-
- public void addition()
- {
- int x = 30;
- int y = 20;
- Console.WriteLine("Your sum is equal to : {0}", x + y);
- Console.ReadLine();
- }
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
- class Mathematics
- {
- public int GetSum(int x, int y)
- {
- return x + y;
- }
- }
-
- static void Main(string[] args)
-
- {
-
- Mathematics m = new Mathematics();
- m.GetSum(12, 45);
-
- }
- 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
- class Mathematics
- {
- public static long GetProduct(int x, int y)
- {
- return x * y;
- }
- }
-
- static void Main(string[] args)
- {
- Mathematics.GetProduct(12, 33);
- }
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
- Class firstClassName : secondClassName
- {
-
- }
Example
These are two different classes called “TeachingStaff” and “NonTeachingStaff” which are given below:
- class NonTeachingStaff
- {
- public void fees collection()
- {
- Console.WriteLine("Used to collect fees from students");
- }
-
- public void Annoucement()
- {
- Console.WriteLine("Used to make annoucement");
- }
-
- class TeachingStaff
- {
- public void Subject()
- {
- Console.WriteLine("Computer,English,Sceince");
- }
- }
After Inheritance - class Teaching:NonTeachingStaff
- {
- public void subject()
-
- {
- Console.WriteLine("Computer,English,Sceince");
-
- }
-
- }
Polymorphism
Getting multiple ways of an action is known as polymorphism. Polymorphism provide 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 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
- class Mathematics
- {
- public int GetSum(int x, int y)
- {
- return x + y;
- }
-
- public float GetSum(float x, float y)
- {
- return x + y;
- }
-
- 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 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
- 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 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 - abstract class Parent
- {
- abstract public void AnFirstAbstractMethod();
- abstract public void AnSecondAbstractMethod ();
- public void NonAbstract()
- {
- Console.WriteLine("This is an non abstract method in a abstract class");
- }
- }
-
- class Child: Parent
- {
- public override void AnFirstAbstractMethod ()
- {
- Console.WriteLine("This is an first abstract method’s body which is define in a non-abstract class which inherit abstract class ");
- }
- public override void AnSecondAbstractMethod ()
- {
- Console.WriteLine("This is an second abstract method’s body which is define in a non-abstract class which inherit abstract class ");
- }
-
- }
-
- class Program
- {
- static void Main(string[] args)
- {
-
- Child obj = new Child();
- Console.ReadLine();
- }
- }