question about Class and Overridable

Discussion in 'ASP .Net' started by Ben, Jun 24, 2007.

  1. Ben

    Ben Guest

    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
     
    Ben, Jun 24, 2007
    #1
    1. Advertising

  2. Ben

    Masudur Guest

    On Jun 24, 9:36 pm, "Ben" <b@bn> wrote:
    > 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
     
    Masudur, Jun 24, 2007
    #2
    1. Advertising

  3. Ben

    Ben Guest

    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" <> schreef in bericht
    news:...
    > On Jun 24, 9:36 pm, "Ben" <b@bn> wrote:
    >> 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
    >
     
    Ben, Jun 24, 2007
    #3
  4. Ben

    Masudur Guest

    On Jun 25, 12:53 am, "Ben" <b@bn> wrote:
    > 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" <> schreef in berichtnews:...
    >
    > > On Jun 24, 9:36 pm, "Ben" <b@bn> wrote:
    > >> 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/usingvbnet/a/blinheritancea.htm

    Thanks again
    Masudur
    http://munnacs.110mb.com
     
    Masudur, Jun 24, 2007
    #4
  5. Ben

    Ben Guest

    Ok, thanks

    "Masudur" <> schreef in bericht
    news:...
    > On Jun 25, 12:53 am, "Ben" <b@bn> wrote:
    >> 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" <> schreef in
    >> berichtnews:...
    >>
    >> > On Jun 24, 9:36 pm, "Ben" <b@bn> wrote:
    >> >> 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/usingvbnet/a/blinheritancea.htm
    >
    > Thanks again
    > Masudur
    > http://munnacs.110mb.com
    >
    >
    >
    >
     
    Ben, Jun 25, 2007
    #5
  6. Ben

    Phill W. Guest

    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.
     
    Phill W., Jun 25, 2007
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. bminder
    Replies:
    1
    Views:
    2,175
    nfedin
    Feb 23, 2004
  2. Duron

    Font.Text overridable?

    Duron, Sep 13, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    328
    Duron
    Sep 14, 2004
  3. Dilip
    Replies:
    5
    Views:
    395
  4. Marcin Prochownik

    How to block member overridable

    Marcin Prochownik, Jun 21, 2007, in forum: C++
    Replies:
    5
    Views:
    497
    Bharath
    Jun 21, 2007
  5. markscottwright
    Replies:
    1
    Views:
    260
    John Machin
    Jun 18, 2008
Loading...

Share This Page