Can a subclass have two superclasses?

Asked

Viewed 1,026 times

5

Suppose I have a superclass Pessoa and another Funcionário, with its certain attributes and methods. My class Professor may be "daughter" of Pessoa and Funcionário, once it fits both? If yes how is it possible? With extends even?

  • Not in Java modeling. Java predicts that each class can only inherit from a single mother class (but can have a grandmother class, great-grandmother class, etc.). I believe that a more affectionate modeling would need to be done, possibly with more classes and interfaces to get into the Java world.

  • 1

    If its structure is based on interface, being a person and working interfaces and not concrete implementations, yes, it is possible. Otherwise, not because java does not allow multiple inheritance.

  • She could inherit from Employee... It would not be necessary to inherit from Person also, given that Employee is a person

  • @Andersonhenrique if official is a sub-class of person...

  • Of course @Jeffersonquesado

  • Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site

Show 1 more comment

2 answers

13

What you’re looking for is called multiple inheritance. While this is certainly a powerful mechanism, multiple inheritance opens doors to new problems (see, for example, that question on the diamond problem).

Java, by a decision of design of the authors of the language, supports only inheritance-simple.

That said, Java allows a class to implement multiple interfaces (and an interface to extend multiple other interfaces). Since Java 8 Interfaces may also contain methods default. Combining Composition and Interfaces is possible to "reuse behavior" without having to deal with much of the disadvantages of multiple-inheritance.

  • hahah thanks for the reply, I delved into the concepts of Heritage and understood the idea, vlw partner

12

Anthony Accioly’s answer answers the question correctly, I will supplement it.

Java has multiple inheritance of subtype, but not subclass, that is, you may have several types in your class, but you cannot have multiple implementations. Actually since Java 8 it is possible to have several behavior implementations (method), but not of state (variable), through standard methods.

Complete multiple inheritance is almost never required and almost always wrong, I can barely remember the last time I saw a case that needed multiple inheritance, I think it was a biology problem. Reuse techniques are useful, but they do not make a type be two complete types, at most having members of two types.

That said, I regret to inform you that almost all examples that teach OOP are weak and wrong and so everyone learns wrong.

Your example is a classic deception. Professor is not a Pessoa and Funcionário, at most it has members of a Person and an Official. Because contrary to what some preach, object orientation cannot model the real world, she can model an abstraction of the real world. Don’t understand Pessoa as a human being, and rather as a token of a person, because that is what that class is. The same of Funcionário. Employee is an abstract concept created by humans, not being concrete it does not exist in the real world. And it’s also a data sheet that a person can have for a function.

If you wanted to insist on modeling as if it were the real world is easy to solve your problem, just hierarchize correctly. Professor inherits from Funcionário who inherits from Pessoa. Ready solved your problem, because all Professor is a Funcionário, right? All Funcionário is a Pessoa, right?

Good, wrong, but hierarchy is a solution for several cases, not this.

At most a Professor can have data and behaviors that all Funcionário and every Person has. So it makes sense to have interfaces or traits (which is more or less what Java 8 introduced) or other options, that is, it has these subtypes, but not subclasses. Until mixins may be useful, but Java does not have one, so the delegation.

Even this is usually wrong. A person can be Professor and Aluno at the same time. A Pessoa can be Funcionário and Cliente at the same time, how do you solve this? Simple, make each part independent. A Pessoa is a person, nothing more. A Funcionário is an official and a Cliente is a customer, nothing more than that and of course a Professor is a teacher and a Aluno is a student and nothing more than this. You do composition between them making a association of the activity that each one is exercising.

And I didn’t even get the credit Cliente can be Pessoa Física or Jurídica.

If you make this inheritance you have more than one person file in the system, the information ceases to be canonical (DRY data) and begins to cause confusion. It seems public IT thing (tribute to Bacco).

Most of the data is related to each other and not inherited. Almost every inheritance should be of subtype, and look there, and partially of behavior. Almost every inheritance should be abstract class or interface (or trait). Then a Pessoa Física or Jurídica inherit from Pessoa, legal, because Person by itself does not exist, is only a basis for the types of people. What exists rarely makes sense to inherit, except in biology, GUI, which is abstract by definition, and some other problems.

Browser other questions tagged

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