By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,692 Members | 1,144 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,692 IT Pros & Developers. It's quick & easy.

Multiple polymorphism...How to?

P: n/a
Hi,

Does anybody know how Multiple polymorphism can be done in VB.NET or DOT
doesn't support it? Is there any third party extensions available like for
Java to do this?

Regards,
....Ashok
------------------------------------------------------------------------
"It is beautiful for an engineer to shape and design the same way
that an artist shapes and designs. But whether the whole process
makes any sense, whether men become happier - that I can no
longer decide." - Rudolph Diesel
Nov 21 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a

"G.Ashok" <gw******@hotmail.com> wrote in message
news:eo**************@TK2MSFTNGP14.phx.gbl...
Hi,

Does anybody know how Multiple polymorphism can be done in VB.NET or DOT
doesn't support it? Is there any third party extensions available like for
Java to do this?

Regards,
...Ashok


Can you describe what you want a whole lot better?
Polymorphism in itself can mean different things depending upon context.
If are are talking about simply implementing multiple interfaces on a single
object, then this is no problem.
Nov 21 '05 #2

P: n/a
"G.Ashok" <gw******@hotmail.com> schrieb:
Does anybody know how Multiple polymorphism can be done in VB.NET or DOT
doesn't support it? Is there any third party extensions available like for
Java to do this?


Are you referring to multiple inheritance? No, .NET doesn't support MI for
good reasons.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #3

P: n/a
G.

You can go as deep as you want to make from a cell an homo sapiens sapiens.

Or any other example.

I hope this helps,

Cor
Nov 21 '05 #4

P: n/a
Ashok,
As the others ask: What specifically do you mean by "Multiple polymorphism"?

Do you mean "Multiple Dispatch" (aka double dispatch) polymorphism?
Do you mean "Multiple Inheritance" as Herfried suggests? (PersonForm :
Inherits Form AndAlso Person)
Do you mean "Multiple Layer" inheritance as Cor Suggests? (MainForm :
inherits Form : ... inherits Control : ... Inherits Object)
Do you mean something else?
The "easiest" way to implement Double Dispatch in .NET is to use the Visitor
pattern.

Something like:

Public MustInherit Class C

Public Sub F(ByVal aC As C)
aC.Fc(Me)
End Sub

Public MustOverride Sub Fc(ByVal aC As C)

' NOTE we use qualified names rather then overloading here
' we could have just as easily call these all Fc...
Public MustOverride Sub Fc1(ByVal aC1 As C1)
Public MustOverride Sub Fc2(ByVal aC2 As C2)
Public MustOverride Sub Fc3(ByVal aC3 As C3)
Public MustOverride Sub Fc4(ByVal aC4 As C4)

End Class

Public Class C1
Inherits C

Public Overrides Sub Fc(ByVal aC As C)
aC.Fc1(Me)
End Sub

Public Overrides Sub Fc1(ByVal aC1 As C1)
Debug.WriteLine("Fc1(c1)", "c1")
End Sub

Public Overrides Sub Fc2(ByVal aC2 As C2)
Debug.WriteLine("Fc2(c2)", "c1")
End Sub

Public Overrides Sub Fc3(ByVal aC3 As C3)
Debug.WriteLine("Fc3(c3)", "c1")
End Sub

Public Overrides Sub Fc4(ByVal aC4 As C4)
Debug.WriteLine("Fc4(c4)", "c1")
End Sub

End Class

Public Class C2
Inherits C

Public Overrides Sub Fc(ByVal aC As C)
aC.Fc2(Me)
End Sub

Public Overrides Sub Fc1(ByVal aC1 As C1)
Debug.WriteLine("Fc1(c1)", "c2")
End Sub

Public Overrides Sub Fc2(ByVal aC2 As C2)
Debug.WriteLine("Fc2(c2)", "c2")
End Sub

Public Overrides Sub Fc3(ByVal aC3 As C3)
Debug.WriteLine("Fc3(c3)", "c2")
End Sub

