Should inheritance (of non-interface types) be removed from programming languages?

前端 未结 20 2165
旧时难觅i
旧时难觅i 2020-12-30 05:20

This is quite a controversial topic, and before you say \"no\", is it really, really needed?

I have been programming for about 10 years, and I can\'t honestly sa

相关标签:
20条回答
  • 2020-12-30 05:50

    I wish languages would provide some mechanisms to make it easier to delegate to member variables. For example, suppose interface I has 10 methods, and class C1 implements this interface. Suppose I want to implement class C2 that is just like a C1 but with method m1() overridden. Without using inheritance, I would do this as follows (in Java):

    public class C2 implements I {
        private I c1;
    
        public C2() {
           c1 = new C1();
        }
    
        public void m1() {
            // This is the method C2 is overriding.
        }
    
        public void m2() {
            c1.m2();
        }
    
        public void m3() {
            c1.m3();
        }
    
        ...
    
        public void m10() {
            c1.m10();
        }
    }
    

    In other words, I have to explicitly write code to delegate the behavior of methods m2..m10 to the member variable m1. That's a bit of a pain. It also clutters the code up so that it's harder to see the real logic in class C2. It also means that whenever new methods are added to interface I, I have to explicitly add more code to C1 just to delegate these new methods to C1.

    I wish languages would allow me to say: C1 implements I, but if C1 is missing some method from I, automatically delegate to member variable c1. That would cut down the size of C1 to just

    public class C2 implements I(delegate to c1) {
        private I c1;
    
        public C2() {
           c1 = new C1();
        }
    
        public void m1() {
            // This is the method C2 is overriding.
        }
    }
    

    If languages allowed us to do this, it would be much easier to avoid use of inheritance.

    Here's a blog article I wrote about automatic delegation.

    0 讨论(0)
  • 2020-12-30 05:51

    I guess I have to play the devil's advocate. If we didn't have inheritance then we wouldn't be able to inherit abstract classes that uses the template method pattern. There are lots of examples where this is used in frameworks such as .NET and Java. Thread in Java is such an example:

    // Alternative 1:
    public class MyThread extends Thread {
    
        // Abstract method to implement from Thread
        // aka. "template method" (GoF design pattern)
        public void run() {
            // ...
        }
    }
    
    // Usage:
    MyThread t = new MyThread();
    t.start();
    

    The alternative is, in my meaning, verbose when you have to use it. Visual clutteer complexity goes up. This is because you need to create the Thread before you can actually use it.

    // Alternative 2:
    public class MyThread implements Runnable {
        // Method to implement from Runnable:
        public void run() {
            // ...
        }
    }
    
    // Usage:
    MyThread m = new MyThread();
    Thread t = new Thread(m);
    t.start();
    // …or if you have a curious perversion towards one-liners
    Thread t = new Thread(new MyThread());
    t.start();
    

    Having my devil's advocate hat off I guess you could argue that the gain in the second implementation is dependency injection or seperation of concerns which helps designing testable classes. Depending on your definition of what an interface is (I've heard of at least three) an abstract class could be regarded as an interface.

    0 讨论(0)
提交回复
热议问题