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

Code in derived class not called

P: n/a
Hi

Please can someone explain this behaviour:

I have a MustInherit Base class and a Derived class that Inherits Base and
Shadows a method in the base class.

If I Dim a variable of type Derived and New it as Derived the code in the
Derived class is called. However, if I Dim the variable as type Base but New
it as type Derived the Base class code is called - I would expect the code in
the derived class to be called in all instances.

Public MustInherit Class Base
Public Sub DoSomething()
MsgBox("Base")
End Sub
End Class

Public Class Class1
Inherits Base

Public Shadows Sub DoSomething()
MsgBox("Class1")
End Sub
End Class
Dim x As Base = New Class1
x.DoSomething() ' "Base" displayed

Dim y As Class1 = New Class1
y.DoSomething() ' "Class1" displayed

Many thanks for your comments.

Julia.
Nov 21 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
That is correct behaviour. Think about it - if your base class didn't have
the DoSomething method, would you even have been able to call DoSomething
via the base class variable? Definitely not - only if you cast it to the
derived class would you be able to do that. The base class variable can and
will only access members in the base class even though the object is an
instance of the derived class.

hope that helps..
Imran.

"Julia" <Ju***@discussions.microsoft.com> wrote in message
news:A4**********************************@microsof t.com...
Hi

Please can someone explain this behaviour:

I have a MustInherit Base class and a Derived class that Inherits Base and
Shadows a method in the base class.

If I Dim a variable of type Derived and New it as Derived the code in the
Derived class is called. However, if I Dim the variable as type Base but New it as type Derived the Base class code is called - I would expect the code in the derived class to be called in all instances.

Public MustInherit Class Base
Public Sub DoSomething()
MsgBox("Base")
End Sub
End Class

Public Class Class1
Inherits Base

Public Shadows Sub DoSomething()
MsgBox("Class1")
End Sub
End Class
Dim x As Base = New Class1
x.DoSomething() ' "Base" displayed

Dim y As Class1 = New Class1
y.DoSomething() ' "Class1" displayed

Many thanks for your comments.

Julia.

Nov 21 '05 #2

P: n/a
That is correct behaviour. Think about it - if your base class didn't have
the DoSomething method, would you even have been able to call DoSomething
via the base class variable? Definitely not - only if you cast it to the
derived class would you be able to do that. The base class variable can and
will only access members in the base class even though the object is an
instance of the derived class.

hope that helps..
Imran.

"Julia" <Ju***@discussions.microsoft.com> wrote in message
news:A4**********************************@microsof t.com...
Hi

Please can someone explain this behaviour:

I have a MustInherit Base class and a Derived class that Inherits Base and
Shadows a method in the base class.

If I Dim a variable of type Derived and New it as Derived the code in the
Derived class is called. However, if I Dim the variable as type Base but New it as type Derived the Base class code is called - I would expect the code in the derived class to be called in all instances.

Public MustInherit Class Base
Public Sub DoSomething()
MsgBox("Base")
End Sub
End Class

Public Class Class1
Inherits Base

Public Shadows Sub DoSomething()
MsgBox("Class1")
End Sub
End Class
Dim x As Base = New Class1
x.DoSomething() ' "Base" displayed

Dim y As Class1 = New Class1
y.DoSomething() ' "Class1" displayed

Many thanks for your comments.

Julia.

Nov 21 '05 #3

P: n/a
JD
Look into Overridable and Overrides, this will give you the behavior you are
looking for.
"Julia" <Ju***@discussions.microsoft.com> wrote in message
news:A4**********************************@microsof t.com...
Hi

Please can someone explain this behaviour:

I have a MustInherit Base class and a Derived class that Inherits Base and
Shadows a method in the base class.

If I Dim a variable of type Derived and New it as Derived the code in the
Derived class is called. However, if I Dim the variable as type Base but New it as type Derived the Base class code is called - I would expect the code in the derived class to be called in all instances.

Public MustInherit Class Base
Public Sub DoSomething()
MsgBox("Base")
End Sub
End Class

Public Class Class1
Inherits Base

Public Shadows Sub DoSomething()
MsgBox("Class1")
End Sub
End Class
Dim x As Base = New Class1
x.DoSomething() ' "Base" displayed

