Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Inheritence

Inheritence

Scheduled Pinned Locked Moved C#
question
14 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    sris 426
    wrote on last edited by
    #1

    Dear all, I have one base class say BaseOne in that I have 2 methods.One method should not be override by the other method.How can i achive this. Thanks, Srinivas Mateti

    T E P 3 Replies Last reply
    0
    • S sris 426

      Dear all, I have one base class say BaseOne in that I have 2 methods.One method should not be override by the other method.How can i achive this. Thanks, Srinivas Mateti

      T Offline
      T Offline
      The Man from U N C L E
      wrote on last edited by
      #2

      I assume you mean that one method can be overridden and the other cannot, as you cannot override a method in the same class. Nothing can stop anyone overloading the method however. On that assumption mark the overridable method as Virtual and the not overridable one as sealed.

      If you have knowledge, let others light their candles at it. Margaret Fuller (1810 - 1850) www.JacksonSoft.co.uk

      S 1 Reply Last reply
      0
      • S sris 426

        Dear all, I have one base class say BaseOne in that I have 2 methods.One method should not be override by the other method.How can i achive this. Thanks, Srinivas Mateti

        E Offline
        E Offline
        Eduard Keilholz
        wrote on last edited by
        #3

        Erhm, by 'the other mothod', do you mean method 2 in the same class? If you want the method not to be overridable from the class that ionherits your class you may want to take al look at the protected[^] keyword.

        .: I love it when a plan comes together :. http://www.zonderpunt.nl

        D 1 Reply Last reply
        0
        • E Eduard Keilholz

          Erhm, by 'the other mothod', do you mean method 2 in the same class? If you want the method not to be overridable from the class that ionherits your class you may want to take al look at the protected[^] keyword.

          .: I love it when a plan comes together :. http://www.zonderpunt.nl

          D Offline
          D Offline
          dan sh
          wrote on last edited by
          #4

          Virtual is the keyword that makes a method overridable. Even a protected virtual method can be overidden in the derived class.

          It's not necessary to be so stupid, either, but people manage it. - Christian Graus, 2009 AD

          1 Reply Last reply
          0
          • T The Man from U N C L E

            I assume you mean that one method can be overridden and the other cannot, as you cannot override a method in the same class. Nothing can stop anyone overloading the method however. On that assumption mark the overridable method as Virtual and the not overridable one as sealed.

            If you have knowledge, let others light their candles at it. Margaret Fuller (1810 - 1850) www.JacksonSoft.co.uk

            S Offline
            S Offline
            sris 426
            wrote on last edited by
            #5

            I am extreamly sorry..one method can be override by the child class and the other method should not be override the child class... Please see the correction and sorry for the mistake. Thanks, Srinivas Mateti

            D 1 Reply Last reply
            0
            • S sris 426

              I am extreamly sorry..one method can be override by the child class and the other method should not be override the child class... Please see the correction and sorry for the mistake. Thanks, Srinivas Mateti

              D Offline
              D Offline
              dan sh
              wrote on last edited by
              #6

              Mark the method you want to override as virtual. And do not mark the other. Like:

              public virtual void OverridableMethod();
              public void NonOverridableMethod();

              It's not necessary to be so stupid, either, but people manage it. - Christian Graus, 2009 AD

              S 1 Reply Last reply
              0
              • D dan sh

                Mark the method you want to override as virtual. And do not mark the other. Like:

                public virtual void OverridableMethod();
                public void NonOverridableMethod();

                It's not necessary to be so stupid, either, but people manage it. - Christian Graus, 2009 AD

                S Offline
                S Offline
                sris 426
                wrote on last edited by
                #7

                Thanks for the reply...so from your reply what i understood is 1) Virtual methods should be override by the child class,if the child class doest implement the virtual methods then error will shown up. 2) And only virtual methods can be override by the child class non virual methods can not be implemented by child class One more q: Can I again declare virtual method as virtual in child class??? Thanks in advance, Srinivas Mateti

                D T 2 Replies Last reply
                0
                • S sris 426

                  Thanks for the reply...so from your reply what i understood is 1) Virtual methods should be override by the child class,if the child class doest implement the virtual methods then error will shown up. 2) And only virtual methods can be override by the child class non virual methods can not be implemented by child class One more q: Can I again declare virtual method as virtual in child class??? Thanks in advance, Srinivas Mateti

                  D Offline
                  D Offline
                  dan sh
                  wrote on last edited by
                  #8

                  sris 426 wrote:

                  if the child class doest implement the virtual methods then error will shown up.

                  No.

                  sris 426 wrote:

                  And only virtual methods can be override by the child class non virual methods can not be implemented by child class

                  Correct. Although I assume you mean overridden when you say implement.

                  sris 426 wrote:

                  Can I again declare virtual method as virtual in child class???

                  Yes they can.

                  It's not necessary to be so stupid, either, but people manage it. - Christian Graus, 2009 AD

                  1 Reply Last reply
                  0
                  • S sris 426

                    Dear all, I have one base class say BaseOne in that I have 2 methods.One method should not be override by the other method.How can i achive this. Thanks, Srinivas Mateti

                    P Offline
                    P Offline
                    PIEBALDconsult
                    wrote on last edited by
                    #9

                    As others have answered, you can do that. But I wonder why not allow overriding of both methods? Is this homework?

                    1 Reply Last reply
                    0
                    • S sris 426

                      Thanks for the reply...so from your reply what i understood is 1) Virtual methods should be override by the child class,if the child class doest implement the virtual methods then error will shown up. 2) And only virtual methods can be override by the child class non virual methods can not be implemented by child class One more q: Can I again declare virtual method as virtual in child class??? Thanks in advance, Srinivas Mateti

                      T Offline
                      T Offline
                      The Man from U N C L E
                      wrote on last edited by
                      #10

                      To clarify: sealed - Specifies that a method cannot be overriden. abstract - Specifies that a method must be implemented in a derived class. virtual - Specifies that a member may be overriden, but does not have to be. Note that if your class includes abstract members then the class itself must also be marked as abstract. http://www.dnzone.com/go?356[^] lists all the permutations of sealed, abstract etc. for c# and vb.Net. A virtual method will still be virtual in a sub-class of a sub-class unless you mark is as override sealed in the class in the middle, which then stops classes further down the inheitance tree from overriding that method. A sub-class can always declare additional methods which may or may not be overridable in turn, as you desire.

                      If you have knowledge, let others light their candles at it. Margaret Fuller (1810 - 1850) www.JacksonSoft.co.uk

                      S 1 Reply Last reply
                      0
                      • T The Man from U N C L E

                        To clarify: sealed - Specifies that a method cannot be overriden. abstract - Specifies that a method must be implemented in a derived class. virtual - Specifies that a member may be overriden, but does not have to be. Note that if your class includes abstract members then the class itself must also be marked as abstract. http://www.dnzone.com/go?356[^] lists all the permutations of sealed, abstract etc. for c# and vb.Net. A virtual method will still be virtual in a sub-class of a sub-class unless you mark is as override sealed in the class in the middle, which then stops classes further down the inheitance tree from overriding that method. A sub-class can always declare additional methods which may or may not be overridable in turn, as you desire.

                        If you have knowledge, let others light their candles at it. Margaret Fuller (1810 - 1850) www.JacksonSoft.co.uk

                        S Offline
                        S Offline
                        sris 426
                        wrote on last edited by
                        #11

                        Thanks for the reply... Can I use sealed for methods....? I think I can not use sealed for methods...When declared sealed for methods it gave error... PLease confirm... Thanks, Srinivas Mateti

                        T 1 Reply Last reply
                        0
                        • S sris 426

                          Thanks for the reply... Can I use sealed for methods....? I think I can not use sealed for methods...When declared sealed for methods it gave error... PLease confirm... Thanks, Srinivas Mateti

                          T Offline
                          T Offline
                          The Man from U N C L E
                          wrote on last edited by
                          #12

                          As I said, if you mark a method as override sealed in the class in the middle, it then stops classes further down the inheitance tree from overriding that method. So sealed can be used on a method as shown below. secondLevelClass cannot override MyMethod when inheriting from subclasstwo, but can if it inherits from subclassone, as you will see from the error if you try to compile the code below.

                          public class baseclass{
                          	public virtual void MyMethod(){
                          	}
                          }
                          
                          public class subclassOne : baseclass{
                          	public override void MyMethod()
                          	{
                          		base.MyMethod();
                          	}
                          }
                          
                          public class subclassTwo : baseclass{
                          	public sealed override void MyMethod()
                          	{
                          		base.MyMethod();
                          	}
                          }
                          
                          
                          public class secondlevelClass : subclassTwo{
                          	public override void MyMethod()
                          	{
                          		base.MyMethod();
                          	}
                          }
                          

                          If you have knowledge, let others light their candles at it. Margaret Fuller (1810 - 1850) www.JacksonSoft.co.uk

                          P S 2 Replies Last reply
                          0
                          • T The Man from U N C L E

                            As I said, if you mark a method as override sealed in the class in the middle, it then stops classes further down the inheitance tree from overriding that method. So sealed can be used on a method as shown below. secondLevelClass cannot override MyMethod when inheriting from subclasstwo, but can if it inherits from subclassone, as you will see from the error if you try to compile the code below.

                            public class baseclass{
                            	public virtual void MyMethod(){
                            	}
                            }
                            
                            public class subclassOne : baseclass{
                            	public override void MyMethod()
                            	{
                            		base.MyMethod();
                            	}
                            }
                            
                            public class subclassTwo : baseclass{
                            	public sealed override void MyMethod()
                            	{
                            		base.MyMethod();
                            	}
                            }
                            
                            
                            public class secondlevelClass : subclassTwo{
                            	public override void MyMethod()
                            	{
                            		base.MyMethod();
                            	}
                            }
                            

                            If you have knowledge, let others light their candles at it. Margaret Fuller (1810 - 1850) www.JacksonSoft.co.uk

                            P Offline
                            P Offline
                            PIEBALDconsult
                            wrote on last edited by
                            #13

                            Yeah, that's inconsistent. Methods are sealed by default, but you can't specify it unless you're sealing an otherwise virtual method. :rolleyes:

                            1 Reply Last reply
                            0
                            • T The Man from U N C L E

                              As I said, if you mark a method as override sealed in the class in the middle, it then stops classes further down the inheitance tree from overriding that method. So sealed can be used on a method as shown below. secondLevelClass cannot override MyMethod when inheriting from subclasstwo, but can if it inherits from subclassone, as you will see from the error if you try to compile the code below.

                              public class baseclass{
                              	public virtual void MyMethod(){
                              	}
                              }
                              
                              public class subclassOne : baseclass{
                              	public override void MyMethod()
                              	{
                              		base.MyMethod();
                              	}
                              }
                              
                              public class subclassTwo : baseclass{
                              	public sealed override void MyMethod()
                              	{
                              		base.MyMethod();
                              	}
                              }
                              
                              
                              public class secondlevelClass : subclassTwo{
                              	public override void MyMethod()
                              	{
                              		base.MyMethod();
                              	}
                              }
                              

                              If you have knowledge, let others light their candles at it. Margaret Fuller (1810 - 1850) www.JacksonSoft.co.uk

                              S Offline
                              S Offline
                              sris 426
                              wrote on last edited by
                              #14

                              Thanks for the answer..now i am more clear.... Thanks, Srinivas mateti

                              1 Reply Last reply
                              0
                              Reply
                              • Reply as topic
                              Log in to reply
                              • Oldest to Newest
                              • Newest to Oldest
                              • Most Votes


                              • Login

                              • Don't have an account? Register

                              • Login or register to search.
                              • First post
                                Last post
                              0
                              • Categories
                              • Recent
                              • Tags
                              • Popular
                              • World
                              • Users
                              • Groups