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

question about Class and Overridable

P: n/a
Ben
Hi,

i defined a function in the base class 'ford' and the same function (with
different output) in subclass "peugeot".
I first put 'Overridable function' in the base class and 'Overrides
function' in the subclass.
It works. Then i removed them in both classes, like here below:

Public Class ford
.....
Public Function tuut() As String
Return "this is function of class ford"
End Function
End Class

Public Class peugeot
Inherits ford
Public Sub New()
....
End Sub
Public Function tuut() As String
Return "this is function of class peugeot "
End Function
End Class

and it still works, giving the same result ("this is function of class
peugeot").

My question is: why shoud i use in class ford: Public overridable Function
tuut() As String
and in class peugeot: Public overrides Function tuut() As String because
with or without, it gives the same output in code-behind with this:
Label1.Text = peugeot.tuut?

Thanks
Ben
Jun 24 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
On Jun 24, 9:36 pm, "Ben" <b@bnwrote:
Hi,

i defined a function in the base class 'ford' and the same function (with
different output) in subclass "peugeot".
I first put 'Overridable function' in the base class and 'Overrides
function' in the subclass.
It works. Then i removed them in both classes, like here below:

Public Class ford
....
Public Function tuut() As String
Return "this is function of class ford"
End Function
End Class

Public Class peugeot
Inherits ford
Public Sub New()
....
End Sub
Public Function tuut() As String
Return "this is function of class peugeot "
End Function
End Class

and it still works, giving the same result ("this is function of class
peugeot").

My question is: why shoud i use in class ford: Public overridable Function
tuut() As String
and in class peugeot: Public overrides Function tuut() As String because
with or without, it gives the same output in code-behind with this:
Label1.Text = peugeot.tuut?

Thanks
Ben
Hi... you are kind of braking the OOP Concept...
in Child class you are suppose to get a wornning msg saying you are
shadowing a method...
well you got to say Overridable to a base class method so that child
classes can override the method...
if you dont want to override the method dont put Overridable

now in child class if you declare same medthod ... it is by default a
sharow of the base class's method...

Public Shadows Function tuut() As String
Return "this is function of class peugeot "
End Function

if you omit Shadows it doesn't matter ... still compiler take it as
shadow method...
Thanks
Masudur
http://munnacs.110mb.com

Jun 24 '07 #2

P: n/a
Ben
Hi thanks but i'm nou sure to understand ...
I indeed get a warning message in the child class.
So, except the fact it's against the OOP rules and that i get a warning
message, the result is the same with or without, no?

But what do you mean with this?
"now in child class if you declare same medthod ... it is by default a
sharow of the base class's method..."

According the definition of shadows (no inheritance exists between base and
child, and the base class is accessed), i should get the other string (this
is function of class ford), or am i wrong?
"Masudur" <mu*****@gmail.comschreef in bericht
news:11**********************@w5g2000hsg.googlegro ups.com...
On Jun 24, 9:36 pm, "Ben" <b@bnwrote:
>Hi,

i defined a function in the base class 'ford' and the same function (with
different output) in subclass "peugeot".
I first put 'Overridable function' in the base class and 'Overrides
function' in the subclass.
It works. Then i removed them in both classes, like here below:

Public Class ford
....
Public Function tuut() As String
Return "this is function of class ford"
End Function
End Class

Public Class peugeot
Inherits ford
Public Sub New()
....
End Sub
Public Function tuut() As String
Return "this is function of class peugeot "
End Function
End Class

and it still works, giving the same result ("this is function of class
peugeot").

My question is: why shoud i use in class ford: Public overridable
Function
tuut() As String
and in class peugeot: Public overrides Function tuut() As String because
with or without, it gives the same output in code-behind with this:
Label1.Text = peugeot.tuut?

Thanks
Ben

Hi... you are kind of braking the OOP Concept...
in Child class you are suppose to get a wornning msg saying you are
shadowing a method...
well you got to say Overridable to a base class method so that child
classes can override the method...
if you dont want to override the method dont put Overridable

now in child class if you declare same medthod ... it is by default a
sharow of the base class's method...

Public Shadows Function tuut() As String
Return "this is function of class peugeot "
End Function

if you omit Shadows it doesn't matter ... still compiler take it as
shadow method...
Thanks
Masudur
http://munnacs.110mb.com

Jun 24 '07 #3

P: n/a
On Jun 25, 12:53 am, "Ben" <b@bnwrote:
Hi thanks but i'm nou sure to understand ...
I indeed get a warning message in the child class.
So, except the fact it's against the OOP rules and that i get a warning
message, the result is the same with or without, no?

But what do you mean with this?
"now in child class if you declare same medthod ... it is by default a
sharow of the base class's method..."

According the definition of shadows (no inheritance exists between base and
child, and the base class is accessed), i should get the other string (this
is function of class ford), or am i wrong?

"Masudur" <munn...@gmail.comschreef in berichtnews:11**********************@w5g2000hsg.go oglegroups.com...
On Jun 24, 9:36 pm, "Ben" <b@bnwrote:
Hi,
i defined a function in the base class 'ford' and the same function (with
different output) in subclass "peugeot".
I first put 'Overridable function' in the base class and 'Overrides
function' in the subclass.
It works. Then i removed them in both classes, like here below:
Public Class ford
....
Public Function tuut() As String
Return "this is function of class ford"
End Function
End Class
Public Class peugeot
Inherits ford
Public Sub New()
....
End Sub
Public Function tuut() As String
Return "this is function of class peugeot "
End Function
End Class
and it still works, giving the same result ("this is function of class
peugeot").
My question is: why shoud i use in class ford: Public overridable
Function
tuut() As String
and in class peugeot: Public overrides Function tuut() As String because
with or without, it gives the same output in code-behind with this:
Label1.Text = peugeot.tuut?
Thanks
Ben
Hi... you are kind of braking the OOP Concept...
in Child class you are suppose to get a wornning msg saying you are
shadowing a method...
well you got to say Overridable to a base class method so that child
classes can override the method...
if you dont want to override the method dont put Overridable
now in child class if you declare same medthod ... it is by default a
sharow of the base class's method...
Public Shadows Function tuut() As String
Return "this is function of class peugeot "
End Function
if you omit Shadows it doesn't matter ... still compiler take it as
shadow method...
Thanks
Masudur
http://munnacs.110mb.com
hi...

""But what do you mean with this?
"now in child class if you declare same medthod ... it is by default
a
sharow of the base class's method..." ""
sorry type mistake it will be shadow...

"The keyword Shadows means that when a member of a derived class has
the same name as a member of the same type in the base class, then the
member in the derived class entirely replaces all variations of the
method from the base class, leaving the derived class with only a
single version of the method, that is, the one created in the derived
class. Shadows does not extend an interface, but rather replaces
existing methods. In addition, Shadows allows a member type to
"override" any other member type. Thus, for example, a method can
"override" a property. Keep in mind that the Shadows keyword is not
required, since Shadows is the default. But if you leave it off, the
compiler will warn that the method is being shadowed, not overloaded."

http://visualbasic.about.com/od/usin...heritancea.htm

Thanks again
Masudur
http://munnacs.110mb.com


Jun 24 '07 #4

P: n/a
Ben
Ok, thanks

"Masudur" <mu*****@gmail.comschreef in bericht
news:11**********************@p77g2000hsh.googlegr oups.com...
On Jun 25, 12:53 am, "Ben" <b@bnwrote:
>Hi thanks but i'm nou sure to understand ...
I indeed get a warning message in the child class.
So, except the fact it's against the OOP rules and that i get a warning
message, the result is the same with or without, no?

But what do you mean with this?
"now in child class if you declare same medthod ... it is by default a
sharow of the base class's method..."

According the definition of shadows (no inheritance exists between base
and
child, and the base class is accessed), i should get the other string
(this
is function of class ford), or am i wrong?

"Masudur" <munn...@gmail.comschreef in
berichtnews:11**********************@w5g2000hsg.g ooglegroups.com...
On Jun 24, 9:36 pm, "Ben" <b@bnwrote:
Hi,
>i defined a function in the base class 'ford' and the same function
(with
different output) in subclass "peugeot".
I first put 'Overridable function' in the base class and 'Overrides
function' in the subclass.
It works. Then i removed them in both classes, like here below:
>Public Class ford
....
Public Function tuut() As String
Return "this is function of class ford"
End Function
End Class
>Public Class peugeot
Inherits ford
Public Sub New()
....
End Sub
Public Function tuut() As String
Return "this is function of class peugeot "
End Function
End Class
>and it still works, giving the same result ("this is function of class
peugeot").
>My question is: why shoud i use in class ford: Public overridable
Function
tuut() As String
and in class peugeot: Public overrides Function tuut() As String
because
with or without, it gives the same output in code-behind with this:
Label1.Text = peugeot.tuut?
>Thanks
Ben
Hi... you are kind of braking the OOP Concept...
in Child class you are suppose to get a wornning msg saying you are
shadowing a method...
well you got to say Overridable to a base class method so that child
classes can override the method...
if you dont want to override the method dont put Overridable
now in child class if you declare same medthod ... it is by default a
sharow of the base class's method...
Public Shadows Function tuut() As String
Return "this is function of class peugeot "
End Function
if you omit Shadows it doesn't matter ... still compiler take it as
shadow method...
Thanks
Masudur
http://munnacs.110mb.com

hi...

""But what do you mean with this?
"now in child class if you declare same medthod ... it is by default
a
sharow of the base class's method..." ""
sorry type mistake it will be shadow...

"The keyword Shadows means that when a member of a derived class has
the same name as a member of the same type in the base class, then the
member in the derived class entirely replaces all variations of the
method from the base class, leaving the derived class with only a
single version of the method, that is, the one created in the derived
class. Shadows does not extend an interface, but rather replaces
existing methods. In addition, Shadows allows a member type to
"override" any other member type. Thus, for example, a method can
"override" a property. Keep in mind that the Shadows keyword is not
required, since Shadows is the default. But if you leave it off, the
compiler will warn that the method is being shadowed, not overloaded."

http://visualbasic.about.com/od/usin...heritancea.htm

Thanks again
Masudur
http://munnacs.110mb.com


Jun 25 '07 #5

P: n/a
Ben wrote:
i defined a function in the base class 'ford' and the same function (with
different output) in subclass "peugeot".
Well, to start with, a "peugeot" is not a "further refinement" of a
"ford" so the one should /not/ be a subclass of the other. Both are
probably subclasses of "car", but neither directly relates to the other.

But anyway ...
I first put 'Overridable function' in the base class and 'Overrides
function' in the subclass.
It works. Then i removed them in both classes, like here below:

Public Class ford
....
Public Function tuut() As String
Return "this is function of class ford"
End Function
End Class

Public Class peugeot
Inherits ford
Public Sub New()
....
End Sub
Public Function tuut() As String
Return "this is function of class peugeot "
End Function
End Class

and it still works, giving the same result ("this is function of class
peugeot").
Yes, it works, but it also gives you a warning that peugeot::tuut is
Shadowing ford::tuut.

This is Bad.

Try these:

Dim c1 as ford = New ford
MsgBox( c1.tuut )

Dim c2 as peugeot = New peugeot
MsgBox( c2.tuut )

Dim c3 as Ford = New peugeot
MsgBox( c3.tuut )

That last one is the root of your problem. A variable of Type ford
/can/ contain a value that happens to be a peugeot - because one is a
subclass of the other, the higher-level variable can "hold" the
lower-level object.

Welcome to the Wacky World of Polymorphism.

What you need to happen is for a peugeot object to /always/ execute its
implementation of tuut() and that's where the overriding bit comes in.

When you override a method, you effectively "unplug" the original
implementation (the code in ford::tuut) and replace it with your own
implementation (the code in peugeot::tuut). That way, even if you have
a ford variable and call tuut on it, it will do /whatever/ it
appropriate for the Type of object that the variable contains.

Put the overridable/overrides back in and try the three tests, above,
again.

HTH,
Phill W.
Jun 25 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.