Question about polymorphism..
-
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
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...
-
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...
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.
-
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.
You missed the keyword, enforce, what he wants to do is perfectly possible, just not enforcable... :doh:
-
You missed the keyword, enforce, what he wants to do is perfectly possible, just not enforcable... :doh:
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.
-
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.
And what about classes inheriting the 3rd class?
-
You missed the keyword, enforce, what he wants to do is perfectly possible, just not enforcable... :doh:
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.
-
And what about classes inheriting the 3rd class?
-
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.
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
-
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
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'...