C
C#2mo ago
Yarden

✅ 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
ero
ero2mo ago
can you show an example?
Yarden
YardenOP2mo ago
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(); //} }
}
ero
ero2mo ago
$code
MODiX
MODiX2mo ago
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/
Yarden
YardenOP2mo ago
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();
//}

}


}
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();
//}

}


}
This is the base class, look at the method "getRadioColor"
using Microsoft.VisualBasic;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NewClass
{
sealed class Car : Vehicle
{
private static int carsCreated;
private int id;
Radio radio = new Radio();//Has-a
public Car(string brand, string model, string color, int year, int numberOfWheels)
: base(brand, model, color, year, numberOfWheels)
{
id = carsCreated++;
}
public int getID()
{
return id;
}
public string getRadioColor()
{
return "string";
}
static Car()
{
carsCreated = 0;
}
public static void Main(string[] args)
{
//Car car0 = Car("Mitsubishi", "Space star", "Blue", 2018);
Car car1 = new Car("Mitsubishi", "Space star", "Blue", 2018, 5);
Car car2 = new Car("BMW", "X5 SUV", "White", 2022, 4);
Car car3 = new Car("Alfa Romeo", "Giulietta", "Red", 2024, 4);
Car car4 = new Car("Suzuki", "Baleno", "White", 1996, 4);

car1.setRadioColor("Red");

Car[] cars = new Car[] { car1, car2, car3, car4 };

//Iteration over the items of cars:
foreach (Car item in cars)
{
Console.WriteLine("Brand: {0}, Model: {1}, Color: {2}, Year: {3}, ID: {4}", item.getBrand(), item.getModel(), item.Color, item.Year, item.getID());
Console.WriteLine("car radio's color: {0}", item.getRadioColor());
}

Console.WriteLine(car1.getRadioColor());
Console.ReadLine();
}
}
}
using Microsoft.VisualBasic;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NewClass
{
sealed class Car : Vehicle
{
private static int carsCreated;
private int id;
Radio radio = new Radio();//Has-a
public Car(string brand, string model, string color, int year, int numberOfWheels)
: base(brand, model, color, year, numberOfWheels)
{
id = carsCreated++;
}
public int getID()
{
return id;
}
public string getRadioColor()
{
return "string";
}
static Car()
{
carsCreated = 0;
}
public static void Main(string[] args)
{
//Car car0 = Car("Mitsubishi", "Space star", "Blue", 2018);
Car car1 = new Car("Mitsubishi", "Space star", "Blue", 2018, 5);
Car car2 = new Car("BMW", "X5 SUV", "White", 2022, 4);
Car car3 = new Car("Alfa Romeo", "Giulietta", "Red", 2024, 4);
Car car4 = new Car("Suzuki", "Baleno", "White", 1996, 4);

car1.setRadioColor("Red");

Car[] cars = new Car[] { car1, car2, car3, car4 };

//Iteration over the items of cars:
foreach (Car item in cars)
{
Console.WriteLine("Brand: {0}, Model: {1}, Color: {2}, Year: {3}, ID: {4}", item.getBrand(), item.getModel(), item.Color, item.Year, item.getID());
Console.WriteLine("car radio's color: {0}", item.getRadioColor());
}

Console.WriteLine(car1.getRadioColor());
Console.ReadLine();
}
}
}
This is the son's class, I override this method and used it
ero
ero2mo ago
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 intended
Yarden
YardenOP2mo ago
Isn'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
ero
ero2mo ago
that's not how it works in c#, no
MODiX
MODiX2mo ago
ero
REPL Result: Success
C1 c2 = new C2();
C1 c3 = new C3();

c2.M();
c3.M();

class C1
{
public virtual void M() => Console.WriteLine("C1.M");
}

class C2 : C1
{
public override void M() => Console.WriteLine("C2.M");
}

class C3 : C1
{
public new void M() => Console.WriteLine("C3.M");
}
C1 c2 = new C2();
C1 c3 = new C3();

c2.M();
c3.M();

class C1
{
public virtual void M() => Console.WriteLine("C1.M");
}

class C2 : C1
{
public override void M() => Console.WriteLine("C2.M");
}

class C3 : C1
{
public new void M() => Console.WriteLine("C3.M");
}
Console Output
C2.M
C1.M
C2.M
C1.M
Compile: 517.691ms | Execution: 33.886ms | React with ❌ to remove this embed.
ero
ero2mo ago
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
Vehicle car = new Car(...);
Console.WriteLine(car.getRadioColor());
Vehicle car = new Car(...);
Console.WriteLine(car.getRadioColor());
it would print the return value of radio.getColor()
Yarden
YardenOP2mo ago
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
ero
ero2mo ago
it only matters if you cast your child class to the parent class
Yarden
YardenOP2mo ago
Ohhh okie!
ero
ero2mo ago
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 possible
Yarden
YardenOP2mo ago
Thank you so so so so so much ❤️ Was very helpful
Want results from more Discord servers?
Add your server