✅ override and virtual
The book I'm reading about C# and the internet say that "you can't override a method of the base class without the virtual keyword" but I don't write virtual keyword and yet I can..
So I'm a bit confused..
15 Replies
can you show an example?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace NewClass
{
public class Vehicle
{
private string brand;
private string model;
private string color;
private int year;
private int numOfWheels;
private Radio radio = new Radio();//Has-a
protected Vehicle(string brand, string model, string color, int year, int numberOfWheels)
{
this.brand = brand;
this.model = model;
this.color = color;
this.year = year;
this.numOfWheels = numberOfWheels;
}
public int NumberOfWheels
{
get { return numOfWheels; }
set { numOfWheels = value; }
}
protected string getBrand()
{
return brand;
}
protected void setBrand(string brand) => this.brand = brand;
protected string getModel()
{
return model;
}
protected void setModel(string model) => this.model = model;
public string Color
{
get { return this.color; }
set { this.color = value; }
}
public int Year
{
get { return year; }
set
{
this.year = value;
}
//get => year;
//set => year = value;
}
public void setRadioColor(string color) { radio.setColor(color); } public string getRadioColor() { return radio.getColor(); }
//public static void Main(string[] args) //{ // Vehicle vehicle = new Vehicle("1", "2", "3", 1, 2); // vehicle.numOfWheels = 10; // Console.WriteLine(vehicle.numOfWheels); // Console.ReadLine(); //} }
}
public void setRadioColor(string color) { radio.setColor(color); } public string getRadioColor() { return radio.getColor(); }
//public static void Main(string[] args) //{ // Vehicle vehicle = new Vehicle("1", "2", "3", 1, 2); // vehicle.numOfWheels = 10; // Console.WriteLine(vehicle.numOfWheels); // Console.ReadLine(); //} }
}
$code
To post C# code type the following:
```cs
// code here
```
Get an example by typing
$codegif
in chat
For longer snippets, use: https://paste.mod.gg/
This is the base class, look at the method "getRadioColor"
This is the son's class, I override this method and used it
you're not overriding a method
you would be using the
override
keyword
you're not. in this example, you're shadowing the method
the compiler would suggest you use the new
keyword if hiding the base method was intendedIsn't overriding means to use the same name but change the logic in the son's class?
That's exactly what was told in Java
that's not how it works in c#, no
ero
REPL Result: Success
Console Output
Compile: 517.691ms | Execution: 33.886ms | React with ❌ to remove this embed.
in this example,
C2
properly overrides C1.M
. thus, c2.M()
prints C2.M
.
C3.M
instead shadows C1.M
. thus, c3.M()
prints C1.M
and not C3.M
.
translated to your example, if you did
it would print the return value of radio.getColor()
But what is the difference?
When I didn't use override the same output was printed.
You named it "overshadowing", but what exactly the difference? it seems like a similar effect
it only matters if you cast your child class to the parent class
Ohhh okie!
you can do
Car car = new Car(...);
and use car.getRadioColor()
, which would return string
as expected.
if you do Vehicle car = new Car(...);
and use car.getRadioColor()
, it will call Vehicle
's implementation, which will instead return the return value of radio.getColor()
it's very much recommended to avoid shadowing wherever possibleThank you so so so so so much ❤️
Was very helpful