why use the 'sealed' ?

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

    why use the 'sealed' ?

    any better reason ?

    --
    FireCrow Studio
    Kylin Garden
    EMail:gaotianpu @gmail.com
    ICQ:156134382


  • Jon Skeet [C# MVP]

    #2
    Re: why use the 'sealed' ?

    Kylin <gaotianpu@gmai l.com> wrote:[color=blue]
    > any better reason ?[/color]

    Any better reason than what?

    Use sealed when either you want to override a property/method but make
    sure that it can't be further overridden, or when you want to make sure
    that a class can't be derived from.

    --
    Jon Skeet - <skeet@pobox.co m>
    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

    If replying to the group, please do not mail me too

    Comment

    • Yunus Emre ALPÖZEN [MCAD.NET]

      #3
      Re: why use the 'sealed' ?

      Suppose that you have something like this;

      public abstract class anAbstractClass
      {
      public abstract void someMethod();
      public abstract void anotherMethod() ;
      }

      public class FirstDerivative Class : anAbstractClass
      {
      public sealed override void someMethod()
      {
      }
      public override void anotherMethod()
      {
      }
      }

      public class SecondDerivativ eClass : FirstDerivative Class
      {
      public override void anotherMethod()
      {
      }
      }
      public sealed class ThirdDerivative Class : SecondDerivativ eClass
      {
      public override void anotherMethod()
      {
      }
      }


      Now you have an abstract class and two abstract methods. At
      FirstDerivative Class, you implement someMethod() and do not want to anybody
      to override it. You define it as sealed method. At SecondDerivativ eClass you
      can not override someMethod() but can override anotherMethod. At
      ThirdDerivative Class you still can override anotherMethod. But you guarantee
      that there can not be a fourth derivative class by defining that class as a
      sealed. Is it clear?
      --

      Thanks,
      Yunus Emre ALPÖZEN
      BSc, MCAD.NET

      "Kylin" <gaotianpu@gmai l.com> wrote in message
      news:ObC0Ht5UFH A.3544@TK2MSFTN GP12.phx.gbl...[color=blue]
      > any better reason ?
      >
      > --
      > FireCrow Studio
      > Kylin Garden
      > EMail:gaotianpu @gmail.com
      > ICQ:156134382
      >
      >[/color]


      Comment

      • Alan Samet

        #4
        Re: why use the 'sealed' ?

        As a general rule, I avoid sealing my classes. It seems that invariably
        I'll want to add functionality to something at a later date. I'm not
        even sure that I like the idea that methods and classes can be
        nonvirtual. And I really don't like the fact that static members are
        always final.

        While I'm about to do some complaining about certain .NET things, I'm
        going to introduce it by saying that I believe .NET is a great product
        for something still in its first version. The guys at Microsoft did a
        great job.

        I sortof wish you could add functionality to (or replace members of)
        existing classes in C# like you can in languages like Ruby. -- like
        being able to add the Left/Right methods to the string class for the
        scope of an application, or replacing the Substring function with
        something that doesn't throw an exception when the Length parameter
        exceeds past the end of the string.

        I kicked the air a few times when I noticed how many members of the
        ADO.NET namespaces were sealed -- members like SqlDataAdapter. If it
        were not sealed, and say, the "Fill" method was marked as virtual, you
        could easily write some great diagnostic tools.

        For instance, say you wanted the ability to view all of the raw
        datasets going to an application. All that would be necessary would be
        to subclass the SqlDataAdapter class, override the Fill method with
        something that would, say, raise an event with your data. Then write a
        simple windows application that spawned a window with a datagrid
        populated with that data every time Fill was called. Have that windows
        application act as a Remoting Server for your application, and set the
        application to be a Remoting Client. When you're done diagnosing,
        simply remove your remoting configuration.

        -Alan

        Kylin wrote:[color=blue]
        > any better reason ?
        >
        > --
        > FireCrow Studio
        > Kylin Garden
        > EMail:gaotianpu @gmail.com
        > ICQ:156134382[/color]

        Comment

        • Jon Skeet [C# MVP]

          #5
          Re: why use the 'sealed' ?

          Alan Samet <alansamet@gmai l.com> wrote:[color=blue]
          > As a general rule, I avoid sealing my classes. It seems that invariably
          > I'll want to add functionality to something at a later date. I'm not
          > even sure that I like the idea that methods and classes can be
          > nonvirtual.[/color]

          Personally, I love it. Designing classes to be derived from properly is
          a lot of work, and shouldn't be undertaken when it's not necessary. To
          be honest, I wish classes were sealed by default.
          [color=blue]
          > And I really don't like the fact that static members are
          > always final.[/color]

          Well, how would you expect polymorphism to work with them, seeing as
          you can't invoke a static method on an instance anyway? Which static
          method is invoked is a compile-time decision, so it doesn't make sense
          for them to be virtual.
          [color=blue]
          > While I'm about to do some complaining about certain .NET things, I'm
          > going to introduce it by saying that I believe .NET is a great product
          > for something still in its first version. The guys at Microsoft did a
          > great job.
          >
          > I sortof wish you could add functionality to (or replace members of)
          > existing classes in C# like you can in languages like Ruby. -- like
          > being able to add the Left/Right methods to the string class for the
          > scope of an application, or replacing the Substring function with
          > something that doesn't throw an exception when the Length parameter
          > exceeds past the end of the string.[/color]

          Sounds dangerous to me - sounds like the kind of thing which could
          easily introduce unwanted and possibly insecure behaviour. For
          instance, I know that when I use a string I can call any method and it
          won't change the contents of the string. If one piece of code could
          change the implementation of string, all the rest of my code would have
          to take that into account.
          [color=blue]
          > I kicked the air a few times when I noticed how many members of the
          > ADO.NET namespaces were sealed -- members like SqlDataAdapter. If it
          > were not sealed, and say, the "Fill" method was marked as virtual, you
          > could easily write some great diagnostic tools.[/color]

          But then the situations in which Fill is called for you from other
          methods would have to be documented, and would then be set in stone,
          etc. Like I said, designing for derivation is time-consuming.
          [color=blue]
          > For instance, say you wanted the ability to view all of the raw
          > datasets going to an application. All that would be necessary would be
          > to subclass the SqlDataAdapter class, override the Fill method with
          > something that would, say, raise an event with your data. Then write a
          > simple windows application that spawned a window with a datagrid
          > populated with that data every time Fill was called. Have that windows
          > application act as a Remoting Server for your application, and set the
          > application to be a Remoting Client. When you're done diagnosing,
          > simply remove your remoting configuration.[/color]

          I can see how it's useful in certain situations, but I think the
          downsides outweigh the upsides in general. Of course, with a proper AOP
          system in place, you could have the above without making Fill itself
          virtual.

          --
          Jon Skeet - <skeet@pobox.co m>
          Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

          If replying to the group, please do not mail me too

          Comment

          • Bob Powell [MVP]

            #6
            Re: why use the 'sealed' ?

            >>I wish classes were sealed by default

            That goes completely against the grain of oop concepts. The whole idea of a
            system that supports inheritance and polymorphism is to promote reusability.
            Sealing a class is an architectural descision taken when a provider wants to
            prohibit that process even when a potential consumer of the code sees a need
            to derive from it. It's not one that should be taken lightly or left as a
            default case because that would mean that many classes would leave the
            factory floor with unintentional sealing in place. The sealed keyword must
            be explicit because it's connotations are so far reacing.

            Like Alan I think that the sealed state of many of the .NET classes are an
            architectural nightmare that prohibits people from using classes that are so
            deficient that they need overriding just to make sense of them.

            --
            Bob Powell [MVP]
            Visual C#, System.Drawing

            Find great Windows Forms articles in Windows Forms Tips and Tricks


            Answer those GDI+ questions with the GDI+ FAQ


            All new articles provide code in C# and VB.NET.
            Subscribe to the RSS feeds provided and never miss a new article.





            "Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
            news:MPG.1ce887 beddaa06fc98c0c 5@msnews.micros oft.com...[color=blue]
            > Alan Samet <alansamet@gmai l.com> wrote:[color=green]
            >> As a general rule, I avoid sealing my classes. It seems that invariably
            >> I'll want to add functionality to something at a later date. I'm not
            >> even sure that I like the idea that methods and classes can be
            >> nonvirtual.[/color]
            >
            > Personally, I love it. Designing classes to be derived from properly is
            > a lot of work, and shouldn't be undertaken when it's not necessary. To
            > be honest, I wish classes were sealed by default.
            >[color=green]
            >> And I really don't like the fact that static members are
            >> always final.[/color]
            >
            > Well, how would you expect polymorphism to work with them, seeing as
            > you can't invoke a static method on an instance anyway? Which static
            > method is invoked is a compile-time decision, so it doesn't make sense
            > for them to be virtual.
            >[color=green]
            >> While I'm about to do some complaining about certain .NET things, I'm
            >> going to introduce it by saying that I believe .NET is a great product
            >> for something still in its first version. The guys at Microsoft did a
            >> great job.
            >>
            >> I sortof wish you could add functionality to (or replace members of)
            >> existing classes in C# like you can in languages like Ruby. -- like
            >> being able to add the Left/Right methods to the string class for the
            >> scope of an application, or replacing the Substring function with
            >> something that doesn't throw an exception when the Length parameter
            >> exceeds past the end of the string.[/color]
            >
            > Sounds dangerous to me - sounds like the kind of thing which could
            > easily introduce unwanted and possibly insecure behaviour. For
            > instance, I know that when I use a string I can call any method and it
            > won't change the contents of the string. If one piece of code could
            > change the implementation of string, all the rest of my code would have
            > to take that into account.
            >[color=green]
            >> I kicked the air a few times when I noticed how many members of the
            >> ADO.NET namespaces were sealed -- members like SqlDataAdapter. If it
            >> were not sealed, and say, the "Fill" method was marked as virtual, you
            >> could easily write some great diagnostic tools.[/color]
            >
            > But then the situations in which Fill is called for you from other
            > methods would have to be documented, and would then be set in stone,
            > etc. Like I said, designing for derivation is time-consuming.
            >[color=green]
            >> For instance, say you wanted the ability to view all of the raw
            >> datasets going to an application. All that would be necessary would be
            >> to subclass the SqlDataAdapter class, override the Fill method with
            >> something that would, say, raise an event with your data. Then write a
            >> simple windows application that spawned a window with a datagrid
            >> populated with that data every time Fill was called. Have that windows
            >> application act as a Remoting Server for your application, and set the
            >> application to be a Remoting Client. When you're done diagnosing,
            >> simply remove your remoting configuration.[/color]
            >
            > I can see how it's useful in certain situations, but I think the
            > downsides outweigh the upsides in general. Of course, with a proper AOP
            > system in place, you could have the above without making Fill itself
            > virtual.
            >
            > --
            > Jon Skeet - <skeet@pobox.co m>
            > http://www.pobox.com/~skeet
            > If replying to the group, please do not mail me too[/color]


            Comment

            • Alan Samet

              #7
              Re: why use the 'sealed' ?

              -- snip --[color=blue][color=green]
              > > [Alan Samet] For instance, say you wanted the ability to view all[/color][/color]
              of the raw[color=blue][color=green]
              > > datasets going to an application. All that would be necessary would[/color][/color]
              be[color=blue][color=green]
              > > to subclass the SqlDataAdapter class, override the Fill method with
              > > something that would, say, raise an event with your data. Then[/color][/color]
              write a[color=blue][color=green]
              > > simple windows application that spawned a window with a datagrid
              > > populated with that data every time Fill was called. Have that[/color][/color]
              windows[color=blue][color=green]
              > > application act as a Remoting Server for your application, and set[/color][/color]
              the[color=blue][color=green]
              > > application to be a Remoting Client. When you're done diagnosing,
              > > simply remove your remoting configuration.[/color]
              >[/color]

              Jon Skeet [ C# MVP ] wrote:[color=blue]
              > I can see how it's useful in certain situations, but I think the
              > downsides outweigh the upsides in general. Of course, with a proper[/color]
              AOP[color=blue]
              > system in place, you could have the above without making Fill itself
              > virtual.[/color]

              I couldn't agree with you more on this.

              On a side note, it looks as if Boo will provide a lot of the stuff that
              I'd like to see in a .NET language:



              -Alan

              Comment

              • Jeff Louie

                #8
                Re: why use the 'sealed' ?

                Hi Bob.... Here is some info on what Jon is talking about.

                Regards,
                Jeff[color=blue]
                >That goes completely against the grain of oop concepts.<[/color]

                *** Sent via Developersdex http://www.developersdex.com ***

                Comment

                • Alan Pretre

                  #9
                  Re: why use the 'sealed' ?

                  "Kylin" <gaotianpu@gmai l.com> wrote in message
                  news:ObC0Ht5UFH A.3544@TK2MSFTN GP12.phx.gbl...[color=blue]
                  > any better reason ?[/color]

                  If a method (or a set of methods) provides a contract (explicit or de facto)
                  to a consumer that it will behave in a certain way, with certain
                  side-effects, and these results, then with inheritance the original method
                  loses the ability to make that guarantee when something else comes along and
                  inserts itself in its place. At this point all bets are off and the
                  original guarantee to the consumer is no longer valid. If the loss of this
                  guarantee would be of too great a consequence to the consumer then the class
                  designer can seal the class to prevent this situation.

                  -- Alan


                  Comment

                  • Jon Skeet [C# MVP]

                    #10
                    Re: why use the 'sealed' ?

                    Bob Powell [MVP] <bob@_spamkille r_bobpowell.net > wrote:[color=blue][color=green][color=darkred]
                    > >>I wish classes were sealed by default[/color][/color]
                    >
                    > That goes completely against the grain of oop concepts.[/color]

                    Why? It wouldn't change what was possible at all - just the default.
                    [color=blue]
                    > The whole idea of a system that supports inheritance and polymorphism
                    > is to promote reusability.[/color]

                    Well, that's certainly *part* of the idea, IMO. There's more to it than
                    just that, and I don't see why making things sealed by default would
                    cause problems there.

                    I very rarely derive from another of my classes which I haven't
                    explicitly considered suitable for derivation to start with. It creates
                    all kinds of headaches.
                    [color=blue]
                    > Sealing a class is an architectural descision taken when a provider wants to
                    > prohibit that process even when a potential consumer of the code sees a need
                    > to derive from it. It's not one that should be taken lightly or left as a
                    > default case because that would mean that many classes would leave the
                    > factory floor with unintentional sealing in place. The sealed keyword must
                    > be explicit because it's connotations are so far reacing.[/color]

                    Whereas I see leaving things unsealed as having far reaching
                    implications.
                    [color=blue]
                    > Like Alan I think that the sealed state of many of the .NET classes are an
                    > architectural nightmare that prohibits people from using classes that are so
                    > deficient that they need overriding just to make sense of them.[/color]

                    Those are problems with the classes' design in other ways than being
                    sealed though. Deriving from a class to fix design flaws is hardly
                    elegant.

                    --
                    Jon Skeet - <skeet@pobox.co m>
                    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

                    If replying to the group, please do not mail me too

                    Comment

                    Working...