Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / June 2007

Tip: Looking for answers? Try searching our database.

question about Class and Overridable

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Ben - 24 Jun 2007 16:36 GMT
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
Masudur - 24 Jun 2007 18:21 GMT
> Hi,
>
[quoted text clipped - 32 lines]
> 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
Ben - 24 Jun 2007 19:53 GMT
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,
>>
[quoted text clipped - 54 lines]
> Masudur
> http://munnacs.110mb.com
Masudur - 24 Jun 2007 20:07 GMT
> Hi thanks but i'm nou sure to understand ...
> I indeed get a warning message in the child class.
[quoted text clipped - 67 lines]
> > 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/usingvbnet/a/blinheritancea.htm

Thanks again
Masudur
http://munnacs.110mb.com
Ben - 25 Jun 2007 09:32 GMT
Ok, thanks

>> Hi thanks but i'm nou sure to understand ...
>> I indeed get a warning message in the child class.
[quoted text clipped - 97 lines]
> Masudur
> http://munnacs.110mb.com
Phill W. - 25 Jun 2007 13:27 GMT
> 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.
[quoted text clipped - 19 lines]
> 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.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.