Object-Oriented Programming (OOP) Concept in C#.NET
Getting your OOP Concepts clear is the first and the most important step towards becoming a C# Programmer.
As the name suggests, "Object Oriented" means everything is revolving around Objects. To understand what Objects mean, Let's start with a simple Question.
What are the components of a computer program?
A computer program has 2 components.
Data consist of all the variables in a program which can be modified or used. For Example: variables, constants etc.
Code consists of statements that actually makes use of the Data and manipulate it. For Example, functions, methods, procedures etc.
int i = 10, j = 20, k; // <- Data
k = i + j; // <- Code that operated on i j and k
There are two approaches to programming.
- Procedure Oriented Programming: The program's revolves around "What to do and how to do it". It consists of statements that execute one by one and preforms a specific task. The focus is on CODE.
- Object Oriented Programming: The program's revolves around "What is being changed". The main focus is on the DATA. There is some DATA and then there are methods which have controlled access to it. We will discuss about "Objects" later in detail. For now, just understand that everything is considered an Object and has certain operations that can be performed on that object based on the type of object.
A dog is an Object, a book is an Object, a car, a building , everything is an object! and each object has its own properties and we write methods to change these properties.
For Example: A car would have properties like color, model, current_speed. The methods would be accelerate() and stop().
Let's say that the color is "white", model is "Honda City" and when it is parked the current_speed is 0.
When accelerate() method is called it increases the current_speed of the car and the stop() method starts decreasing the speed till the current_speed is back to 0.
We will discuss the PILLARS of Object Oriented Programming in the next page.
(There can be other properties too like seating_capacity, mileage etc. and other methods like change_color(), brake() etc but we have not mentioned them here to avoid complexity)