Search code examples
c#ooppolymorphism

Are upcasting and downcasting polymorphisms?


In the example below, I have marked an object with different types by performing both upcasting and downcasting. Essentially, I’ve used these casting techniques to express the object in multiple ways depending on its type at runtime. My question is whether this scenario can be considered an example of polymorphism in object-oriented programming.

To clarify, I understand that polymorphism allows objects to be treated as instances of their parent class, providing flexibility and enabling method overriding. However, I am unsure if the use of upcasting and downcasting also falls under the definition of polymorphism or if it is something different.

Specifically, does polymorphism only apply to virtual methods and overloaded methods that exhibit method behavior changes at runtime? Or can it also be interpreted as casting objects to different types (upcasting and downcasting) and then treating them accordingly?

namespace Inheritance
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Vehicle supersport = new SupersportCar("BMW", "m5", 1730, 3_000_000, 507, 520, 4.7);
            SupersportCar ssc = (SupersportCar)new Vehicle("BMW", "m5", 1730, 3_000_000);
        }
    }

    class Vehicle
    {
        public string Brand { get; set; }
        public string Model { get; set; }
        public int Weight { get; set; }
        public double Price { get; set; }

        public Vehicle(string Brand, string Model, int Weight, double Price)
        {
            this.Brand = Brand;
            this.Model = Model;
            this.Weight = Weight;
            this.Price = Price;

            Console.WriteLine("YENİ ARAÇ");
            Console.WriteLine("Vehicle Constructer");
            Console.WriteLine($"""
            Araç Markası: {Brand}
            Araç modeli: {Model}
            Araç ağırlığı: {Weight}
            Araç fiyatı: {Price}
            """);
            Console.WriteLine("*********\n");
        }

        public static void StaticArac()
        {

        }
    }

    class Car : Vehicle
    {
        public double Power { get; set; }
        public double Torque { get; set; }

        public Car(string Brand, string Model, int Weight, double Price, double Power, double Torque) : base(Brand,Model,Weight,Price)
        {
            this.Power = Power;
            this.Torque = Torque;

            Console.WriteLine("YENİ ARAÇ");
            Console.WriteLine("Car Constructer");
            Console.WriteLine($"""
            Araç Markası: {Brand}
            Araç modeli: {Model}
            Araç ağırlığı: {Weight}
            Araç fiyatı: {Price}
            Araç gücü: {Power} hp
            Araç torku: {Torque} NM
            """);
            Console.WriteLine("*********\n");
        }
    }

    class SupersportCar : Car
    {
        public double FromZeroToHundered { get; set; }

        public SupersportCar(string Brand, string Model, int Weight, double Price, double Power, double Torque, double FromZeroToHundered) : base(Brand, Model, Weight, Price, Power, Torque)
        {
            this.FromZeroToHundered = FromZeroToHundered;

            Console.WriteLine("YENİ ARAÇ");
            Console.WriteLine("SupersportCar Constructer");
            Console.WriteLine($"""
            Araç Markası: {Brand}
            Araç modeli: {Model}
            Araç ağırlığı: {Weight}
            Araç fiyatı: {Price}
            Araç gücü: {Power} hp
            Araç torku: {Torque} NM
            0-100 hızlanması: {FromZeroToHundered} sn
            """);
            Console.WriteLine("*********");
        }
    }

}

Solution

  • In your question we are talking about a subtyping polymorphism (there are other forms of polymorphism).

    Subtyping polymorphism is a form of polymorphism in which an object of a child class can be used anywhere an object of a parent class (interface) is expected, without changing the code that operates on that object.

    Neither upcasting nor downcasting are polymorphisms in themselves. But upcasting is necessary for it to work. It allows objects of child classes to be used through the parent class interface, which makes dynamic (runtime) polymorphism possible.


    P.S. Downcasting is considered a bad practice in most cases.