-2

I've been doing some studying on the types of inheritance. From what I've learned:

  • Single inheritance refers to when a class inherits another class.
  • Multi-level inheritance refers to when a class inherits another class that inherits another class, etc.
  • Hierarchical inheritances refers to when a super-class that has many sub-classes.

Say you wanted to have B inherit from A. In Java, all classes are ultimately inherited from the Object class. Therefore, A -> B becomes Object -> A -> B.

Now since A inherits Object and Object has many sub classes, is it safe to say that A -> B is single or hierarchical inheritance? Can you have "true" single inheritance in Java?

closed as unclear what you're asking by gnat, BobDalgleish, Andres F., jwenting, David Arno May 28 at 8:46

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 3
    Can you elaborate on why the many subclasses of Object matter to A -> B, as opposed to e.g. Object only being inherited by A? It doesn't, as far as I'm aware, which is why your questions doesn't make much sense (to me). But to answer your question, we need to know precisely what you're trying to point out and why you think that is (or isn't) a problem or meaningful distinction. – Flater May 27 at 7:43
  • 2
    Classes can only extend other classes, although they may also implement interfaces, and in Java, interfaces can have default implementations. Not quite sure what you mean by "true single inheritance". – Neil May 27 at 7:49
  • Java can simulate multiple inheritance with interfaces. – Robert Harvey May 27 at 15:02
  • The crucial Question to answer is whether a Java interface qualifies as class for those definitions, or not. – Deduplicator May 28 at 0:34
  • 1
    "Can you have "true" single inheritance in Java?". Yes, you can have a final class that inherits from Object. That former class is an example of single inheritance. But then you mark an answer that talks about single vs multi inheritance as correct, despite that being completely unrelated to what you appear to be asking in your question. So I now feel that this question is confused and confusing and thus doesn't add value to the site. Voting to close it therefore. – David Arno May 28 at 8:52
7

The concepts that you expose are class-based and not mutually exclusive.

When it is said that Java implements single inheritance, the term single is opposed to multiple and is to be understood upwards:

Inheritance is characterized as single or multiple depending on the number of superclasses a subclass can inherit from. In single inheritance a class can only inherit from one superclass. Single inheritance results in a strict tree hierarchy where each subclass is related to its superclass by an "is-a" relationship.
- Ghan Bir Singh, Rensselaer Polytechnic Institute

In Java, one class can have only one superclass from which it inherits. This is why it is called single inheritance. In addition, it is a multilevel inheritance since the class indirectly inherits from the superclass of the superclass, up to the root Object.

But one java class can not simultaneously directly inherit from two superclasses, and belong to two different inheritance hierarchies.

  • Java supports multiple inheritance via implementations in interfaces. Therefore Java is a multi-inheritance language; not a single-inheritance one. – David Arno May 28 at 8:48
  • 3
    @DavidArno I agree that interface inheritance is a substitute to MI in some of its most frequent use cases (i.e. inheritance of pure abstract classes). However, the inheritance of an interface is not the same as class inheritance to which I refer in my answer: interfaces requires you to implement all the methods/behaviors foreseen in the interface, without any reuse. Of course, default methods nowadays provide an [alternative to mixins](hannesdorfmann.com/android/java-mixins ). But still, interfaces are not sufficient to make java a true MI language. – Christophe May 28 at 9:33
6

You posted the definitions of those terms in your question. All you have to do is to apply the definitions to your observations:

Single inheritance refers to when a class inherits another class.

B inherits from A and A inherits from Object, ergo it is Single inheritance.

Multi-level inheritance refers to when a class inherits another class that inherits another class, etc.

B inherits from A and A inherits from Object, ergo it is Multi-level inheritance.

Hierarchical inheritances refers to when a super-class that has many sub-classes.

Object has many subclasses, ergo it is hierarchical inheritance.

Can you have "true" single inheritance in Java?

The term "true" single inheritance is not defined in your question, ergo, the answer depends on how you define the term "true" single inheritance.

0

Java has multiple inheritance. Strictly speaking. So probably not "true" MI, as it is crippled.

While a Java class (aside from java.lang.object) always inherits from exactly one other Java class, it can also inherit (called "implement") from any number of Java interfaces.
Those interfaces have only one restriction compared to the priviliged class:
They cannot have any member-variables. If you want to despute it, remember the default methods.

Now if you arbitrarily decide that this restriction is enough so that Java interfaces are irrelevant to the definitions you gave, then Java only has single inheritance, and it should be "true" enough.

Now, any way you slice it, the inheritance is multi-level, not single-level. But that's not actually what you asked about, even if you might have wanted to.

  • Read the question. It does not mention or ask about multiple inheritance at all. – ub3rst4r May 28 at 4:24
  • 1
    @ub3rst4r If it has some kind of MI, it obviously doesn't have true SI. – Deduplicator May 28 at 8:05

Not the answer you're looking for? Browse other questions tagged or ask your own question.