Need help: about OOP inheritance/abstract class

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tee

    Need help: about OOP inheritance/abstract class

    Hi,

    I have a base usercontrol with a method (blank method, no code), I have
    another few usercontrols that will inherit this base usercontrol, but I want
    to force all the usercontrol that inheriting this base usercontrol to
    override the method with its own code. How do I do it?

    I have tried to make the base usercontrol an abstract class (mustinherits +
    mustoverrides), this works, but all the usercontrols that inherit it will
    not able to open in VS designer because of inherting an abstract class.

    I have also tried to use interface, this works as well, but I can't force
    those usercontrols that inherit base usercontrol MUST have the interface.

    Anyone has a solution for this?


    Thanks,
    Tee


  • Elementary Penguin

    #2
    Re: Need help: about OOP inheritance/abstract class

    Tee wrote:
    [color=blue]
    > Hi,
    >
    > I have a base usercontrol with a method (blank method, no code), I have
    > another few usercontrols that will inherit this base usercontrol, but I
    > want to force all the usercontrol that inheriting this base usercontrol to
    > override the method with its own code. How do I do it?[/color]

    If you are going to /force/ them to override it ( polymorphism ) why have it
    in the first place?

    You should define an *interface* with a stub method and force them to
    implement it.

    I do this in one of my web service applications. There are two interfaces
    and a base abstract class. The interface accounts for methods that must
    be implemented in the derived classes, but which have no default method in
    the abstract class. So each derived class has one abstract class and two
    interfaces to inherit from.


    --

    Textcasting Technology
    Incognito Blog


    Comment

    • Elementary Penguin

      #3
      Re: Need help: about OOP inheritance/abstract class

      Tee wrote:
      [color=blue]
      > Hi,
      >
      > I have a base usercontrol with a method (blank method, no code), I have
      > another few usercontrols that will inherit this base usercontrol, but I
      > want to force all the usercontrol that inheriting this base usercontrol to
      > override the method with its own code. How do I do it?[/color]

      If you are going to /force/ them to override it ( polymorphism ) why have it
      in the first place?

      You should define an *interface* with a stub method and force them to
      implement it.

      I do this in one of my web service applications. There are two interfaces
      and a base abstract class. The interface accounts for methods that must
      be implemented in the derived classes, but which have no default method in
      the abstract class. So each derived class has one abstract class and two
      interfaces to inherit from.


      --

      Textcasting Technology
      Incognito Blog


      Comment

      • Olorin

        #4
        Re: Need help: about OOP inheritance/abstract class

        I haven't tried, but have you tried to use inheirtance AND interface
        together?

        i.e.
        * base class with stuff that should be inherited as-is
        * interface with the definition of the method that the inheriting
        classes should implement/override
        * inheriting/implementing classes: they inherit from the base class,
        AND they implement the interface.

        The children should then get all that the base class provides and Be
        Forced to implement the method(s) defined in the interface.
        HTH,
        F.O.R.

        Comment

        • Olorin

          #5
          Re: Need help: about OOP inheritance/abstract class

          I haven't tried, but have you tried to use inheirtance AND interface
          together?

          i.e.
          * base class with stuff that should be inherited as-is
          * interface with the definition of the method that the inheriting
          classes should implement/override
          * inheriting/implementing classes: they inherit from the base class,
          AND they implement the interface.

          The children should then get all that the base class provides and Be
          Forced to implement the method(s) defined in the interface.
          HTH,
          F.O.R.

          Comment

          • Tee

            #6
            Re: Need help: about OOP inheritance/abstract class

            I have tried it.
            [color=blue]
            > The children should then get all that the base class provides and Be
            > Forced to implement the method(s) defined in the interface.
            > HTH,
            > F.O.R.[/color]

            The base class that implements the interface will automatically has the
            method in the class (just an empty method, no code).
            And yes, the children get all the base class provided, and they inherit the
            empty method as well ... the problem is it won't force the children to
            overrides it.


            Thanks.


            "Olorin" <francesco.rizz i@gmail.com> wrote in message
            news:1106795597 .572258.201360@ c13g2000cwb.goo glegroups.com.. .[color=blue]
            > I haven't tried, but have you tried to use inheirtance AND interface
            > together?
            >
            > i.e.
            > * base class with stuff that should be inherited as-is
            > * interface with the definition of the method that the inheriting
            > classes should implement/override
            > * inheriting/implementing classes: they inherit from the base class,
            > AND they implement the interface.
            >
            > The children should then get all that the base class provides and Be
            > Forced to implement the method(s) defined in the interface.
            > HTH,
            > F.O.R.
            >[/color]


            Comment

            • Tee

              #7
              Re: Need help: about OOP inheritance/abstract class

              I have tried it.
              [color=blue]
              > The children should then get all that the base class provides and Be
              > Forced to implement the method(s) defined in the interface.
              > HTH,
              > F.O.R.[/color]

              The base class that implements the interface will automatically has the
              method in the class (just an empty method, no code).
              And yes, the children get all the base class provided, and they inherit the
              empty method as well ... the problem is it won't force the children to
              overrides it.


              Thanks.


              "Olorin" <francesco.rizz i@gmail.com> wrote in message
              news:1106795597 .572258.201360@ c13g2000cwb.goo glegroups.com.. .[color=blue]
              > I haven't tried, but have you tried to use inheirtance AND interface
              > together?
              >
              > i.e.
              > * base class with stuff that should be inherited as-is
              > * interface with the definition of the method that the inheriting
              > classes should implement/override
              > * inheriting/implementing classes: they inherit from the base class,
              > AND they implement the interface.
              >
              > The children should then get all that the base class provides and Be
              > Forced to implement the method(s) defined in the interface.
              > HTH,
              > F.O.R.
              >[/color]


              Comment

              • Bruce Wood

                #8
                Re: Need help: about OOP inheritance/abstract class

                This is not just a problem with user controls; it is also a problem
                with inheriting Forms.

                You cannot use an abstract base class for the reasons you mentioned.
                There is no way around it at present.

                The best you can do is write the methods in the base class to throw a
                NotImplementedE xception. Then if the child class does not override them
                it will blow up at run time. There is no way to enforce the override
                via a compile-time check.

                Comment

                • Bruce Wood

                  #9
                  Re: Need help: about OOP inheritance/abstract class

                  This is not just a problem with user controls; it is also a problem
                  with inheriting Forms.

                  You cannot use an abstract base class for the reasons you mentioned.
                  There is no way around it at present.

                  The best you can do is write the methods in the base class to throw a
                  NotImplementedE xception. Then if the child class does not override them
                  it will blow up at run time. There is no way to enforce the override
                  via a compile-time check.

                  Comment

                  • Dennis Myrén

                    #10
                    Re: Need help: about OOP inheritance/abstract class

                    Why not just(shortened) :

                    abstract class Abstract
                    {
                    public abstract void MustOverrideThi sAbstractMethod ( ) ;
                    }

                    class Concrete : Abstract
                    {
                    override public void MustOverrideThi sAbstractMethod ( )
                    {
                    //Implement something here.
                    }

                    }

                    Declaring a method as abstract within an abstract class means the base class
                    will demand an implementation of that method from the concrete derived
                    class.


                    --
                    Regards,
                    Dennis JD Myrén
                    Oslo Kodebureau
                    "Tee" <thy@streamyx.c om> wrote in message
                    news:unWQZ0BBFH A.380@TK2MSFTNG P10.phx.gbl...[color=blue]
                    > Hi,
                    >
                    > I have a base usercontrol with a method (blank method, no code), I have
                    > another few usercontrols that will inherit this base usercontrol, but I
                    > want
                    > to force all the usercontrol that inheriting this base usercontrol to
                    > override the method with its own code. How do I do it?
                    >
                    > I have tried to make the base usercontrol an abstract class (mustinherits
                    > +
                    > mustoverrides), this works, but all the usercontrols that inherit it will
                    > not able to open in VS designer because of inherting an abstract class.
                    >
                    > I have also tried to use interface, this works as well, but I can't force
                    > those usercontrols that inherit base usercontrol MUST have the interface.
                    >
                    > Anyone has a solution for this?
                    >
                    >
                    > Thanks,
                    > Tee
                    >
                    >[/color]


                    Comment

                    • Dennis Myrén

                      #11
                      Re: Need help: about OOP inheritance/abstract class

                      Why not just(shortened) :

                      abstract class Abstract
                      {
                      public abstract void MustOverrideThi sAbstractMethod ( ) ;
                      }

                      class Concrete : Abstract
                      {
                      override public void MustOverrideThi sAbstractMethod ( )
                      {
                      //Implement something here.
                      }

                      }

                      Declaring a method as abstract within an abstract class means the base class
                      will demand an implementation of that method from the concrete derived
                      class.


                      --
                      Regards,
                      Dennis JD Myrén
                      Oslo Kodebureau
                      "Tee" <thy@streamyx.c om> wrote in message
                      news:unWQZ0BBFH A.380@TK2MSFTNG P10.phx.gbl...[color=blue]
                      > Hi,
                      >
                      > I have a base usercontrol with a method (blank method, no code), I have
                      > another few usercontrols that will inherit this base usercontrol, but I
                      > want
                      > to force all the usercontrol that inheriting this base usercontrol to
                      > override the method with its own code. How do I do it?
                      >
                      > I have tried to make the base usercontrol an abstract class (mustinherits
                      > +
                      > mustoverrides), this works, but all the usercontrols that inherit it will
                      > not able to open in VS designer because of inherting an abstract class.
                      >
                      > I have also tried to use interface, this works as well, but I can't force
                      > those usercontrols that inherit base usercontrol MUST have the interface.
                      >
                      > Anyone has a solution for this?
                      >
                      >
                      > Thanks,
                      > Tee
                      >
                      >[/color]


                      Comment

                      • Tee

                        #12
                        Re: Need help: about OOP inheritance/abstract class

                        Hi Dennis,

                        See my previous post:[color=blue]
                        >I have tried to make the base usercontrol an abstract class (mustinherits +[/color]
                        mustoverrides), this works, but all the usercontrols that inherit it will
                        not able to open in VS designer because of inherting an abstract class.

                        This just won't work on forms or usercontrols ... it won't effect runtime,
                        but it's trouble enough if you can't open it in VS Designer.


                        Thanks,
                        Tee


                        "Dennis Myrén" <dennis@oslokb. no> wrote in message
                        news:dN1Kd.6183 $Sl3.148441@new s4.e.nsc.no...[color=blue]
                        > Why not just(shortened) :
                        >
                        > abstract class Abstract
                        > {
                        > public abstract void MustOverrideThi sAbstractMethod ( ) ;
                        > }
                        >
                        > class Concrete : Abstract
                        > {
                        > override public void MustOverrideThi sAbstractMethod ( )
                        > {
                        > //Implement something here.
                        > }
                        >
                        > }
                        >
                        > Declaring a method as abstract within an abstract class means the base[/color]
                        class[color=blue]
                        > will demand an implementation of that method from the concrete derived
                        > class.
                        >
                        >
                        > --
                        > Regards,
                        > Dennis JD Myrén
                        > Oslo Kodebureau
                        > "Tee" <thy@streamyx.c om> wrote in message
                        > news:unWQZ0BBFH A.380@TK2MSFTNG P10.phx.gbl...[color=green]
                        > > Hi,
                        > >
                        > > I have a base usercontrol with a method (blank method, no code), I have
                        > > another few usercontrols that will inherit this base usercontrol, but I
                        > > want
                        > > to force all the usercontrol that inheriting this base usercontrol to
                        > > override the method with its own code. How do I do it?
                        > >
                        > > I have tried to make the base usercontrol an abstract class[/color][/color]
                        (mustinherits[color=blue][color=green]
                        > > +
                        > > mustoverrides), this works, but all the usercontrols that inherit it[/color][/color]
                        will[color=blue][color=green]
                        > > not able to open in VS designer because of inherting an abstract class.
                        > >
                        > > I have also tried to use interface, this works as well, but I can't[/color][/color]
                        force[color=blue][color=green]
                        > > those usercontrols that inherit base usercontrol MUST have the[/color][/color]
                        interface.[color=blue][color=green]
                        > >
                        > > Anyone has a solution for this?
                        > >
                        > >
                        > > Thanks,
                        > > Tee
                        > >
                        > >[/color]
                        >
                        >[/color]


                        Comment

                        • Tee

                          #13
                          Re: Need help: about OOP inheritance/abstract class

                          Hi Dennis,

                          See my previous post:[color=blue]
                          >I have tried to make the base usercontrol an abstract class (mustinherits +[/color]
                          mustoverrides), this works, but all the usercontrols that inherit it will
                          not able to open in VS designer because of inherting an abstract class.

                          This just won't work on forms or usercontrols ... it won't effect runtime,
                          but it's trouble enough if you can't open it in VS Designer.


                          Thanks,
                          Tee


                          "Dennis Myrén" <dennis@oslokb. no> wrote in message
                          news:dN1Kd.6183 $Sl3.148441@new s4.e.nsc.no...[color=blue]
                          > Why not just(shortened) :
                          >
                          > abstract class Abstract
                          > {
                          > public abstract void MustOverrideThi sAbstractMethod ( ) ;
                          > }
                          >
                          > class Concrete : Abstract
                          > {
                          > override public void MustOverrideThi sAbstractMethod ( )
                          > {
                          > //Implement something here.
                          > }
                          >
                          > }
                          >
                          > Declaring a method as abstract within an abstract class means the base[/color]
                          class[color=blue]
                          > will demand an implementation of that method from the concrete derived
                          > class.
                          >
                          >
                          > --
                          > Regards,
                          > Dennis JD Myrén
                          > Oslo Kodebureau
                          > "Tee" <thy@streamyx.c om> wrote in message
                          > news:unWQZ0BBFH A.380@TK2MSFTNG P10.phx.gbl...[color=green]
                          > > Hi,
                          > >
                          > > I have a base usercontrol with a method (blank method, no code), I have
                          > > another few usercontrols that will inherit this base usercontrol, but I
                          > > want
                          > > to force all the usercontrol that inheriting this base usercontrol to
                          > > override the method with its own code. How do I do it?
                          > >
                          > > I have tried to make the base usercontrol an abstract class[/color][/color]
                          (mustinherits[color=blue][color=green]
                          > > +
                          > > mustoverrides), this works, but all the usercontrols that inherit it[/color][/color]
                          will[color=blue][color=green]
                          > > not able to open in VS designer because of inherting an abstract class.
                          > >
                          > > I have also tried to use interface, this works as well, but I can't[/color][/color]
                          force[color=blue][color=green]
                          > > those usercontrols that inherit base usercontrol MUST have the[/color][/color]
                          interface.[color=blue][color=green]
                          > >
                          > > Anyone has a solution for this?
                          > >
                          > >
                          > > Thanks,
                          > > Tee
                          > >
                          > >[/color]
                          >
                          >[/color]


                          Comment

                          • Dennis Myrén

                            #14
                            Re: Need help: about OOP inheritance/abstract class

                            Sorry, i should probably read the posts fully before replying them.

                            I had a bunch of webforms once which all needed common extra features in
                            order
                            to provide the functionality of a step in a wizard.
                            There was no way i could force those webforms to implement my custom
                            interface
                            in addition to inherit System.Web.UI.P age.
                            But i myself though, knew which ones of them should provide that
                            functionality,
                            and had them implement that interface.
                            class WizardStep1 : System.Web.UI.P age, IWizardStep



                            --
                            Regards,
                            Dennis JD Myrén
                            Oslo Kodebureau
                            "Tee" <thy@streamyx.c om> wrote in message
                            news:%23K7ha9EB FHA.2624@TK2MSF TNGP11.phx.gbl. ..[color=blue]
                            > Hi Dennis,
                            >
                            > See my previous post:[color=green]
                            >>I have tried to make the base usercontrol an abstract class (mustinherits
                            >>+[/color]
                            > mustoverrides), this works, but all the usercontrols that inherit it will
                            > not able to open in VS designer because of inherting an abstract class.
                            >
                            > This just won't work on forms or usercontrols ... it won't effect runtime,
                            > but it's trouble enough if you can't open it in VS Designer.
                            >
                            >
                            > Thanks,
                            > Tee
                            >
                            >
                            > "Dennis Myrén" <dennis@oslokb. no> wrote in message
                            > news:dN1Kd.6183 $Sl3.148441@new s4.e.nsc.no...[color=green]
                            >> Why not just(shortened) :
                            >>
                            >> abstract class Abstract
                            >> {
                            >> public abstract void MustOverrideThi sAbstractMethod ( ) ;
                            >> }
                            >>
                            >> class Concrete : Abstract
                            >> {
                            >> override public void MustOverrideThi sAbstractMethod ( )
                            >> {
                            >> //Implement something here.
                            >> }
                            >>
                            >> }
                            >>
                            >> Declaring a method as abstract within an abstract class means the base[/color]
                            > class[color=green]
                            >> will demand an implementation of that method from the concrete derived
                            >> class.
                            >>
                            >>
                            >> --
                            >> Regards,
                            >> Dennis JD Myrén
                            >> Oslo Kodebureau
                            >> "Tee" <thy@streamyx.c om> wrote in message
                            >> news:unWQZ0BBFH A.380@TK2MSFTNG P10.phx.gbl...[color=darkred]
                            >> > Hi,
                            >> >
                            >> > I have a base usercontrol with a method (blank method, no code), I have
                            >> > another few usercontrols that will inherit this base usercontrol, but I
                            >> > want
                            >> > to force all the usercontrol that inheriting this base usercontrol to
                            >> > override the method with its own code. How do I do it?
                            >> >
                            >> > I have tried to make the base usercontrol an abstract class[/color][/color]
                            > (mustinherits[color=green][color=darkred]
                            >> > +
                            >> > mustoverrides), this works, but all the usercontrols that inherit it[/color][/color]
                            > will[color=green][color=darkred]
                            >> > not able to open in VS designer because of inherting an abstract class.
                            >> >
                            >> > I have also tried to use interface, this works as well, but I can't[/color][/color]
                            > force[color=green][color=darkred]
                            >> > those usercontrols that inherit base usercontrol MUST have the[/color][/color]
                            > interface.[color=green][color=darkred]
                            >> >
                            >> > Anyone has a solution for this?
                            >> >
                            >> >
                            >> > Thanks,
                            >> > Tee
                            >> >
                            >> >[/color]
                            >>
                            >>[/color]
                            >
                            >[/color]


                            Comment

                            • Dennis Myrén

                              #15
                              Re: Need help: about OOP inheritance/abstract class

                              Sorry, i should probably read the posts fully before replying them.

                              I had a bunch of webforms once which all needed common extra features in
                              order
                              to provide the functionality of a step in a wizard.
                              There was no way i could force those webforms to implement my custom
                              interface
                              in addition to inherit System.Web.UI.P age.
                              But i myself though, knew which ones of them should provide that
                              functionality,
                              and had them implement that interface.
                              class WizardStep1 : System.Web.UI.P age, IWizardStep



                              --
                              Regards,
                              Dennis JD Myrén
                              Oslo Kodebureau
                              "Tee" <thy@streamyx.c om> wrote in message
                              news:%23K7ha9EB FHA.2624@TK2MSF TNGP11.phx.gbl. ..[color=blue]
                              > Hi Dennis,
                              >
                              > See my previous post:[color=green]
                              >>I have tried to make the base usercontrol an abstract class (mustinherits
                              >>+[/color]
                              > mustoverrides), this works, but all the usercontrols that inherit it will
                              > not able to open in VS designer because of inherting an abstract class.
                              >
                              > This just won't work on forms or usercontrols ... it won't effect runtime,
                              > but it's trouble enough if you can't open it in VS Designer.
                              >
                              >
                              > Thanks,
                              > Tee
                              >
                              >
                              > "Dennis Myrén" <dennis@oslokb. no> wrote in message
                              > news:dN1Kd.6183 $Sl3.148441@new s4.e.nsc.no...[color=green]
                              >> Why not just(shortened) :
                              >>
                              >> abstract class Abstract
                              >> {
                              >> public abstract void MustOverrideThi sAbstractMethod ( ) ;
                              >> }
                              >>
                              >> class Concrete : Abstract
                              >> {
                              >> override public void MustOverrideThi sAbstractMethod ( )
                              >> {
                              >> //Implement something here.
                              >> }
                              >>
                              >> }
                              >>
                              >> Declaring a method as abstract within an abstract class means the base[/color]
                              > class[color=green]
                              >> will demand an implementation of that method from the concrete derived
                              >> class.
                              >>
                              >>
                              >> --
                              >> Regards,
                              >> Dennis JD Myrén
                              >> Oslo Kodebureau
                              >> "Tee" <thy@streamyx.c om> wrote in message
                              >> news:unWQZ0BBFH A.380@TK2MSFTNG P10.phx.gbl...[color=darkred]
                              >> > Hi,
                              >> >
                              >> > I have a base usercontrol with a method (blank method, no code), I have
                              >> > another few usercontrols that will inherit this base usercontrol, but I
                              >> > want
                              >> > to force all the usercontrol that inheriting this base usercontrol to
                              >> > override the method with its own code. How do I do it?
                              >> >
                              >> > I have tried to make the base usercontrol an abstract class[/color][/color]
                              > (mustinherits[color=green][color=darkred]
                              >> > +
                              >> > mustoverrides), this works, but all the usercontrols that inherit it[/color][/color]
                              > will[color=green][color=darkred]
                              >> > not able to open in VS designer because of inherting an abstract class.
                              >> >
                              >> > I have also tried to use interface, this works as well, but I can't[/color][/color]
                              > force[color=green][color=darkred]
                              >> > those usercontrols that inherit base usercontrol MUST have the[/color][/color]
                              > interface.[color=green][color=darkred]
                              >> >
                              >> > Anyone has a solution for this?
                              >> >
                              >> >
                              >> > Thanks,
                              >> > Tee
                              >> >
                              >> >[/color]
                              >>
                              >>[/color]
                              >
                              >[/color]


                              Comment

                              Working...