Inheritance in Object-Oriented Programming

 

1 - Inheritance in OOP

Inheritance is a concept in object-oriented programming. It is a mechanism that allows one class to be derived from another class.

In other words, we will have classes that are children and other classes that are parents.

Child classes can use both their own methods and properties as well as those of the parent class, as long as their access modifier allows it. 

For example, the following code

class Vehiculo
{
    public decimal VelocidadMaxima { get; set; }
    public int NumeroRuedas { get; set; }
    public string Marca { get; set; }
    public string Modelo { get; set; }

}

class Moto : Vehiculo
{
    public int Cilindrada { get; set; }
}

class Coche : Vehiculo 
{
    public string Traccion { get; set; }
}

Translated to an image would look like this:

object-oriented programming inheritance example

 

As we can see, both Car and Motorcycle point to Vehicle, which means they can use its properties and methods. So the following code Moto.VelocidadMaxima or Moto.Cilindrada is just as valid as Coche.VelocidadMaxima or Coche.Traccion.

To indicate that a class derives from another, we use a colon : as seen in the example.

When we inherit from a parent class, we can only do so from one class. We cannot inherit from two classes, for example class Triciclo : Mixto, Vehiculo is not valid.

But if in this example, the Mixto class inherits from Vehiculo, we can do the following:

class Vehiculo{
    //Código
}

class Mixto : Vehiculo{
    //Código
}

class Triciclo : Mixto{
    //Codigo
}

As we can see Mixto inherits from Vehiculo, and Triciclo inherits from Mixto so from triciclo we will have access to the properties of the Vehiculo class as seen in the example.

class Vehiculo
{
    public int NumeroRuedas { get; set; }
}

class Mixto : Vehiculo
{
    //Código
}

class Triciclo : Mixto
{
    public void AsignarRuedas()
    {
        NumeroRuedas = 3;
    }
}

 

2 - Constructor Declaration

When we create a constructor, we declare a method with no return type and with the same name as the class.

.Net automatically generates a default constructor, which is an empty constructor with no arguments, through the object class. But what if we want to have a custom constructor and the parent class also has a constructor?

To do this, we must follow the hierarchy in which both constructors will be executed. This means that from the child class constructor, we must indicate which parameters will be passed to the parent class.

The parameters that we send from the child class to the parent class are specified using  :base(parameters) 

class Vehiculo
{
    public decimal VelocidadMaxima { get; set; }
    public int NumeroRuedas { get; set; }
    public string Marca { get; set; }
    public string Modelo { get; set; }

    public Vehiculo(string marca, string modelo){
        Marca = marca;
        Modelo = modelo
    }
}

class Moto : Vehiculo
{
    public int Cilindrada { get; set; }

    public Moto(string marca, string modelo, int cilindrada) : base(marca, modelo){
        Cilindrada  = cilindrada;
    }
}

As we can see in the example, the parent class Vehiculo has a constructor with two parameters. So in the child class Moto, we have created a constructor that, in addition to the parameter needed for its own property, includes two more parameters that are sent to the parent class using :base()

 

 

3 - A Child Class is Also a Type of the Parent Class

A child class not only inherits the methods and properties of the parent class, but also inherits the type of the parent class.

If we execute the following code:

Vehiculo vehiculo = (Vehiculo)new Moto("ducati", "A1", 1000);

We can see that it works without any issue. However, since we have casted the Moto class to the Vehiculo class, we will not be able to access the properties of moto.

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

Uso del bloqueador de anuncios adblock

Hola!

Primero de todo bienvenido a la web de NetMentor donde podrás aprender programación en C# y .NET desde un nivel de principiante hasta más avanzado.


Yo entiendo que utilices un bloqueador de anuncios como AdBlock, Ublock o el propio navegador Brave. Pero te tengo que pedir por favor que desactives el bloqueador para esta web.


Intento personalmente no poner mucha publicidad, la justa para pagar el servidor y por supuesto que no sea intrusiva; Si pese a ello piensas que es intrusiva siempre me puedes escribir por privado o por Twitter a @NetMentorTW.


Si ya lo has desactivado, por favor recarga la página.


Un saludo y muchas gracias por tu colaboración

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

Buy me a coffee Invitame a un café