question about Class and Overridable

B

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
 
M

Masudur

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
 
B

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?
 
M

Masudur

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?

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/usingvbnet/a/blinheritancea.htm

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

Ben

Ok, thanks

Masudur said:
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/usingvbnet/a/blinheritancea.htm

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

Phill W.

Ben said:
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.
 

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

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,586
Members
45,083
Latest member
SylviaHarr

Latest Threads

Top