Multi Inherence

  • Thread starter Thread starter Bernard Bourée
  • Start date Start date
Bernard,
Is there a way to overpass the impossibility of VN.NET to accept the multi
heritage, that is to allow a class to inherit from TWO mother classes ?
I have always to be very carefully with this kind of texts because there is
some English language in it.

In my opinion is good design build on inheriting. Not on multi heritage.

And inheriting is build on the princible that a class can inherite from its
parent class.

And here comes the English language expect, for me can you never inherit
from two mothers. In natural life by instance Mothers can be an inherited
class from Human, where as well can be Fathers. There should never be a need
to instance as well from Fathers and Mothers because than you have to use
the class Human.

In my opinion you should avoid doing what you want that in your design, you
can make work arounds. However by instance inheriting from cars and from
mamals to create a new class is in my opinon always bad design, what you
should avoid as much as possible.

Just my thought reading this thread

Cor
 
Your note said that there are "other" .net languages that support multiple
inheiritances. I was asking how a class that inherited from two different
classes would know which property or method of the inheirited classes to call
if the two different classes happened to have a property or method with the
same name!
 
Dennis,
Your question, as I understand it, is one of the major reasons that Multiple
Inheritance is avoided in some circles.

My understanding that Eiffel.NET (http://www.eiffel.com) requires you to
indicate which method is called, similar to how VB.NET implements
"Implements Interface".

Eiffel.NET supports multiple inheritance on the .NET framework, although the
framework itself does not support multiple inheritance. Which I think is
rather cool! Granted only Eiffel classes can participate in the multiple
inheritance...

Hope this helps
Jay
 
Cor

Thank you for your interesting advise, but how to solve the following
problem.

I'm a car manufacturer and to do that I can use different kind of engines
(let say EngineA, EngineB, etc, each of them having their properties and
methods) but I need also different Shapes (A,B,etc) and Windows 'A,B,C),
etct, etc.

When I assemble a car I need to use the different objects (engines, windows,
shapes,...) and the car itself will have its own properties and methods.

If I understand correctly the concept, I need here to have a multi
inheritance of various objects .

Am I wrong or should I have an other way of thinking ?

Thanks for your help.
 
Bernard,

You said it yourself,

You make from the class cars a special object of the type car or an
inherrited class by instance trarilers (strange example however it has no
engine) :-)

The car has properties
engines which can be a an object from the class engines (or from an
inherited class from that)
windows which can be a string or an object from the class windows etc.

However the trailer has (when you have not shadowed them) all the properties
from the car, which when you have not overriden those are the same as the
Car

I made a simple sample for you from it.
\\\
Imports System.ComponentModel
'This above is only for the <Browsable(false)>
'before somebody say that that is the solution to
'hide a property in the intelisence
Public Module main
Public Sub main()
Dim mycar As New car
Dim mycarengine As New Engine
mycar.Engine = mycarengine
mycar.Engine.CC = 2000
mycarengine.Mark = "Peugeot"
mycar.Window = "Safe glass"
Dim mytrailer As New trailer
mytrailer.Engine = mycarengine
'This does nothing,
'for me is this the same problem as with the
'backgroundproperty of the picturebox
Console.WriteLine("The Car engine brand is a {0} ", _
DirectCast(mycar.Engine, Engine).Mark)
Console.WriteLine("The Car engine cc is {0} ", _
DirectCast(mycar.Engine, Engine).CC.ToString)
Console.WriteLine("The car window is from {0}", mycar.Window)
If mytrailer.Engine Is Nothing Then
Console.WriteLine("The trailer has no engine")
End If
Console.WriteLine("The trailer window is from {0}",
mytrailer.Window)
End Sub
End Module
Public Class car
Private mEngine As Engine
Private mWindow As String
Public Property Engine() As Engine
Get
Return mEngine
End Get
Set(ByVal Value As Engine)
mEngine = Value
End Set
End Property
Public Overridable Property Window() As String
Get
Return mWindow
End Get
Set(ByVal Value As String)
mWindow = Value
End Set
End Property
End Class
Public Class trailer
Inherits car
Sub New()
MyBase.Window = Window
MyBase.Engine = Nothing
End Sub
Public Overrides Property Window() As String
Get
Return "Unsafe glass"
End Get
Set(ByVal Value As String)
MyBase.Window = "Unsafe glass"
End Set
End Property
<Browsable(False)> _
Public Shadows Property Engine() As Engine
'I can not find any solution to hide it from intelisence
'When I make it private it takes direct the base class
Get
End Get
Set(ByVal Value As Engine)
End Set
End Property
End Class
Public Class Engine
Dim mMark As String
Dim mCC As Integer
Public Property Mark() As String
Get
Return mMark
End Get
Set(ByVal Value As String)
mMark = Value
End Set
End Property
Public Property CC() As Integer
Get
Return mCC
End Get
Set(ByVal Value As Integer)
mCC = Value
End Set
End Property
End Class
///

I hope this gives an idea?

Cor
 
Cor

Once more thank you so much to try to educate me.
It is more clear for me know.

Regards
 
Jay

If I understand correctly your exemple, in order to achieve my multy
inheritance I need to declare as properties in the Class Section, ALL
properties of IFluid and IDRop as well.

This means that If I make a modification in the Fluid Class I need also to
make the same modification the Section Class. Is it correct ?

