Passing Derived class as a parameter to a method when the parameter type is base class

Solution 1:

Put really simply:

A Derived class (or subclass) is an instance of its base class.

So, when you pass an instance of ConcreteStrategyAdd into the constructor, you are essentially passing a Strategy object in.

There is no casting involved. The type hierarchy allows for this type of programming. It allows programmers to use polymorphism in their code.

Solution 2:

There is no casting needed, since ConcreteStrategyAdd is a Strategy - it satisfies all the requirements of being a Strategy. This is the principle of Polymorphism.

Perhaps a more simplistic example is needed:

abstract class Fruit { }

class Apple : Fruit { }
class Orange : Fruit { }
class Melon : Fruit { }

class FruitBasket
{
    void Add(Fruit item) { ... }
}

FruitBasket basket = new FruitBasket();
basket.Add(new Apple()); // Apple IS A fruit
basket.Add(new Orange()); // Orange IS A fruit
basket.Add(new Melon()); // Melon IS A fruit

class Potato : Vegetable { }

basket.Add(new Potato()); // ERROR! Potato IS NOT A fruit.