Dim y As Class1 = New Class1
y.DoSomething() ' "Class1" displayed

Many thanks for your comments.

Julia.

Nov 21 '05 #4

P: n/a
JD
Look into Overridable and Overrides, this will give you the behavior you are
looking for.
"Julia" <Ju***@discussions.microsoft.com> wrote in message
news:A4**********************************@microsof t.com...
Hi

Please can someone explain this behaviour:

I have a MustInherit Base class and a Derived class that Inherits Base and
Shadows a method in the base class.

If I Dim a variable of type Derived and New it as Derived the code in the
Derived class is called. However, if I Dim the variable as type Base but New it as type Derived the Base class code is called - I would expect the code in the derived class to be called in all instances.

Public MustInherit Class Base
Public Sub DoSomething()
MsgBox("Base")
End Sub
End Class

Public Class Class1
Inherits Base

Public Shadows Sub DoSomething()
MsgBox("Class1")
End Sub
End Class
Dim x As Base = New Class1
x.DoSomething() ' "Base" displayed

Dim y As Class1 = New Class1
y.DoSomething() ' "Class1" displayed

Many thanks for your comments.

Julia.

Nov 21 '05 #5

P: n/a
Julia,
I have a MustInherit Base class and a Derived class that Inherits Base and
Shadows a method in the base class. There's the rub!
However, if I Dim the variable as type Base but New
it as type Derived the Base class code is called - I would expect the code
in
the derived class to be called in all instances.
To get the behavior you want you need to use Overrides in the Derived class.
In the base class you need to use Overridable or MustOverride.

Something like:
Public MustInherit Class Base
Public Overridable Sub DoSomething()
MsgBox("Base")
End Sub
End Class

Public Class Class1
Inherits Base

Public Overrides Sub DoSomething()
MsgBox("Class1")
End Sub
End Class
Shadows is used when you define DoSomething in Class1, then later you add
DoSomething in Base, where Base.DoSomething is not compatible with
Class1.DoSomething, Shadows allows both to exist without interfering with
each other. In other words so that Base.DoSomething can be called without
invoking Class1.DoSomething.

Overridable is used when you offer implementation in Base that Derived can
replace or supplement.
MustOverride is used when Base has no implementation that Derived has to
implement.

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
"Julia" <Ju***@discussions.microsoft.com> wrote in message
news:A4**********************************@microsof t.com... Hi

Please can someone explain this behaviour:

I have a MustInherit Base class and a Derived class that Inherits Base and
Shadows a method in the base class.

If I Dim a variable of type Derived and New it as Derived the code in the
Derived class is called. However, if I Dim the variable as type Base but
New
it as type Derived the Base class code is called - I would expect the code
in
the derived class to be called in all instances.

Public MustInherit Class Base
Public Sub DoSomething()
MsgBox("Base")
End Sub
End Class

Public Class Class1
Inherits Base

Public Shadows Sub DoSomething()
MsgBox("Class1")
End Sub
End Class
Dim x As Base = New Class1
x.DoSomething() ' "Base" displayed

Dim y As Class1 = New Class1
y.DoSomething() ' "Class1" displayed

Many thanks for your comments.

Julia.

Nov 21 '05 #6

P: n/a
Julia,
I have a MustInherit Base class and a Derived class that Inherits Base and
Shadows a method in the base class. There's the rub!
However, if I Dim the variable as type Base but New
it as type Derived the Base class code is called - I would expect the code
in
the derived class to be called in all instances.
To get the behavior you want you need to use Overrides in the Derived class.
In the base class you need to use Overridable or MustOverride.

Something like:
Public MustInherit Class Base
Public Overridable Sub DoSomething()
MsgBox("Base")
End Sub
End Class

Public Class Class1
Inherits Base

Public Overrides Sub DoSomething()
MsgBox("Class1")
End Sub
End Class
Shadows is used when you define DoSomething in Class1, then later you add
DoSomething in Base, where Base.DoSomething is not compatible with
Class1.DoSomething, Shadows allows both to exist without interfering with
each other. In other words so that Base.DoSomething can be called without
invoking Class1.DoSomething.

