Is inheritance a bad practice for all languages?

Asked

Viewed 479 times

12

I took a course in architecture of Java projects and the instructor told me that heritage in Java is considered a bad practice, which should always be avoided and is preferable if possible to use composition in place.

This is valid for all other languages or only for Java?

This is valid for multiple inheritance also?

  • Bad practice is to misuse the resource. It itself is not harmful, as long as you know what you are doing and its risks. If it were that bad, no language would give the possibility of use.

  • 4

    Is it bad practice?? I recommend looking for another instructor. And in java it is not possible to use multiple inheritance

  • 1
  • Fernando, would have a scenario where this "instructor" said it is a malpractice? It is very widespread, and difficult to judge only with this. However, if he really said it is because... Better not judge :p

  • It is complicated to consider as "bad practice". Using inheritance has to be very well justified, when we mean that one class is another. It’s easy to get confused. Besides, with inheritance we are violating another part of OOP, which is encapsulation.

  • 1

    He presented several scenarios. I tried to make the question compact, which I think I got wrong for him.

  • 1

    I agree with your instructor. Heritage class is something that creates more problems than it solves and, unless you are forced to use it to make use of some pre-existing functionality that requires it, you at all times can eliminate it and until today I have never seen a case where the form without inheritance was inferior to the form with inheritance - what happens is just the opposite. If such pre-existing functionality that requires inheritance does not exist, then you will never need it and make a project as good or better than without inheritance.

Show 2 more comments

1 answer

28


It’s bad practice to teach things like bad practice or good practice.

The ideal would be to explain why things happen. Often the person does not explain why he does not know (I am not saying that this is the case). It’s common for people to learn from cake recipes. She reads somewhere and starts to repeat that as absolute truth without question, without understanding why to use.

This is a problem of object orientation and not of Java, therefore inherent in all languages that adopt OOP. Curiously the most striking feature of "paradigm" does not work as well as people imagined and only in some cases it really is useful. Often it generates more problems than it generates solutions.

If you want to use that term that I don’t like, yes, it is bad practice, as long as you understand that "practice" is something general that usually has many exceptions. If you use it correctly, where it is useful, there is no problem. If you think that because it is "bad practice" you should not use it, then you are doing it wrong. You have to learn where to use it and where to avoid it. It is a "bad practice" to abuse the resource where it is not necessary and has a better solution (composition and derivatives).

There are some questions here that help understand this:

Multiple inheritance

Multiple inheritance is another problem that aggravates the difficulty. So few language allow.

Imagine how complicated it is for an object to be two things at once. This can occur in biology problems. Even in chemistry things are composed by others and are not other things at the same time.

Besides the conceptual problem there are technical problems. It is difficult to implement a compiler that does this right and will require the language to have ways to resolve certain conflicts that may arise when you inherit from two different entities that may have states or behaviors with the same name. Having a name conflict is the least of the problems, since the conceptual conflict is more important.

One of the biggest problems is the diamond problem. Where two classes (B and C) inherit from a third (A). Then a fourth (D) inherits from these two classes (B and C). Both had the same states and behaviors of A. Which of the two will be used in D?

Diamond Problem

The way to access these limbs becomes complex and probably less performative than expected.

So multiple inheritance should be greatly avoided.

Some more modern languages are avoiding having inheritance, although this brings some difficulties to some few scenarios where inheritance works very well.

  • I gave +1 just to read the first sentence.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.