Inheritance in object-oriented programming

3

I was reading this article today and I came across the following statement:

  

"Inheritance refers to the ability of an object to access methods and other properties of another object."

Inheritance would not be the hierarchical relationship between classes? Where they can inherit properties and methods from other classes and thus pass them to their instances (objects).

The relationship where objects access properties and methods of other objects are described in the Association concepts (where one object is considered as an attribute of another), Aggregation object-parent gets another object in one of its methods) and Composition (where an object of another class is instantiated within the parent object's own class), right?

Can anyone complement this statement?

    
asked by anonymous 15.12.2017 / 17:21

1 answer

5

I'll start by saying what I always say. Almost no one knows object orientation properly, which includes me, but I am making a huge effort to resolve the pending parts . And look I've been using it for about 30 years.

Almost everyone abuses inheritance . People find that something has a hierarchical relationship when it does not . Most objects are composed by other objects and are not derived from other objects. And almost 100% of the examples of inheritance in books, blogs, and other sources show inheritance in something that has no hierarchical relationship, which makes almost everyone learn wrong. And misinformation is widespread as people are not questioning.

In this specific case the phrase was probably said in the context of JavaScript that does not have the explicit declaration of types but rather objects that serve prototypes for other objects. The phrase is unfortunate because it gives to interpret of another form. An object accessing property of another object is possible on every object that has behavior, even without having an inheritance relation.

Inheritance is the hierarchical relationship, made up of the ability of the derived type to have everything the base type has, (subclass), either in contracts (subtype) .

Then an object derived from another has the same characteristics as the base object (its prototype), everything is in it, it does not have to access another object. The inheritance is given by copy. In the new derived object you can add or even change certain characteristics (not all languages allow this). If I change the characteristics, in my conception, it ends with the inheritance since the son is no longer able to perform everything like his father. In the real world the inheritance would hold, but in the computer this becomes complicated. There are those who think that the inheritance remains.

Note that we are talking about only one object, so the word "other" does not fit there anymore. You use another concrete template object for this object, but it ends the relationship between them the moment the new one is created, you do not have to access anything in the other. Totally dynamic languages usually do this.

In class-based languages it works the same, except that the model is a plant and not a prototype, so there is no relation to another object or even at the time of its creation.

Inheritance is not about classes, this is one more mistake that almost everyone commits. There is inheritance without class. Otherwise, the definition of AP is more correct than that of the article. Class is a condition of inheritance.

This also has nothing to do with Association, Aggregation, Composition . These are powerful mechanisms for composing the object, but there is no direct relation to access, that is circumstantial.

Strictly the phrase is wrong, but you can understand why. Of course, for laymen it is a disinformation that helps most people not understand OOP, and everything that people do not understand, but they think they understand, they often go dexterous. And all that is deified "can not be contested" by her or other people. So in programming we have to study even theology: P

Some people may disagree because there are different schools than object-oriented. I said the one that seems to be more used. Some people pick up some concepts from one school and others from another school. And they even impose conflicting concepts in their conception. And I'm talking about people who are at a professional level above.

Related: What are the concepts of cohesion and coupling? (this is more important than OO, the DRY as well). See also What is paradigm? .

    
15.12.2017 / 18:02