Overridable is used when you offer implementation in Base that Derived can
replace or supplement.
MustOverride is used when Base has no implementation that Derived has to
implement.

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
"Julia" <Ju***@discussions.microsoft.com> wrote in message
news:A4**********************************@microsof t.com... Hi

Please can someone explain this behaviour:

I have a MustInherit Base class and a Derived class that Inherits Base and
Shadows a method in the base class.

If I Dim a variable of type Derived and New it as Derived the code in the
Derived class is called. However, if I Dim the variable as type Base but
New
it as type Derived the Base class code is called - I would expect the code
in
the derived class to be called in all instances.

Public MustInherit Class Base
Public Sub DoSomething()
MsgBox("Base")
End Sub
End Class

Public Class Class1
Inherits Base

Public Shadows Sub DoSomething()
MsgBox("Class1")
End Sub
End Class
Dim x As Base = New Class1
x.DoSomething() ' "Base" displayed

Dim y As Class1 = New Class1
y.DoSomething() ' "Class1" displayed

Many thanks for your comments.

Julia.

Nov 21 '05 #7

P: n/a
Hi Julia

As others have said, the behaviour you are seeing is correct, and Overrides
/ Overridable will give you the behaviour you want.

In terms of the why, let's have a go at that.

A regular method (as in not an Overridable or virtual method) in a .NET
class is implemented as a global function with a hidden first argument (the
Me / this) pointer (oops reference). So when using a reference to a type, you
will always call this global function, no matter what the object is that is
being referenced.

In contrast, an Overridable / virtual method is implemented as a virtual
function table in the object itself. Calling a virtual / overridable method
looks up the implementation from the object indirectly, rather than calling
the function directly.

HTH

Nigel Armstrong

"Julia" wrote:
Hi

Please can someone explain this behaviour:

I have a MustInherit Base class and a Derived class that Inherits Base and
Shadows a method in the base class.

If I Dim a variable of type Derived and New it as Derived the code in the
Derived class is called. However, if I Dim the variable as type Base but New
it as type Derived the Base class code is called - I would expect the code in
the derived class to be called in all instances.

Public MustInherit Class Base
Public Sub DoSomething()
MsgBox("Base")
End Sub
End Class

Public Class Class1
Inherits Base

Public Shadows Sub DoSomething()
MsgBox("Class1")
End Sub
End Class
Dim x As Base = New Class1
x.DoSomething() ' "Base" displayed

Dim y As Class1 = New Class1
y.DoSomething() ' "Class1" displayed

Many thanks for your comments.

Julia.

Nov 21 '05 #8

P: n/a
Hi Julia

As others have said, the behaviour you are seeing is correct, and Overrides
/ Overridable will give you the behaviour you want.

In terms of the why, let's have a go at that.

A regular method (as in not an Overridable or virtual method) in a .NET
class is implemented as a global function with a hidden first argument (the
Me / this) pointer (oops reference). So when using a reference to a type, you
will always call this global function, no matter what the object is that is
being referenced.

In contrast, an Overridable / virtual method is implemented as a virtual
function table in the object itself. Calling a virtual / overridable method
looks up the implementation from the object indirectly, rather than calling
the function directly.

HTH

Nigel Armstrong

"Julia" wrote:
Hi

Please can someone explain this behaviour:

I have a MustInherit Base class and a Derived class that Inherits Base and
Shadows a method in the base class.

If I Dim a variable of type Derived and New it as Derived the code in the
Derived class is called. However, if I Dim the variable as type Base but New
it as type Derived the Base class code is called - I would expect the code in
the derived class to be called in all instances.

Public MustInherit Class Base
Public Sub DoSomething()
MsgBox("Base")
End Sub
End Class

Public Class Class1
Inherits Base

Public Shadows Sub DoSomething()
MsgBox("Class1")
End Sub
End Class
Dim x As Base = New Class1
x.DoSomething() ' "Base" displayed

Dim y As Class1 = New Class1
y.DoSomething() ' "Class1" displayed

Many thanks for your comments.

Julia.

Nov 21 '05 #9

