Index
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:
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
.
If there is any problem you can add a comment bellow or contact me in the website's contact form