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 have classes that act as children, and others that act as parents.
Child classes can use 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, it would look like this:
As we can see, both Car and Motorcycle point to Vehicle, which means they can use both its properties and its 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 must use the colon :
as seen in the example.
When inheriting 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 class Mixto
inherits from Vehiculo
, then we can do the following:
class Vehiculo{ //Code}class Mixto : Vehiculo{ //Code}class Triciclo : Mixto{ //Code}
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 shown in the example.
class Vehiculo{ public int NumeroRuedas { get; set; }}class Mixto : Vehiculo{ //Code}class Triciclo : Mixto{ public void AsignarRuedas() { NumeroRuedas = 3; }}
2 - Constructor Declaration
When we create a constructor, we declare a method with no return type that has the same name as the class.
.Net generates by default, through the object
class, a default constructor, which is an empty constructor with no arguments. But, what happens if we want to have a custom one, and the parent class also has a constructor?
For this, we must follow the hierarchy that both constructors are going to execute. To do this, from the child class's constructor, we must specify which parameters we are going to pass to the parent class.
The parameters that we want to send from the child class to the parent class, we will indicate them 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 includes, in addition to the required parameter for that class's property, two more 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. It also inherits its type.
If we run the following code:
Vehiculo vehiculo = (Vehiculo)new Moto("ducati", "A1", 1000);
We see that it runs without any issue, but since we have cast the Moto
class to Vehiculo
, we will not be able to access the properties specific to Moto
.
If there is any problem you can add a comment bellow or contact me in the website's contact form