Polymorphism in Object-Oriented Programming

Welcome to this new post where we will continue discussing the fundamental pillars of object-oriented programming: polymorphism.

 

1- What is polymorphism in object-oriented programming

We call polymorphism the mechanism that allows us to have a method in a parent class, as we saw in inheritance (link), and override it in the child class.

This means that we will have the same method in both classes, but in the child class it will perform different actions. That’s why polymorphism is also known as method overriding.

There are two forms of polymorphism

  1. At runtime, which is related to interfaces (post available in a few weeks)
  2. The second one is called static polymorphism, which determines which method will be executed during compilation.

 

 

2 - Static polymorphism in C#

2.1 - Using the new keyword

To indicate that we want to override a method, we do it by using the reserved word new before the method name in the child class. This creates a new version of the method, only for that class.

By doing this, we allow “the same method” to perform different actions when called.

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;    }    public void Acelerar()    {        Console.WriteLine("Acelerar vehículo");        //Pisar el acelerador    }}class Moto : Vehiculo{    public int Cilindrada { get; set; }    public Moto(string marca, string modelo, int cilindrada) : base(marca, modelo)    {        Cilindrada = cilindrada;    }    public new void Acelerar()    {        //Girar el puño        Console.WriteLine("Acelerar Moto");    }}

As we can see, the Acelerar method from the Moto class includes the new keyword, which means that if we call Moto.Acelerar(), the new code will be executed. Meanwhile, if we have another class, such as Vehiculo.Acelerar(), or Coche.Acelerar() if we use examples from the previous post, the Vehiculo class method will run, since Coche doesn’t have a method that overrides its parent class’s method.

 

2.2- Using the keywords virtual and override

In the professional world, the approach with the new keyword is not very common, as it gives the impression that there will be no overriding, and it can be very confusing.

To avoid this, you can indicate in the parent method that it will be overridden by using the virtual keyword

public virtual void Acelerar(){    Console.WriteLine("Acelerar vehículo");    //Pisar el acelerador}

And in the child class method, you indicate with the override keyword that it will override the parent method.

It’s important to remember that you can’t use the override keyword if the parent method isn’t virtual.

public override void Acelerar(){    //Girar el puño    Console.WriteLine("Acelerar Moto");}

Here’s the complete example:

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;    }    public virtual void Acelerar()    {        Console.WriteLine("Acelerar vehículo");        //Pisar el acelerador    }}class Moto : Vehiculo{    public int Cilindrada { get; set; }    public Moto(string marca, string modelo, int cilindrada) : base(marca, modelo)    {        Cilindrada = cilindrada;    }    public override void Acelerar()    {        //Girar el puño        Console.WriteLine("Acelerar 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

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

Buy me a coffee Invitame a un café