0

We know that abstract class contains both abstract and non - abstract methods , so what if we have only abstract methods so that we can achieve multiple inheritance like interfaces.

Here is the example:

public abstract class superclass1 {
 
   public abstract void func ( );

}

public abstract class superclass2 {

    public abstract void func ( );
}

public class test extends superclass1 , superclass2 {

    /* Code goes here */
}

I know above code is not possible but why Java is not allowing it , because we define class body only after extending it just like interfaces and work correctly such that , so there will be no ambiguity and no problem.

Steve54
  • 11
  • 2
  • 1
    [It is not useful to ask why something *isn't* in the language.](https://meta.stackoverflow.com/questions/293815/is-it-subjective-to-ask-about-why-something-wasnt-implemented-in-the-language). Why not just make both of them interfaces if you want to do this so badly? – Sweeper May 03 '23 at 03:37
  • @Sweeper Thanks for clarification . I just want to know is there any other reason behind it !! – Steve54 May 03 '23 at 03:38
  • A Java Interface has methods that are not implemented - these are _abstract_ methods. In fact, you can define something like this: `public Interface MyInterface { public abstract void doStuff(); }` . So, you can define the `superclass1` and `superclass2` interfaces, you can extend them in the `superclass3` interface. In fact, the Interface definition can also be like this: `public abstract Interface MyInterface { ... `. So, you have this feature that an abstract class with all abstract methods can be an interface and and allows multiple inheritance. – prasad_ May 03 '23 at 03:51
  • @prasad_ the Java keyword is `interface`, not `Interface`, and while `abstract interface` is legal syntax, and `abstract` can be applied to methods on interfaces, using `abstract` makes no difference. The `abstract` keyword is redundant in these situations. – tgdavies May 03 '23 at 03:57
  • @tgdavies _interface, not Interface_ - you are correct about the syntax. – prasad_ May 03 '23 at 04:00
  • If you search for "[java] multiple inheritance" you can find several questions with great answers like https://stackoverflow.com/questions/2515477/why-is-there-no-multiple-inheritance-in-java-but-implementing-multiple-interfac and https://stackoverflow.com/questions/995255/why-is-multiple-inheritance-not-allowed-in-java-or-c – Thomas Kläger May 03 '23 at 04:44

1 Answers1

0

Well, answering your question "why Java is not allowing it?": "Java doesn’t support multiple inheritances in classes because it can lead to diamond problem and rather than providing some complex way to solve it, there are better ways through which we can achieve the same result as multiple inheritances." Read more here

You say, but my class contains only abstract methods, right? But how do you guarantee that it will not change in the future? What if you have 10 purely abstract classes and inherited 100 class out of them with all the combinations and then you decide to add a non-abstract method to one of them? What if your parent class is a library class, you do not even know who and how inherited it. To avoid this complications, Java says: if you need purely abstract class and multiple inheritance, just use interfaces. In the practice I never had a big problems with this limitation. Actually, another way around, I saw examples in C++ where multiple inheritance from classes led to a very complex solutions that are very hard to maintain.

Andrej Istomin
  • 2,527
  • 2
  • 15
  • 22