Object-Oriented Programming and Classes

In this tutorial we will see how to bring reality into our applications, for this we will use object-oriented programming, also known as OOP.

 

1 - What is object-oriented programming?

It is a paradigm that turns real-world objects into code entities called classes. This might seem complex, but it is actually not complicated at all.

Let's imagine a real-world object, such as a motorcycle

imagen moto

As we can see in the image, we have two entities. Piloto and Moto.

It will depend on business logic to determine whether the motorcycle contains the rider, or if the rider contains the motorcycle. For our example, the motorcycle will contain the rider.

When we convert the motorcycle into an object, we must think about its characteristics, like brand, model, number of wheels, engine capacity, etc. These characteristics will be our properties.

In addition, a motorcycle performs actions, such as starting, accelerating, braking, changing gears. All these actions will be the methods.

Therefore, a class is a specific type used to create objects.

As we recall from previous tutorials, we have primitive types like int, decimal, etc. Classes are just another type, but one customized how we want them.

 

2 - Creating a class

To create a class we should use the reserved word class followed by the name we want to use. Like all code blocks, we define where it starts and ends using curly braces { and }. Inside the code block, we introduce our properties and methods.

class Moto{    public decimal VelocidadMaxima { get; set; }    public int NumeroRuedas { get; set; }    public Motorista Piloto { get; set; }    public Acelerar(){        //Código aquí    }    public Arrancar(){        //Código aquí    }}

As we can also see, we use the reserved word public which is an access modifier, which we discuss in the course on access modifiers.

 

2.1 - Creating an instance of a class

An object is the in-memory representation of a class, and you can create as many objects of a class as you like by using the reserved word new.

Moto aprilia = new Moto();

We can then assign values to its properties as well as call its methods.  Of course, since Piloto exists within the Moto type, we can also assign values to it.

aprilia.VelocidadMaxima = 320;aprilia.NumeroRuedas = 2;aprilia.Acelerar();aprilia.Piloto.Nacionalidad = "ESP";

 

 

3 - Constructors

As we have seen, when we create an instance of a class using new Moto(), this statement actually calls the default constructor inside System.Object. But what if we don’t want to use the default constructor, since it will initialize all variables to null?

To do this, we can create a custom constructor, where we can manually assign values, as shown in the example. Now, when we run Moto aprilia = new Moto(); it will by default create a VelocidadMaxima of 320 and NumeroRuedas of 2.

class Moto{    public decimal VelocidadMaxima { get; set; }    public int NumeroRuedas { get; set; }    public Motorista Piloto { get; set; }    public string Marca { get; set; }    public string Modelo { get; set; }    public Moto(){        VelocidadMaxima = 320;        NumeroRuedas = 2;    }    public Acelerar(){        //Código aquí    }    public Arrancar(){        //Código aquí    }}

But what if we have multiple motorcycles?

In our example, since the values are assigned in the default constructor, all our motorcycles would be created with the same values. This means that in the following statements:

Moto aprilia = new Moto();Moto Ducati = new Moto();

Both motorcycles will have a VelocidadMaxima of 320 and a value of 2 in NumeroRuedas, so these values might not be correct.

To fix this, we add a constructor that accepts parameter values. Using Operator Overloading, we can create as many constructors as we need.

class Moto{    public decimal VelocidadMaxima { get; set; }    public int NumeroRuedas { get; set; }    public Motorista Piloto { get; set; }    public string Marca { get; set; }    public string Modelo { get; set; }    public Moto(decimal velocidadMaxima, int NumeroRuedas){        VelocidadMaxima = velocidadMaxima;        NumeroRuedas = NumeroRuedas;    }    public Moto(string marca, string modelo){        Marca = marca;        modelo = modelo;    }    public Moto(){        VelocidadMaxima = 320;        NumeroRuedas = 2;    }    public Acelerar(){        //Código aquí    }    public Arrancar(){        //Código aquí    }}

As we can see, both constructors work perfectly

Moto aprilia = new Moto("Aprilia", "RX");Moto motoSinMarca = new Moto(210,2);

 

 

4 - Inheritance in programming

Another very important feature of object-oriented programming is inheritance. This allows us to inherit information from a parent class to its child.

In the previous example of the motorcycle, let’s suppose that we also have a car. Both are vehicles, so they share common characteristics, such as brand, model, number of wheels, and others that can be different, such as engine capacity for motorcycles or traction for cars. Using Inheritance allows us to save many lines of code and complexity in our programs.

class Vehiculo{    public decimal VelocidadMaxima { get; set; }    public int NumeroRuedas { get; set; }    public string Marca { get; set; }    public string Modelo { get; set; }    public Vehiculo(decimal velocidadMaxima, int NumeroRuedas){        VelocidadMaxima = velocidadMaxima;        NumeroRuedas = NumeroRuedas;    }    public Vehiculo(string marca, string modelo){        Marca = marca;        Modelo = modelo    }    public Acelerar(){        //Código aquí    }    public Arrancar(){        //Código aquí    }}class Moto : Vehiculo{    public int Cilindrada { get; set; }    public Moto(decimal velocidadMaxima, int NumeroRuedas, int cilindrada) : base(velocidadMaxima, NumeroRuedas){       Cilindrada = cilindrada;    }    public Moto(string marca, string modelo, int cilindrada) : base(marca, modelo){        Cilindrada  = cilindrada;    }   public void HacerCaballito(){       //codigo   }}class Coche : Vehiculo {    public string Traccion { get; set; }    public Coche(string marca, string modelo, string Traccion) : base(marca, modelo){    }    public bool CerrarPuertas(){            }}

As we can see, both Coche and Moto inherit, using :, from the Vehiculo class. This means that from both classes, we can access the methods and properties of the Vehiculo class, in addition to their own.

Another interesting detail is that as we can see in the constructor, from the child class we call the parent class constructor using the reserved word : Base() and passing the parameters that the constructor requires.

This post was translated from Spanish. You can see the original one here.
If there is any problem you can add a comment bellow or contact me in the website's contact form

© copyright 2025 NetMentor | Todos los derechos reservados | RSS Feed

Buy me a coffee Invitame a un café