Hej, mam pewien problem ze zrozumieniem polimorfizmu.
Otóż dajmy na to mamy kod (wzięty z książki):
using System;
class Car {
public string CarOwner;
public string PaintColor;
public int MilesPerGallon;
public Car(string newOwner, string paintColor, int mpg) {
CarOwner = newOwner;
PaintColor = paintColor;
MilesPerGallon = mpg;
}
public virtual int CalculateFuelForTrip(int tripDistance) {
return tripDistance / MilesPerGallon;
}
}
class VolvoCar : Car {
public string VolvoSoundSystem;
public VolvoCar(string owner, string paint, int mpg, string sound)
: base(owner, paint, mpg) {
// set the value of the VolvoSoundSystem
VolvoSoundSystem = sound;
}
public override int CalculateFuelForTrip(int tripDistance) {
System.Console.WriteLine("Derived Method Called");
return base.CalculateFuelForTrip(tripDistance);
}
}
class Listing_18 {
static void Main(string[] args) {
// create an instance of VolvoCar
Car myVolvo = new VolvoCar("Adam Freeman", "Black", 30, "Premium");
// upcast from VolvoCar to Car
int paliwo = myVolvo.CalculateFuelForTrip(500);
// wait for input before exiting
Console.WriteLine("Press enter to finish");
Console.ReadLine();
}
}
Jest tutaj klasa podstawowa, dziedziczona (dobrze to nazwałem?), która nadpisuje metodę.
Wszystko rozumiem oprócz
// upcast from VolvoCar to Car
Z czym wiąże się w/w upcast? Rozumiem, że to dalej używa nadpisanej metody z dziedziczonej klasy, ale czy wiąże się to jeszcze z czymś?