biswajitdash:
What you are telling in essence means that "Prgram to the concrete implementation and not the abstract/interface".
The actual principle is - "Always program to an abstract/interface and not an implementation." Thats how polymorphiism is used. Otherwise why does the concept of interface exist?
I am not sure where the disconnect here is.
You can't always program to an abstract, unless you are willing to assign every method to an interface. What if Employee inherits from Person? Employee has a "givePayRaise" method. How are you going to "program to the abstract" and be able to execute that method? You can't, unless you go nuts and make every class implement an interface for no reason.
Polymorphism is used by allowing you to treate CONCRETE (or even abstract types, so long as they extend from another class) implementations as more simple GENERIC objects / interfaces.
This is exactly what my code example did! The BaseClass interface is used when you cast a DerivedClass as BaseClass. This is one of the basic principles of OO. This is polymorphism.
I really don't know where you see a problem with this. You can never call "givePayRaise" off a Person object unless that Person is really an Employee. The Person interface doesn't have "givePayRaise". It is this that you were trying to do in your first code example.
"Prgram to the concrete implementation and not the abstract/interface". Unless you NEED to program against the concrete implementation as I showed above (givePayRaise), then you HAVE to program against the concrete implementation. My code example shows you can program to the abstract/interface if you want and it works just fine.
"Always program to an abstract/interface and not an implementation." Thats how polymorphiism is used. Otherwise why does the concept of interface exist? Any code you write against the BaseClass will work just fine if you use DerivedClass. THAT is how polymorphism works. If you are suggesting that the concept of interfaces exists so that you never have to work with a concrete class, then I would disagree with your way of programming. Implementing interfaces allows a class to be used polymorphically in multiple cases, are are very important in languages like C#/Vb.Net/Java, that don't support multiple inheritance.
If you want to take the position (I would strongly disagree with it) that you need to have every class implement an interface so you can ALWAYS program against an interface or abstract base class, then I would first say that I disagree with that philosophy, and second that you can STILL do that just fine with my code example: you can still do that type of design using shadows.