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.
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:
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:
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:
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:
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:
- Compile-time polymorphism (also known as Static polymorphism, Overloading, or Early binding)
- Runtime polymorphism (also known as Dynamic polymorphism, Overriding or late binding)
Original Source: Click here
No comments yet