Introduction
It is important for a developer to understand the concepts of OOPS or any programming language concepts through real-world examples.
Starting in C#, the first thing which comes into the picture is objects and classes.
Class
A class is a data type that defines a collection of methods and properties in a unit. A class contains one or more objects (I will explain object in the next point, just remember object is described by the class which acts as an instance) which does not change during the execution of a program.
Real-World Example
Class acts like a blueprint. We have a blueprint of a house through which we can make many houses which will have the same properties and methods (operations). Through that blueprint, we can make a house of Mr. Aditya and through that blueprint, we can make a house of Mr. Amatya.
Object
An object is an instance of a class. There can be more than one instances of a class and they contain their own data.
Real-World Example
Here, the houses of Mr. Amatya and Mr. Aditya can be treated as objects of class House.
- Blueprint of House - Class
- House of Mr. Amatya - Object
Array and List
An array has a fixed size with continuous memory allocation of the same datatype. An array index starts at zero.
Declaring array - datatype[] arrayName;
Real-World Example
If Mr. Shryansh uses debit cards, therefore, he will purchase a wallet which has 2-3 slots where he can keep his cards.
He will place his cards in a continuous way so it will be easy and effective. Through this example, you can remember this concept.
Now, say after a few years, he got 4 debit cards and he is having three slots to keep his cards, now his wallet will not be able to keep the cards in an effective way. He will have to purchase a new wallet which should have at least 4 slots for keeping the cards. Though this scenario, you can get the demerits of Array.
Whereas list is a generic type which provides most of the collection's related built-in methods and properties including add, remove, search, and sort.
Example - List<int> numbers = new List<int>();
Real-World Example
Suppose you are in a general store, but you don't know what items to buy. By seeing the offers and requirements you can buy 10 items or even 20 items. Here List comes into the picture. Your items are not fixed and you are adding the items based on your requirements, deals, and offers. After picking 20 items from the store, you calculated the price and it exceeds from 1000 (as you brought only 1000 rupees), so you want to remove some items which are not important or you want it after some days.
So you removed 5 items and the bill was in your budget (say it becomes 900rs). In this scenorio, you can remember the concept of Lists. (Some will be confused with datatypes as List has same datatypes for its items, so say here all the items which I have taken is in String dataType)
Abstraction
This is a concept of showing essential features and hiding non-essential features from the users.
Real-World Example
- Sign up for a Facebook account. Nowadays, everyone above 18 has a Facebook account.
We just fill in our details and our account is created, we don't have to know that how the application is saving our details and how it's showing the details when we login in our respective account. So hiding the details of the saving process is termed as Abstraction in OOPS(C#)
- Switching the fan on -- we don't know after switching the fan how the process works and the fan began to move.
- Applying breaks to cars, we don't know after applying the break, how the car stops.
Encapsulation
Encapsulation means that which binds the data, operations, and methods in a single unit; i.e., class.
Both Abstraction & Encapsulation work hand in hand because Abstraction says what details need to be made visible
and Encapsulation provides the level of the access right to that visible detail; i.e. implements the desired level of abstraction.
Real-World Example
A car is having multiple parts like wheels, engine, steering, gears, etc. which bind together to form an object that is a car. So, we can see that multiple parts of cars encapsulate itself together to form a single object.
Encapsulation = Abstraction + Data Hiding.
Encapsulation is basically used for security reasons.
Inheritance
Inheritance is a concept of deriving a new class from the existing class.
The derived class gets all the features from the base and also some new features can be added to it.
There are mainly four types of inheritance,
- Single level inheritance
- Multi-level inheritance
- Hierarchical inheritance
- Hybrid inheritance
Real-World example of Single level inheritance
In Single level inheritance, there is a single base class & a single derived class.
Maruti --> Swift
Here Maruti is base class and Swift is a derived class.
Real-World example of Multilevel inheritance
In Multilevel inheritance, there is more than one single level of derived class.
Maruti --> Swift --> SwiftDzire
Here Maruti is base class and Swift, SwiftDzire are derived classes.
Real-World example of hierarchical inheritance
Here multiple derived class would be extended from the base class.
|-->> Swift
|
Maruti --
|
|-->> Alto
A real-world example of Hybrid inheritance -
Single, Multilevel, & hierarchical inheritance all together construct a hybrid inheritance.
|-->> Swift -->> SwiftDzire
|
Maruti --
|
|-->> Alto
Polymorphism
‘Many forms of a single entity’.
Real-World Example
- We behave differently in front of parents, seniors, boss, and friends.
- The playground performs multiple acts such as marriage ceremony, cricket matches, religious functions etc.
- Akshay Kumar looks differently in Sing is King, Gabbar, Airlift, and Namaste London movies.
I hope it will help student or freshers in the IT industry.
Thanks.