Understanding object-oriented programming

Object-oriented programming is a programming concept that is based on objects. An object is a collection of related data such as fields and procedures, that is, methods. For example, an object could be anything right from a very simple object such as a pencil to a very complex type such as a car. Each object will have its own set of attributes, that is, properties and functions or the methods that are implemented in that object. For example, for a car object, the possible attributes could be color, registration number, model, and so on. The possible functions could be start, stop, and accelerate.

Before object-oriented programming came into the picture, we did our programming under the principles of procedural programming. In a procedural language, an application was divided into a set of functions. The data that was used in the program was stored in a bunch of local variables that were used by the functions. It formed the basis of legacy programming languages such as COBOL and BASIC. 

The main disadvantages of this programming concept were as follows:

  • No code reuse: As the entire application was divided into a set of sequential functions, there was no code reuse in this programming concept. 
  • Maintenance and scalability: The following diagram is just an indicative flow of how a typical program written in a procedural language will run. In the program, the blocks indicate the different code functions, which are interlinked and interacting with each other to complete a task:

Here, as in any typical program written on procedural language, there would be a bunch of functions, passing around parameters and executing them in a conceptual manner. So, any change or upgrade that's made to any one of these functions will have a good chance of causing issues in the execution of another function. Therefore, from both the standpoint of maintenance and scalability, an application that's written in a procedural language will have its challenges. 

In object-oriented programming, each application can be divided into a bunch of objects that have their own properties and procedures. For example, let's consider the same car scenario we explored in the previous chapter. A car can have the following properties and methods:

Now, what we can do in an object-oriented programming language is directly declare a car object, set the values related to the properties, and call the corresponding properties, such as start. 

Since the object is grouped with the corresponding properties, we don't need to pass any data for the respective properties. Instead, while executing the respective functions such as start, stop, or accelerate, we can then pass data against these properties as a function parameter list.

Thus, in this case, if, in the future, we change the start() function of the car class, we won't need to bother all the other places that it's getting called from. 

This is a major upgrade in terms of the maintainability and scalability of the application compared to the standard way of doing things in a procedural language.

Now, let's deep dive into each of the four pillars of an object-oriented language and understand how we can use them in our C# applications.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
52.15.55.18