I know it is lot of code. But I am trying to understand the concept of Factory pattern with interfaces and base class. I have a base class of Car that implements an interface which will be implemented by every derived class.

Then I have another interface that will be only implemented by few derived classes. OnlyImplementedByBMW.

Then I have derived classes that inherits from base class and also implements an interface. Now I want to use factory pattern to create instance of BMW.
CarFactoryPattern.cs works fine and returns the instance of Bmw object but it is only able to call functions in base class. How can I call BMWFunction1 from C1 object created from factory pattern?

How can I call BMWFunction1 from c1 object created from factory pattern? c1 object is Bmw type of object so shouldn't it has all the functions defined in class for BMW which includes the interface function of "onlyImplementedByBMW"

You have no idea which car you received. All you know is that it is a car. Maybe I returned a BMW, maybe I returned a Mercedes, maybe I returned a ReliantRobin.

It wouldn't make sense for you to call:

myCar.BmwMethod();

because you have no reasonable expectation to know that the car you received is a Bmw.

There are workarounds to this, but first I want to mention the most important thing: These workarounds are commonly abused. Nine times out of ten, you should revisit your intended logic because you got yourself into a bd situation by abusing/overusing polymorphism.

Nonetheless, there are ways to convert your Car into a BMW.

Hard casting

BMW myBMW = (BMW)myCar;

This will throw an exception if myCar is not a BMW. Don't use this unless you know for a fact that you are dealing with a BMW object.

Soft casting

BMW myBMW = myCar as BMW;

This will return null if myCar is not a BMW. While not as bad as hard casting, null reference exceptions are still bad and should be avoided, so don't use this unless you know for a fact that you are dealing with a BMW object.

It doesn't matter whether you hard/soft cast here as you've already confirmed that the cast will succeed.

However, I want to stress again that while this is syntactically valid, do not abuse it or use it as the goto solution. In pretty much every case, when you run into this issue this means you've misused polymorphism somewhere down the line (because you said you needed a Car but really were hoping to get a BMW, which means your code does not match your expectations).

So how should this be implemented? My Scenario is that I have one interface which is implemented by base class but other interfaces are only implemented by some classes. Then I want to use factory pattern? Do I create different factory classes for each car class? Like BmwFactory and MercedesFactory classes?
– Learn AspNetFeb 6 at 16:13

1

@LearnAspNet: Think about the factory methods you're writing. The one in your question is the equivalent of "give me a car", which is not the same as "give me a BMW". If you expect to be given a BMW, then the factory method you call should have the BMW return type. It makes no sense to ask for any car and then (unspoken) expect to be given a BMW. (As a sidenote: there's no inherent issue with returning a BMW or Mercedes type from a CarFactory - they are still derived types of car)
– FlaterFeb 6 at 16:14

@LearnAspNet that is in no way readable. I also can't confirm whether the factory does what you need it to. I don't know what your business requirements are and this isn't really the place to hash all that out.
– FlaterFeb 6 at 16:31

How can I make it readable in the comment section? Requirements are to use to factory pattern to create car objects without exposing the creation logic to the client?
– Learn AspNetFeb 6 at 16:35