-1
Good afternoon,
I’d like to know the difference between override
and :base
in C#
Is there any difference or has the same function?
-1
Good afternoon,
I’d like to know the difference between override
and :base
in C#
Is there any difference or has the same function?
3
As the friend Carltee answered. The modifier override
serves to modify or extend a function. With this you may or may not change parent class behaviors (or base class).
To use the override
in the child class you need to indicate that the function can be modified in the parent class. c# uses the modifier virtual
:
public class TestBase
{
public virtual void Hello() {
Console.WriteLine("Oi pai");
}
}
Thus, the compiler understands that that function can be modified in the child class:
public class TestSon : TestBase
{
public override void Hello() {
//Exibe oi filho no console em vez de oi pai da classe base
Console.WriteLine("oi filho");
}
}
In this example above it replaces the function of the parent class by calling the function by the child. Instead of displaying "hi dad" it displays "hi son.
But if we still want to perform the function of the parent class, we use the modifier base
which is like the modifier super
in some languages. With this we can access the methods and properties of the parent class in the daughter class:
public class TestFatherSon : TestBase
{
public override void Hello() {
//Exibe oi pai e oi filho no console, já que executa a função da classe pai
base.Hello();
Console.WriteLine("oi filho");
}
}
You can see how the examples work in practice by clicking on the link below. It is an online tool that interprets and compiles code in c#:
https://dotnetfiddle.net/yPBq9j
This basic feature can also be used in constructor functions. It works the same way. If for some reason you need to call the parent class builder you use :base()
after declaring the constructor function in the child class:
public class BaseClass
{
int num;
public BaseClass()
{
Console.WriteLine("in BaseClass()");
}
public BaseClass(int i)
{
num = i;
Console.WriteLine("in BaseClass(int i)");
}
public int GetNum()
{
return num;
}
}
public class DerivedClass : BaseClass
{
// This constructor will call BaseClass.BaseClass()
public DerivedClass() : base()
{
}
// This constructor will call BaseClass.BaseClass(int i)
public DerivedClass(int i) : base(i)
{
}
static void Main()
{
DerivedClass md = new DerivedClass();
DerivedClass md1 = new DerivedClass(1);
}
}
Follow the official microsoft documentation to which I took the example above: https://docs.microsoft.com/pt-br/dotnet/csharp/language-reference/keywords/base
0
With the override, you can modify or extend the implementation of the inherited method or property by adding a different logic than the one in the class in which it was inherited.
Already the groundwork is simply used to access this method or property of the base class, and cannot modify its behavior.
Browser other questions tagged c#
You are not signed in. Login or sign up in order to post.
Use code examples to facilitate understanding of your explanation ;)
– Pedro Paulo