P: n/a
JD
> A regular method (as in not an Overridable or virtual method) in a .NET
class is implemented as a global function with a hidden first argument (the Me / this) pointer (oops reference). So when using a reference to a type, you will always call this global function, no matter what the object is that is being referenced.
I don't know if "Global" is a good word to use here. Its my understanding
that each type has a method table, first region within the method table is
for virtual methods, the second region for non-virtual methods. So the
methods are not "Global", and there really isn't a virtual method table,
there is just a method table. The methods are only relevant within the
specific type or its derived types.

Also for the hidden first argument, that happens whether the method is
virtual or not. I don't know if you meant that it only happens with the
non-virtual method.

JD
"Nigel Armstrong" <Ni************@discussions.microsoft.com> wrote in
message news:4C**********************************@microsof t.com... Hi Julia

As others have said, the behaviour you are seeing is correct, and Overrides / Overridable will give you the behaviour you want.

In terms of the why, let's have a go at that.

A regular method (as in not an Overridable or virtual method) in a .NET
class is implemented as a global function with a hidden first argument (the Me / this) pointer (oops reference). So when using a reference to a type, you will always call this global function, no matter what the object is that is being referenced.

In contrast, an Overridable / virtual method is implemented as a virtual
function table in the object itself. Calling a virtual / overridable method looks up the implementation from the object indirectly, rather than calling the function directly.

HTH

Nigel Armstrong

"Julia" wrote:
Hi

Please can someone explain this behaviour:

I have a MustInherit Base class and a Derived class that Inherits Base and Shadows a method in the base class.

If I Dim a variable of type Derived and New it as Derived the code in the Derived class is called. However, if I Dim the variable as type Base but New it as type Derived the Base class code is called - I would expect the code in the derived class to be called in all instances.

Public MustInherit Class Base
Public Sub DoSomething()
MsgBox("Base")
End Sub
End Class

Public Class Class1
Inherits Base

Public Shadows Sub DoSomething()
MsgBox("Class1")
End Sub
End Class
Dim x As Base = New Class1
x.DoSomething() ' "Base" displayed

Dim y As Class1 = New Class1
y.DoSomething() ' "Class1" displayed

Many thanks for your comments.

Julia.

Nov 21 '05 #10

P: n/a
JD
> A regular method (as in not an Overridable or virtual method) in a .NET
class is implemented as a global function with a hidden first argument (the Me / this) pointer (oops reference). So when using a reference to a type, you will always call this global function, no matter what the object is that is being referenced.
I don't know if "Global" is a good word to use here. Its my understanding
that each type has a method table, first region within the method table is
for virtual methods, the second region for non-virtual methods. So the
methods are not "Global", and there really isn't a virtual method table,
there is just a method table. The methods are only relevant within the
specific type or its derived types.

Also for the hidden first argument, that happens whether the method is
virtual or not. I don't know if you meant that it only happens with the
non-virtual method.

JD
"Nigel Armstrong" <Ni************@discussions.microsoft.com> wrote in
message news:4C**********************************@microsof t.com... Hi Julia

As others have said, the behaviour you are seeing is correct, and Overrides / Overridable will give you the behaviour you want.

In terms of the why, let's have a go at that.

A regular method (as in not an Overridable or virtual method) in a .NET
class is implemented as a global function with a hidden first argument (the Me / this) pointer (oops reference). So when using a reference to a type, you will always call this global function, no matter what the object is that is being referenced.

In contrast, an Overridable / virtual method is implemented as a virtual
function table in the object itself. Calling a virtual / overridable method looks up the implementation from the object indirectly, rather than calling the function directly.

HTH

Nigel Armstrong

"Julia" wrote:
Hi

Please can someone explain this behaviour:

I have a MustInherit Base class and a Derived class that Inherits Base and Shadows a method in the base class.

If I Dim a variable of type Derived and New it as Derived the code in the Derived class is called. However, if I Dim the variable as type Base but New it as type Derived the Base class code is called - I would expect the code in the derived class to be called in all instances.

Public MustInherit Class Base
Public Sub DoSomething()
MsgBox("Base")
End Sub
End Class

Public Class Class1
Inherits Base

Public Shadows Sub DoSomething()
MsgBox("Class1")
End Sub
End Class
Dim x As Base = New Class1
x.DoSomething() ' "Base" displayed

Dim y As Class1 = New Class1
y.DoSomething() ' "Class1" displayed

Many thanks for your comments.

Julia.

Nov 21 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.