Object Oriented Programming: Objects and Classes

In terms of object-oriented programming, one of the most fundamental concepts is that of a class.

A class is like a template which we can use to build entities within our software. These templates can indicate the data that our entity will be able to hold, as well as any functionality that our entity can perform. Then with the classes we can model entities that will store data and functionality.

An example of a class would be the Car class, which allows me to:

  • Save data about a specific car, such as its color, its model, brand, year of manufacture, among others. In specific terms, the data of a class is typically stored in variables, called fields. We can also use properties for this.
  • Define the functionality that the car can perform, such as accelerating, stopping, turning on music, among others. We do this through functions that we declare within the class.

Let’s see an example of a class called Car:


public class Car

{

  public string Brand;

  public int ModelYear {get; set; }

  public void Accelerate()

  {

  }

}

This class can save data, such as the brand of the car and its year of release to the market, and has a function called Accelerate, which allows it to execute some functionality.

A moment ago we called the classes as a template, if it is a template, then that means we can use it to build something. We call that something we can build from a class an object.

An object is the instance of a class. When we instantiate a class, we obtain a particular object which can hold data in the way that is indicated in the class. In addition, this object can perform the functionality indicated in the class. We can instantiate as many objects as we want from a class.

To instantiate an object from the car class, we can do it in the following way:


Car myCar = new Car();

Where myCar is the identifier of the object. With this identifier is that we can use the object in the future.

We can use the object myCar using the dot operator. The idea is that after placing the identifier we can access the members of the Car class to which we have access:


myCar.ModelYear = 2018;

myCar.Accelerate();

In the previous code, what we did was to save the value 2018 in the ModelYear property, and then execute the accelerate function.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s