Public Overrides Sub Fc4(ByVal aC4 As C4)
Debug.WriteLine("Fc4(c4)", "c2")
End Sub

End Class

Public Class C3
Inherits C

Public Overrides Sub Fc(ByVal aC As C)
aC.Fc3(Me)
End Sub

Public Overrides Sub Fc1(ByVal aC1 As C1)
Debug.WriteLine("Fc1(c1)", "c3")
End Sub

Public Overrides Sub Fc2(ByVal aC2 As C2)
Debug.WriteLine("Fc2(c2)", "c3")
End Sub

Public Overrides Sub Fc3(ByVal aC3 As C3)
Debug.WriteLine("Fc3(c3)", "c3")
End Sub

Public Overrides Sub Fc4(ByVal aC4 As C4)
Debug.WriteLine("Fc4(c4)", "c3")
End Sub

End Class

Public Class C4
Inherits C

Public Overrides Sub Fc(ByVal aC As C)
aC.Fc4(Me)
End Sub

Public Overrides Sub Fc1(ByVal aC1 As C1)
Debug.WriteLine("Fc1(c1)", "c4")
End Sub

Public Overrides Sub Fc2(ByVal aC2 As C2)
Debug.WriteLine("Fc2(c2)", "c4")
End Sub

Public Overrides Sub Fc3(ByVal aC3 As C3)
Debug.WriteLine("Fc3(c3)", "c4")
End Sub

Public Overrides Sub Fc4(ByVal aC4 As C4)
Debug.WriteLine("Fc4(c4)", "c4")
End Sub

End Class

Public Class Test

Public Shared Sub Main()
Dim xlist() As C = {New C1, New C2, New C3, New C4}
Dim ylist() As C = {New C1, New C2, New C3, New C4}

For Each x As C In xlist
For Each y As C In ylist
Debug.WriteLine(x, "x")
Debug.WriteLine(y, "y")
x.F(y)
Debug.WriteLine(Nothing)
Next
Next
End Sub

End Class
The C.F & C.Fc methods are performing the double dispatch. C.F is
dispatching to C.Fc, which is then dispatching back to one of C.Fc1, C.Fc2,
C.Fc3, C.Fc4...

I find using unique names for C.Fc1, C.Fc2, C.Fc3, C.Fc4 makes the code
easier to read, normally the "function" followed by the type name...
Hope this helps
Jay

"G.Ashok" <gw******@hotmail.com> wrote in message
news:eo**************@TK2MSFTNGP14.phx.gbl...
| Hi,
|
| Does anybody know how Multiple polymorphism can be done in VB.NET or DOT
| doesn't support it? Is there any third party extensions available like for
| Java to do this?
|
| Regards,
| ...Ashok
| ------------------------------------------------------------------------
| "It is beautiful for an engineer to shape and design the same way
| that an artist shapes and designs. But whether the whole process
| makes any sense, whether men become happier - that I can no
| longer decide." - Rudolph Diesel
|
|
Nov 21 '05 #5

P: n/a

Hi, Thank you all for the response in anyway,

Here the story...

Module Module1
Sub Main()

Dim am As New AccountManager

am.handleTransfer(New BankAccount)

End Sub

End Module

Public Interface IBankAccount

Sub deposit(ByVal currency As Currency)

'Sub deposit(ByVal currency As EuroCurrency)

End Interface

Public Class Currency

' // These Currency classes could as easily be interfaces, with no impact on
the examples.

' // members

'}

End Class

Public Class EuroCurrency : Inherits Currency

'// members

End Class

Public Class BankAccount : Implements IBankAccount

Public Sub deposit(ByVal currency As Currency) Implements
IBankAccount.deposit

'// handle standard deposit

End Sub

Public Sub deposit(ByVal currency As EuroCurrency) ' Implements
IBankAccount.deposit

'// handle Euro Currencies

End Sub

End Class

Public Class AccountManager

Public Sub handleTransfer(ByVal account As IBankAccount)

Dim currency As Currency = New EuroCurrency '// getCurrency()

'//

'// Make a deposit. In standard Java and *.NET languages,

