Introduction to Object-Oriented Programming

Imagine the scene of a production unit of a car at a BMW Motors manufacturer, consider a situation where an engineer is provided specialized tools and are told to make a luxurious car.production unit of a car

Similarly in software development, a model and a proper design is needed to develop an application. There are mainly the following two types of programming models that are broadly followed when developing a software application:
  1. Procedural Oriented Programming (C, FORTRAN, COBOL)
  2. Object Oriented Programming (C++, Java, C#)
The differences among them is, Procedural Oriented Programming is based on the concept of breaking a problem into modules and solving each module separately , and later on, combining all the solved modules as a single unit to solve the entire problem. Here a module can be a method that consists of a block of some code and in this model we can call one method from another method that implies that each method is dependent of other methods and hence, making the reusability of our code is quite difficult to make a sense in this approach.
 
So basically, to overcome such difficulty in a Procedure Oriented Language, the model of Object Oriented Programming has been introduced.
 
When a Procedure Oriented Programming program executes, the compiler runs through the code line-by-line, whereas with object oriented classes, we call class methods, properties and events at any point in our code. When we call a method, the execution process jumps to the corresponding class and returns to the next line of execution.
 
Difference b/w POP and OOP 

 
 
Note
 
The C# language is written with Visual Studio, so you can step through your class code to see the flow of execution. It has the great feature of debugging the program. I'll cover this in separate article.
 
OOP uses the concept of classes and objects to reuse the existing code.
 
Now let's consider the motor car production.
 
If a car were designed as a single body machine, it would be seen as hugely complex with many opportunities for failure and bugs.
 
However, when broken down into its constituent parts, such as wheels, pedals, doors and so on. the individual design items become simpler.
 
Each part (or object) is created independently, tested carefully and then assembled into the final product. The creation of the parts can be simplified further when they are broken down into even simpler items.
 
For example, each door is considered as being composed of an outer panel, handle, inner panel and window.
 
The car example is similar or equivalent in this respect to the object-oriented software. Rather than writing a huge program to create, for example, a project management system, the solution is broken into real-world parts such as project, task, estimate, actual, deliverable and so on.
 
Each of these can then be developed and tested independently before being combined.
 
Note: C# is a completely true object oriented language.
 
Let's dig on.
 
Classes and Objects
 
The basic building blocks of object-oriented programming are the class and the object (OOP Hero).
 
A class acts as a blueprint/template to create the instances/objects. This blueprint/template describes the state and behaviour the state and behaviour for all the objects of the class.
 
Or in other words, a class defines the available characteristics and behaviour of a set of similar objects and is defined by a programmer in code. A class is an abstract definition that is made concrete at run-time when objects based upon the class are instantiated and take on the class's behaviour.
 
Note: Classes are essentially templates from which we can create objects as in the following:
  1. Classes allow us to control all the functions that can be applied to a given set of data as well as how to access the data.
  2. Classes also provide us the ability to extend a runtime environment and allow the developer to reuse the existing functionalities of that runtime environment. To understand the meaning of this line let's suppose we need to create an object of a class named Student, so in our program and to do so we first of all need to create a Studnet Class that contains all required functionalities or behaviours and properties of any student under this. Then use that class to create the objects as needed.
  3. Objects help us to access the member of a class either they can be fields, methods or properties, by using the dot.

Look at the following code snapshot.

  1. Object is a collection of number of entities.
  2. Objects take up space in the memory.
  3. Objects are instances of classes.
  4. When a program is executed , the objects interact by sending messages to one another.
  5. Each object contain data and code to manipulate the data.
  6. Objects can interact without having know details of each others data or code. objects
Let's take another analogy instance, let's consider the concept of a "vehicle" class as shown in the image above. The class developed by a programmer would include methods such as Steer(), Accelerate() and Brake(). The class would also include properties such as Colour, NumberOfDoors, TopSpeed and NumberOfWheels. The class is an abstract design that becomes real when objects such as Car, RacingCar, Tank and Tricycle are created, each with its own version of the class's methods and properties.
 
Simple Class Decaration syntax 
 
Class <YourClass Name> {
<access-modifiers> [static] <return-type> methodname(<signature>);
<access-modifiers> [static] <variable type> variablename;
}
 
Access Modifiers (public/internal/protected/private/protected internal) help us to avoid jumbling of data and methods with existing code as well as protect an object of a class from outside interferences.
  1. public: access to members, both inside and outside a class without any restriction.
  2. internal: access to members, only the current assembly can access these members.
  3. protected: access to members, from either the class in which they are declared or a class from the class in which they are declared.
  4. private: access to members of the current assembly, the containing class and the class derived from the containing class.
General syntax to declare a class:

[public,
internal,
abstract,
sealed, static,
unsafe,]
partial] class YourClassName [ Generic type parameters, a base class, and interfaces]
{
    methods,
    properties,
    indexers,
    events,
    fields,
    constructors,
    operator functions,
    nested types,
    a finalizer
}

The basic syntax of declaring or creating an object is:

<classname> objectname = new <classname> ();

Example
 
creating an object

About Basic Fundamentals of OOP that includes Encapsulation, Inheritance, Polymorphism and Abstraction.
 

Since they are a basic foundation of the programming paradigm, I will explain them separately in following articles.

Next Recommended Readings