In this article I am going to discuss about C# Object-Oriented Programming Concepts in detail but before that if you had not read my previous article of C# Object-Oriented Programming then must read it for more clear understanding. We had already covered why object-oriented programming is to used? And now we are going to discuss about the C# object-oriented programming concepts in detail.
What is Object-Oriented Programming Concepts?
In C#, or any OOP language there are mainly four object-oriented programming concepts or principles which is used to implement object-oriented programming in our application. With this concepts we can use various features like code reusability , data hiding , solving complex problems and so on. So lets discuss the C# Object-Oriented programming concepts in detail.
1. Encapsulation
The wrapping up of data member and member functions into single unit is called Encapsulation. In simple words we can say that defining the variables and functions inside one class is called Encapsulation. We can achieve this encapsulation using classes and object. With Encapsulation we can also hide our data using access specifiers like private, public and protected. By defining public access specifer we can access this from anywhere but if it private then it is not accessible to outside of block. We have already done so many programs where we used the public access specifier.
2. Inheritance
Inheritance is one of the most important C# object-oriented programming concepts because with inheritance we can use the feature of code reusability. This refers to the process where we can use the properties of one class into another class. In simple words we can say that inheritance allow us to inherit the properties of one class to another class. In inheritance from where we inheriting the properties is called base class and the class who inherits the properites is called derived class. There are different-2 types of inheritance are used as per requirements.
1. Single Inheritance : In this type of inheritance there is only one base class and one derived class.
public class BaseClass { public string MorningMessage() { return "Good Morning"; } } public class DerivedClass : BaseClass //Single Inheritance { public string NightMessage() { MorningMessage(); //derived class method return "Good Night"; } }
2. Hierarchical Inheritance : In this type of inheritance there is only one base class but there are multiple derived classes who inherited from one base class.
public class BaseClass { public string MorningMessage() { return "Good Morning"; } } public class DerivedClass1 : BaseClass //Hierarchical inheritance { public string EveningMessage() { MorningMessage(); //derived class method return "Good Evening"; } } public class DerivedClass2 : BaseClass //Hierarchical inheritance { public string NightMessage() { MorningMessage(); //derived class method return "Good Night"; } }
3. Multilevel inheritance : In this type of inheritance one class is inheriting from another class and again another class is inheriting from third class. In simple words when one derived class is inheriting from another class is called multilevel inheritance.
public class Person { public string personDetails() { return "Person class"; } } public class Bird : Person { public string birdDetails() { personDetails(); return "Bird Class"; } } public class Animal : Bird { public string animalDetails() { personDetails(); birdDetails(); return "Animal Class"; } }
4. Multiple Inheritance : When our derived class inherits from more than one class then it is called multiple inheritance. Normally we cannot use multiple inheritance in csharp using classes because C# does not support multiple inheritance but we can implement multiple inheritance using interfaces. Till we have not studied the interfaces but don’t worry we will discuss interfaces in our next articles.
public interface IA //Interface 1 { string setCarDescription(string a); } public interface IB //Interface 2 { int getAmount(int Amt); } public class Car : IA, IB //implementation { public int getAmount(int Amt) { return 100; } public string setCarDescription(string a) { return "this is the car"; } }
3. Polymorphism
In C# polymorphism is also one of the important object-oriented programming concepts which is used to show different behaviour of same function by changing in arguments. Polymorphism is greek word which is combination of two words poly that means many and morph means behaviour or forms. Thus polymorphism means one object has many forms or has one name with multiple functionalities. Polymorphism is also referred to as the third pillar of C# object-oriented programming concepts. There are two types of polymorphism are there :
- Static polymorphism or compile-time polymorphism
- Dynamic polymorphism or run time polymorphism
1. Compiler-time Polymorphism : This type is also called early binding. We can achieve compile time polymorphism using method overloading. Method overloading means to create the functions with same name but with different number of arguments.
2. Run-time Polymorphism : This type is also called late binding. We can achieve run time polymorphism using method overriding and virtual methods. Method overriding means to override the same function in another class.
4. Data Abstraction
Data abstraction is the process of hiding certain details and showing only essential information to the user. We can achieve data abstraction in csharp using abstract classes and interfaces. In simple words it is the process of representing the essential features without including the background details.
Conclusion
In this C# tutorials article we have discussed about C# object-oriented programming concepts in detail with its types. Hope you found this article helpful and like this article. In my next article I am going to discuss about classes and object which is very important in C# object-oriented programming concepts because with classes and object we can achieve the functionality of encapsulation.