'// this will always invoke BankAccount's first deposit method.

'// With Multiple Polymorphism, this will invoke the second deposit
method for

'// EuroCurrency (and its derived classes), otherwise the first
deposit method.

account.deposit(currency)

End Sub

End Class

Another workaround approach to the above problem is to create another
interface method, defining the following:

Public Interface IBankAccount
Sub deposit(currency As Currency)
Sub deposit(currency AS EuroCurrency)
End Inteface

But this would force all implementations to define a method for
the second case, even if none were desired. This will further forces to use
several huge amount of switch (Select Case/If TypeOf ) statements

With multiple polymorphism, if the currency returned by getCurrency is not a
EuroCurrency (inheritance is-a), the first deposit method defined in
BankAcount will be called. If the currency is a EuroCurrency, the second
(more specialized) deposit method will be called.

Double dispatch will not eliminate the switch statements.

I hope this helps to undertsand the problem in detail :o)

Regards,
....Ashok
------------------------------------------------------------------------
"It is beautiful for an engineer to shape and design the same way
that an artist shapes and designs. But whether the whole process
makes any sense, whether men become happier - that I can no
longer decide." - Rudolph Diesel
"G.Ashok" <gw******@hotmail.com> wrote in message
news:eo**************@TK2MSFTNGP14.phx.gbl...

| Hi,
|
| Does anybody know how Multiple polymorphism can be done in VB.NET or DOT
| doesn't support it? Is there any third party extensions available like for
| Java to do this?
|
| Regards,
| ...Ashok
| ------------------------------------------------------------------------
| "It is beautiful for an engineer to shape and design the same way
| that an artist shapes and designs. But whether the whole process
| makes any sense, whether men become happier - that I can no
| longer decide." - Rudolph Diesel
|
|


Nov 21 '05 #6

P: n/a
G.Ashok,
| Double dispatch will not eliminate the switch statements.
Double Dispatch is how you solve it. Without switch statements!

Look closely at my initial example & the following sample. It just happened
that my example C was both Currency & AccountManager. The sample below
separates the two types.

Something like:

Module Module1

Sub Main()

Dim manager As New AccountManager

manager.handleTransfer(New BankAccount)

End Sub

End Module

Public Interface IBankAccount

Sub deposit(ByVal currency As Currency)

' routines to handle the double dispatch.
Sub doCurrencyDeposit(ByVal currency As Currency)
Sub doEuroCurrencyDeposit(ByVal currency As EuroCurrency)

End Interface

Public Class Currency

' routines to handle the double dispatch
Public Overridable Sub Deposit(ByVal account As IBankAccount)
account.doCurrencyDeposit(Me)
End Sub

End Class

Public Class EuroCurrency : Inherits Currency

' routines to handle the double dispatch
Public Overrides Sub Deposit(ByVal account As IBankAccount)
account.doEuroCurrencyDeposit(Me)
End Sub

End Class

Public Class BankAccount
Implements IBankAccount

Public Sub deposit(ByVal currency As Currency) Implements
IBankAccount.deposit

' ask the specific type of currency to call me back via the
specific routine...
currency.Deposit(Me)
End Sub

' only called by Currency objects
Public Sub doCurrencyDeposit(ByVal currency As Currency) Implements
IBankAccount.doCurrencyDeposit
' do the actual 'Currency' deposit here
End Sub

' only called by EuroCurrency objects
Public Sub doEuroCurrencyDeposit(ByVal currency As EuroCurrency)
Implements IBankAccount.doEuroCurrencyDeposit
' do the actual 'EuroCurrency' deposit here
End Sub

End Class

Public Class AccountManager

Public Sub handleTransfer(ByVal account As IBankAccount)

Dim currency As currency = New EuroCurrency '// getCurrency()

account.deposit(currency)

currency = new Currency

account.deposit(currency)

End Sub

End Class

NOTE: both deposit(Currency) & DoCurrencyDeposit(Currency) are needed in
IBankAccount to avoid an endless recursive loop, during the "callback" from
the double dispatch...

