Multiple inheritance in Java explained

Multiple inheritance is a feature of some object-oriented computer programming languages. This is where an object or class can come into characteristics and features from one or more parent object or parent class.

Java does not support multiple inheritance because it can cause ambiguity in various scenarios, mainly because of the ‘diamond problem’. Java is designed to be a simple programming language.

The problem is that is a class extended two other classes, and both superclasses had, for example, a doStuff() method, which version of doStuff() would the subclass inherit? This type of scenario is typically known as ‘Diamond Problem’ also known as ‘Deadly Diamond of Death’, and because of the shape of the class diagram that can be created in a multiple inheritance design.

The diamond is formed when classes B and C both extend A, and both C and C have overridden the method in A. Class D has, in principle, inherited two different implementations of the identical method.

When drawn as a class diagram, the shape of the four classes looks the same as a diamond.

After the introduction of Default Methods in Java 8, the interfaces can also have the method bodies. In Java 8 interface, it can have method definition using Default Methods, then clearly it should also result in ambiguity? Yes, but Java 8 can handle this type of compatible problem.

In Java 8, you cannot implement multiple interfaces having the same signature, without explicitly overriding the methods in the Child class. It is possible to call the Parent interface method explicitly from the Child class.

Explaining the inheritance exception for Java 8 may not be necessary but David believes this may show extra credibility knowing this extra bit of information.

David also discusses his love for snippets of code being projected onto a wall or are shown to you in a reasonably easy-to-read form, and you are asked to describe what they mean, what they print, and what value of a variable would be.

However, the types of questions he isn’t too fond of are coding exercises in which you need to type. Especially, if it is in an editor platform or IDE that is unfamiliar, as he believes this is not a test of knowledge.

David’s advice for candidates going through the Java interview process is to understand that when they are asking questions and evaluating you on your answers, it is entirely appropriate to evaluate them on the content of those questions and how they react to your answers. His indication of a good company is: ‘if this company was public, would I buy stock in them?’


And that is our round-up of the popular Java interview questions you may come across. These questions have been outlined on how to best answer during an interview, however, it is advised to do as much research as possible to ensure you answer the question as best as you can.

 

Related blog:

Java training in chennai