Why we need classes and objects?
Understand the advantages and uses of classes and objects in object oriented programming
Apart from representing the real life objects, classes and objects are useful in various other ways. As we have seen in this tutorial so far, it is very easy to perform the console input and output operations using Console.ReadLine and Console.WriteLine. However, we do not know how they work internally. This is because their working is already coded in the base classes of the .NET Framework. All we need to know is how to use Console.ReadLine and Console.WriteLine in our applications. We do not need to know the exact code that makes them work. This is known as encapsulation in object-oriented programming. Once a class has been defined and tested, developers can use it in their applications without knowing the code behind it.
Another advantage of using classes is that we can extend them. Let us understand this with an example. We first create a class named `Person'. The class has properties like name, age and gender. Then we create another class `student'which would have the properties name, age, gender, grade and stream. Notice that some properties of the class `student' are the same as in the `Person' class. We can define the `student' class from scratch but why should we write everything again? We can inherit the `student' class from the `Person' class. This will pass on the properties of the `Person' class to the `student' class. We just need to add the extra properties in the `student' class. In fact, we can extend the `Person' class in other classes too. An `employee' class or a `teacher' class can be created based on the `Person' class. This is a main feature of object-oriented programming and is known as Inheritance. A class can be extended to create other classes. This saves a lot of coding time for the developers as they don't have to write everything from the beginning.