Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 say that I can recall a time where inheritance solved a problem that couldn't be solved another way. On the other hand I can recall many times when I used inheritance, because I felt like I had to or because I though I was clever and ended up paying for it.

I can't really see any circumstances where, from an implementation stand point, aggregation or another technique could not be used instead of inheritance.

My only caveat to this is that we would still allow inheritance of interfaces.

(Update)

Let's give an example of why it's needed instead of saying, "sometimes it's just needed." That really isn't helpful at all. Where is your proof?

(Update 2 Code Example)

Here's the classic shape example, more powerful, and more explicit IMO, without inheritance. It is almost never the case in the real world that something really "Is a" of something else. Almost always "Is Implemented in Terms of" is more accurate.

public interface IShape
{
    void Draw();
}

public class BasicShape : IShape
{
    public void Draw()
    {
        // All shapes in this system have a dot in the middle except squares.
        DrawDotInMiddle();
    }
}

public class Circle : IShape
{
    private BasicShape _basicShape;

    public void Draw()
    {
        // Draw the circle part
        DrawCircle();
        _basicShape.Draw();
    }
}

public class Square : IShape
{
    private BasicShape _basicShape;

    public void Draw()
    {
        // Draw the circle part
        DrawSquare();
    }
}
like image 550
John Sonmez Avatar asked Dec 11 '08 23:12

John Sonmez


People also ask

Should I use inheritance or interface?

We can inherit lesser classes than Interface if we use Inheritance. We can inherit enormously more classes than Inheritance, if we use Interface. Methods can be defined inside the class in case of Inheritance. Methods cannot be defined inside the class in case of Interface (except by using static and default keywords).

What will happen if we remove interface in Java?

In general, you cannot decouple actual implementation without interfaces, having just an abstract or partially implemented class already tightly couples you to that partial implementation and probably its details.

Which programming language does not support inheritance?

Which language does not support all 4 types of inheritance? Explanation: Java doesn't support all 4 types of inheritance. It doesn't support multiple inheritance. But the multiple inheritance can be implemented using interfaces in Java.

What is the advantage of interface over inheritance?

One key advantage of interfaces in a single inheritance language is that interfaces can be implemented on classes that do not share a common root. Another point is that interfaces allow what is known as interface inheritance rather than implementation inheritance.


1 Answers

I blogged about this as a wacky idea a while ago.

I don't think it should be removed, but I think classes should be sealed by default to discourage inheritance when it's not appropriate. It's a powerful tool to have available, but it's like a chain-saw - you really don't want to use it unless it's the perfect tool for the job. Otherwise you might start losing limbs.

The are potential language features such as mix-ins which would make it easier to live without, IMO.

like image 102
Jon Skeet Avatar answered Nov 11 '22 21:11

Jon Skeet