NOTE: Rather then have a class hierarchy for Currency I would consider
implementing a Money type as discussed by Kent Beck in "Test - Driven
Development - By Example" from Addison Wesley press.
http://www.nunit.org/samples.html

The "danger" of double dispatch is that DoCurrencyDeposit &
DoEuroCurrencyDeposit need to be public, which means they may be called
inappropriately, if all the types concerned are in the same assembly I would
consider making them Friend instead of Public...

| I hope this helps to undertsand the problem in detail :o)
I hope you understand my sample in detail! ;-)

Hope this helps
Jay
"G.Ashok" <gw******@hotmail.com> wrote in message
news:uS**************@TK2MSFTNGP14.phx.gbl...
|
| Hi, Thank you all for the response in anyway,
|
| Here the story...
|
| Module Module1
| Sub Main()
|
| Dim am As New AccountManager
|
| am.handleTransfer(New BankAccount)
|
| End Sub
|
| End Module
|
| Public Interface IBankAccount
|
| Sub deposit(ByVal currency As Currency)
|
| 'Sub deposit(ByVal currency As EuroCurrency)
|
| End Interface
|
| Public Class Currency
|
| ' // These Currency classes could as easily be interfaces, with no impact
on
| the examples.
|
| ' // members
|
| '}
|
| End Class
|
| Public Class EuroCurrency : Inherits Currency
|
| '// members
|
| End Class
|
| Public Class BankAccount : Implements IBankAccount
|
| Public Sub deposit(ByVal currency As Currency) Implements
| IBankAccount.deposit
|
| '// handle standard deposit
|
| End Sub
|
| Public Sub deposit(ByVal currency As EuroCurrency) ' Implements
| IBankAccount.deposit
|
| '// handle Euro Currencies
|
| End Sub
|
| End Class
|
| Public Class AccountManager
|
| Public Sub handleTransfer(ByVal account As IBankAccount)
|
| Dim currency As Currency = New EuroCurrency '// getCurrency()
|
| '//
|
| '// Make a deposit. In standard Java and *.NET languages,
|
| '// this will always invoke BankAccount's first deposit method.
|
| '// With Multiple Polymorphism, this will invoke the second deposit
| method for
|
| '// EuroCurrency (and its derived classes), otherwise the first
| deposit method.
|
| account.deposit(currency)
|
| End Sub
|
| End Class
|
| Another workaround approach to the above problem is to create another
| interface method, defining the following:
|
| Public Interface IBankAccount
| Sub deposit(currency As Currency)
| Sub deposit(currency AS EuroCurrency)
| End Inteface
|
| But this would force all implementations to define a method for
| the second case, even if none were desired. This will further forces to
use
| several huge amount of switch (Select Case/If TypeOf ) statements
|
| With multiple polymorphism, if the currency returned by getCurrency is not
a
| EuroCurrency (inheritance is-a), the first deposit method defined in
| BankAcount will be called. If the currency is a EuroCurrency, the second
| (more specialized) deposit method will be called.
|
| Double dispatch will not eliminate the switch statements.
|
| I hope this helps to undertsand the problem in detail :o)
|
| Regards,
| ...Ashok
| ------------------------------------------------------------------------
| "It is beautiful for an engineer to shape and design the same way
| that an artist shapes and designs. But whether the whole process
| makes any sense, whether men become happier - that I can no
| longer decide." - Rudolph Diesel
|
|
| "G.Ashok" <gw******@hotmail.com> wrote in message
| news:eo**************@TK2MSFTNGP14.phx.gbl...
|
|| Hi,
||
|| Does anybody know how Multiple polymorphism can be done in VB.NET or DOT
|| doesn't support it? Is there any third party extensions available like
for
|| Java to do this?
||
|| Regards,
|| ...Ashok
|| ------------------------------------------------------------------------
|| "It is beautiful for an engineer to shape and design the same way
|| that an artist shapes and designs. But whether the whole process
|| makes any sense, whether men become happier - that I can no
|| longer decide." - Rudolph Diesel
||
||
|
|
|
|
Nov 21 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.