--
Bernard Bourée
(e-mail address removed)
Jay B. Harlow said:
Bernard,
In addition to the other comments, you could use one or more Interfaces:

Something like:
Public Interface IFluid
Property Pressure() As Integer
Property Temp() As Integer
Property FlowRate() As Integer
End Interface

Public Class Fluid
Implements IFluid

Private m_flowRate As Integer
Private m_pressure As Integer
Private m_temp As Integer

Public Property FlowRate() As Integer Implements IFluid.FlowRate
Get
Return m_flowRate
End Get
Set(ByVal value As Integer)
m_flowRate = value
End Set
End Property

Public Property Pressure() As Integer Implements IFluid.Pressure
Get
Return m_pressure
End Get
Set(ByVal value As Integer)
m_pressure = value
End Set
End Property

Public Property Temp() As Integer Implements IFluid.Temp
Get
Return m_temp
End Get
Set(ByVal value As Integer)
m_temp = value
End Set
End Property

End Class

Public Interface IDrop
Property Diameter() As Integer
Property Temp() As Integer
Property Pressure() As Integer
End Interface

Public Class Drop
Implements IDrop

Private m_diameter As Integer
Private m_pressure As Integer
Private m_temp As Integer

Public Property Diameter() As Integer Implements IDrop.Diameter
Get
Return m_diameter
End Get
Set(ByVal value As Integer)
m_diameter = value
End Set
End Property

Public Property Pressure() As Integer Implements IDrop.Pressure
Get
Return m_pressure
End Get
Set(ByVal value As Integer)
m_pressure = value
End Set
End Property

Public Property Temp() As Integer Implements IDrop.Temp
Get
Return m_temp
End Get
Set(ByVal value As Integer)
m_temp = value
End Set
End Property

End Class

Public Interface ISection
Inherits IFluid
Inherits IDrop
End Interface

Public Class Section
Implements ISection

Private m_flowRate As Integer
Private m_pressure As Integer
Private m_temp As Integer
Private m_diameter As Integer

Public Property FlowRate() As Integer Implements IFluid.FlowRate
Get
Return m_flowRate
End Get
Set(ByVal value As Integer)
m_flowRate = value
End Set
End Property

Public Property Pressure() As Integer Implements IFluid.Pressure,
IDrop.Pressure
Get
Return m_pressure
End Get
Set(ByVal value As Integer)
m_pressure = value
End Set
End Property

Public Property Temp() As Integer Implements IFluid.Temp, IDrop.Temp
Get
Return m_temp
End Get
Set(ByVal value As Integer)
m_temp = value
End Set
End Property

Public Property Diameter() As Integer Implements IDrop.Diameter
Get
Return m_diameter
End Get
Set(ByVal value As Integer)
m_diameter = value
End Set
End Property

End Class

Then rather then define your parameters as the class, you would define them
as the Interface.

For Example:

Public Shared Sub ProcessFluid(ByVal aFluid As IFluid)
Debug.WriteLine(CObj(aFluid).GetType().Name)
Debug.Indent()
Debug.WriteLine(aFluid.Pressure, "Fluid.Pressure")
Debug.WriteLine(aFluid.Temp, "Fluid.Temp")
Debug.WriteLine(aFluid.FlowRate, "Fluid.FlowRate")
Debug.Unindent()
End Sub

Public Shared Sub ProcessDrop(ByVal aDrop As IDrop)
Debug.WriteLine(CObj(aFluid).GetType().Name)
Debug.Indent()
Debug.WriteLine(aDrop.Diameter, "Drop.Diameter")
Debug.WriteLine(aDrop.Temp, "Drop.Diameter")
Debug.WriteLine(aDrop.Pressure, "Drop.Pressure")
Debug.Unindent()
End Sub


Public Shared Sub Main()
Dim aFluid As New Fluid
Dim aDrop As New Drop
Dim aSection As New Section
ProcessFluid(aFluid)
ProcessFluid(aSection)

ProcessDrop(aDrop)
ProcessDrop(aSection)
End Sub

Note you don't need to use three actual interface, you can get by with only
defining Fluid or Drop in terms on an interface, then Section could inherit
from the other & implement the interface. The key is to use the Interface
instead of the class when you want to accept a Section in addition to the
class... Section could also implement ISection by using delegation to actual
Fluid & Drop objects...

Hope this helps
Jay
 
Bernard,
Based on Cor's comments.

By "Multiple inheritance" do you mean "IS A" or "HAS A".

For example a Car has a Tire, however a Car is not a Tire.

Is Section is a Fluid? Or does Section have a Fluid?
Is Section is a Drop? Or does Section have a Drop?

In my sample I was showing you have to program Section is a Fluid at the
same time Section is a Drop. Normally you would have Section inherit from
Fluid if Section is a Fluid, when any changes to Fluid would also be
reflected to Section, without any real work on your part per se. However!
you can only inherit from a single base class, hence the Interface &
Implements. When you use a Interface, then any changes to the interface
would need to be reflected in both classes...

For a very good "How To" book on OO in VB.NET check out Robin A.
Reynolds-Haertle's book "OOP with Microsoft Visual Basic .NET and Microsoft
Visual C# .NET - Step by Step" from MS Press.

Hope this helps
Jay
 
Jay

IS A or HAS A seams to be the main point.

Thanks a lot once more.

I want to take the opportunity to thanks the persons participating in this
news groups for their dedication and willingness to give extense support and
explanation .
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top