biswajitdash:
You don't lose any polymorphism doing this. That is why the method shadows the inherited classes methods.
What TAsunder asked is correct. Look at the following code block to explain the polymorphic scenario better. The VB.NET way of shadowing really does not satisfy any polymorphism. Assume that the classes are written in VB.NET library and referred from a C# client.
' VB.NET Class Library
Public Class BaseClass
Public Function Add(ByVal i As Integer,
ByVal j As Integer) As Integer
Return i + j
End Function
End Class
Public Class DerivedClass
Inherits BaseClass
Public Shadows Function Add(ByVal i As Integer,
ByVal j As Integer,
ByVal k As Integer) As Integer
Return MyBase.Add(i, j) + k
End Function
End Class
// C# Class Library
public class ClientClass
{
public static void Main()
{
BaseClass b = new BaseClass();
DerivedClass d = new DerivedClass();
BaseClass objRef = null;
// Polymorphic call here. Will work fine.
objRef = b;
objRef.Add(1, 2); // Will work.
// Polymorphic call here. Will fail. Because
// type BaseClass does not have any Add()
// method that takes 3 arguments.
objRef = _d;
objRef.Add(1, 2, 3); // Will fail.
}
}
So, a polymorphic call in run-time will fail. Also following shadowing technique in real-life violates LSP at the best. Any design using this must be avoided.
Sorry but your code example is backwards in terms of understanding how polymorphism and LSP works.
Polymorphism works by allowing you to say that a DerivedClass can be used as a BaseClass. But you are trying to use a BaseClass as a DerivedClass. Polymorphism doesn't work in that direction.
Your line of code:
objRef.Add(1, 2, 3); // Will fail.
Of course it will fail. It won't even compile. objRef is a BaseClass. BaseClass doesn't have a method "Add" that takes 3 parameters.
What if DerivedClass defines a method "Subtract". objRef won't have that either. It's the same thing. Base Classes don't know about the classes that inherit from them.
But if you case objRef as a DerivedClass, then you can call Add with 2 or 3 parameters, just as you would expect to.
If what you coded worked (objRef.Add(1,2,3)) then THAT is what would break LSP.
But LSP is not violated. I modified your code a little to show that LSP is not violated, and polymorphism remains fully functional.
See my code example to clear this up:
' VB.NET Class Library
Public Class BaseClass
Public Function Add(ByVal i As Integer, _
ByVal j As Integer) As Integer
Return i + j
End Function
Public Function LSP() As String
Return "I am LSP compliant"
End Function
End Class
Public Class DerivedClass
Inherits BaseClass
Public Shadows Function Add(ByVal i As Integer, _
ByVal j As Integer, _
ByVal k As Integer) As Integer
Return MyBase.Add(i, j) + k
End Function
Public Shadows Function LSP() As String
Return "I am NOT LSP compliant, bad programmer"
End Function
End Class
static void Main(string[] args)
{
BaseClass b = new BaseClass();
DerivedClass d = new DerivedClass();
BaseClass objRef = null;
// Polymorphic call here. Will work fine.
objRef = b;
objRef.Add(1, 2); // Will work.
// Polymorphic call here. Will fail. Because
// type BaseClass does not have any Add()
// method that takes 3 arguments.
objRef = d;
//objRef.Add(1, 2, 3); // Will fail.
/**************
* My Comments: of course this will fail...
* It will fail at compile time. objRef is a BaseClass.
* BaseClass doesn't have Add(i,i,i) */
// this will work just fine.
((DerivedClass)objRef).Add(1, 2, 3);
// lets make sure LSP is not violated
Console.WriteLine(testLsp(b));
Console.WriteLine(testLsp(d));
Console.WriteLine(testLsp(objRef));
Console.WriteLine(testLsp((DerivedClass)objRef));
Console.WriteLine("All passed");
Console.Read();
}
static string testLsp(BaseClass b)
{
return b.LSP();
}