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. Question about polymorphism..

Question about polymorphism..

Scheduled Pinned Locked Moved C#
questionoop
10 Posts 3 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.
  • E Offline
    E Offline
    e laj
    wrote on last edited by
    #1

    Is it possible to enforce that abstract method will be implemented by every class in the hierarchy, even by those classes that derived from classes that ALREADY implemented the abstract method? Thanks in advanced. Ilan

    L 1 Reply Last reply
    0
    • E e laj

      Is it possible to enforce that abstract method will be implemented by every class in the hierarchy, even by those classes that derived from classes that ALREADY implemented the abstract method? Thanks in advanced. Ilan

      L Offline
      L Offline
      leppie
      wrote on last edited by
      #2

      e-laj wrote:

      Is it possible to enforce that abstract method will be implemented by every class in the hierarchy, even by those classes that derived from classes that ALREADY implemented the abstract method?

      No. I not aware of any tricks that will allow that either. You can check the assembly via reflection when it gets loaded an throw an Exception perhaps...

      xacc.ide-0.1.3.2

      R 1 Reply Last reply
      0
      • L leppie

        e-laj wrote:

        Is it possible to enforce that abstract method will be implemented by every class in the hierarchy, even by those classes that derived from classes that ALREADY implemented the abstract method?

        No. I not aware of any tricks that will allow that either. You can check the assembly via reflection when it gets loaded an throw an Exception perhaps...

        xacc.ide-0.1.3.2

        R Offline
        R Offline
        ricardojb
        wrote on last edited by
        #3

        public abstract class Class1 { protected abstract void Method1(); } public interface IInterface1 { void Method1(); } public class Class2: Class1 { protected override void Method1() { //.... } public void HelperMethod() { this.Method1(); } } public class Class3 : Class2, IInterface1 { } this would do it, I don't really see why you would to through all this trouble but it will work. Class1 is abstract and declares a protected abstract method that Class2 must implement. Interface1 also requires the same method. Class3 inherits Class2 and the interface, but because the method is protected, it can't see it, so it needs to explicitly implement it because of the interface, additionally, the HelperMethod gives public access to Method1, this way the method is still public but the Method that follows the rules is not visible to the classes down the hierarchy.

        L 1 Reply Last reply
        0
        • R ricardojb

          public abstract class Class1 { protected abstract void Method1(); } public interface IInterface1 { void Method1(); } public class Class2: Class1 { protected override void Method1() { //.... } public void HelperMethod() { this.Method1(); } } public class Class3 : Class2, IInterface1 { } this would do it, I don't really see why you would to through all this trouble but it will work. Class1 is abstract and declares a protected abstract method that Class2 must implement. Interface1 also requires the same method. Class3 inherits Class2 and the interface, but because the method is protected, it can't see it, so it needs to explicitly implement it because of the interface, additionally, the HelperMethod gives public access to Method1, this way the method is still public but the Method that follows the rules is not visible to the classes down the hierarchy.

          L Offline
          L Offline
          leppie
          wrote on last edited by
          #4

          You missed the keyword, enforce, what he wants to do is perfectly possible, just not enforcable... :doh:

          xacc.ide-0.1.3.2

          R 2 Replies Last reply
          0
          • L leppie

            You missed the keyword, enforce, what he wants to do is perfectly possible, just not enforcable... :doh:

            xacc.ide-0.1.3.2

            R Offline
            R Offline
            ricardojb
            wrote on last edited by
            #5

            no, I didn't miss it, using the interface in this context has the sole purpose of enforcing it, that's the only reason to declare the same method in an abstract class and an interface, he is still inheritting from the class he wants to inherits but the compiler will force the developer to implement the method in the third class. And btw, I tested it, if you don't implement the method in the last class you get a compiler error, even though it inherits Class2 that already implemented the method.

            L 1 Reply Last reply
            0
            • R ricardojb

              no, I didn't miss it, using the interface in this context has the sole purpose of enforcing it, that's the only reason to declare the same method in an abstract class and an interface, he is still inheritting from the class he wants to inherits but the compiler will force the developer to implement the method in the third class. And btw, I tested it, if you don't implement the method in the last class you get a compiler error, even though it inherits Class2 that already implemented the method.

              L Offline
              L Offline
              leppie
              wrote on last edited by
              #6

              And what about classes inheriting the 3rd class?

              xacc.ide-0.1.3.2

              R 1 Reply Last reply
              0
              • L leppie

                You missed the keyword, enforce, what he wants to do is perfectly possible, just not enforcable... :doh:

                xacc.ide-0.1.3.2

                R Offline
                R Offline
                ricardojb
                wrote on last edited by
                #7

                No I didn't, try it. what he wants to do is having the compiler enforcing it and the code I posted will just do it. The interface will require the class to implement the method, normally that wouldn't be enough to enforce to rewrite it because it's already implemented by a parent class, but because it's protected and not public or internal, the child class won't see it and therefore the compiler requires the implementation of the interface. I didn't say it but it should be obvious that both methods (in the interface and the abstract class) need to have the exact same signature, that is, same name, same return type and same parameters.

                1 Reply Last reply
                0
                • L leppie

                  And what about classes inheriting the 3rd class?

                  xacc.ide-0.1.3.2

                  R Offline
                  R Offline
                  ricardojb
                  wrote on last edited by
                  #8

                  that wasn't the requirement, you can either declare the third class as sealed (then they can't inherit it) or otherwise continue hidding the methods from the child class by declaring them protected and not public, I haven't tested this though.

                  R 1 Reply Last reply
                  0
                  • R ricardojb

                    that wasn't the requirement, you can either declare the third class as sealed (then they can't inherit it) or otherwise continue hidding the methods from the child class by declaring them protected and not public, I haven't tested this though.

                    R Offline
                    R Offline
                    ricardojb
                    wrote on last edited by
                    #9

                    I missed something important, interface members have to be public and therefore will be visible to the child classes. The answer will be that it will work up to the second generation, not beyond, if it's a requirement that classes have to inherit Class3 then it can't be done through design and needs some other solution. If the requirement is to only inherit from the second class, then all childer classes can be sealed to force to inherit from Class2 or Class1 but not from Class3 and beyond. Reflection would not be a solution because it would enforce it at runtime, not compile time, basically it would simply throw an error if the separate implementation is not there but it would compile fine. -- modified at 23:14 Saturday 1st April, 2006

                    L 1 Reply Last reply
                    0
                    • R ricardojb

                      I missed something important, interface members have to be public and therefore will be visible to the child classes. The answer will be that it will work up to the second generation, not beyond, if it's a requirement that classes have to inherit Class3 then it can't be done through design and needs some other solution. If the requirement is to only inherit from the second class, then all childer classes can be sealed to force to inherit from Class2 or Class1 but not from Class3 and beyond. Reflection would not be a solution because it would enforce it at runtime, not compile time, basically it would simply throw an error if the separate implementation is not there but it would compile fine. -- modified at 23:14 Saturday 1st April, 2006

                      L Offline
                      L Offline
                      leppie
                      wrote on last edited by
                      #10

                      ricardojb wrote:

                      Reflection would not be a solution because it would enforce it at runtime, not compile time, basically it would simply throw an error if the separate implementation is not there but it would compile fine.

                      Yes, that's what I told him. I sure as hell hope a person using the code would try debug it at least once, and get notified on the 'enforcement'...

                      xacc.ide-0.1.3.2

                      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