the blog for developers

Java Interview questions: Multiple Inheritance

Recruiting and interviewing is a never ending task for development managers. Sometimes you get help from HR, but you’re still on your own when deciding to hire or not to hire. Or as I’ve written last time in “Java Interview questions: Write a String Reverser”:

Interviewing developers for a programming job is hard and tedious. There are some excellent Guides, like the Joel Guerilla Guide to interviewing, but in the end you need to decide yourself to hire or not to hire. To get a quick idea about their programming abilities I have considered asking the String reverse question.

Beside the String reverse question I have another favorite.

Does Java support multiple inheritance?

Well, obviously Java does not have multiple inheritance in the classical sense of the word. So the right answer should be “no”, or “no, but” or “yes, but”. From there one can explore several ways. Mostly I start by asking if the Java language designers were too stupid to implement multiple inheritance? Why did the C++ guys implement it then? We mostly land at the Diamond Anti-Pattern then:

In object-oriented programming languages with multiple inheritance and knowledge organization, the diamond problem is an ambiguity that arises when two classes B and C inherit from A, and class D inherits from both B and C. If a method in D calls a method defined in A (and does not override the method), and B and C have overridden that method differently, then from which class does it inherit: B, or C?

The other way to explore is how Java “emulates” multiple inheritance? The answer, which might already have surfaced, ist Interfaces. We then usually discuss interfaces in Java, if, when and how the candidate has used them in the past. What are interfaces good for, does he like them? I can explore how good he is at modelling and sometimes make him draw a diagram with interfaces. We go on with problems of interfaces in Java, and what happens when two interfaces have the same static fields and a class implements both – some kind of “multiple inheritance” in Java:

public interface I1 {
   String NAME = "codemonkeyism";
}

public interface I2 {
   String NAME = "stephan";
}

public class C implements I1, I2 {
   public static void main(String[] args) {
      System.out.println(NAME);
   }
}

Staying true, the language designer decided that this does not work in Java:

C.java:3: reference to NAME is ambiguous, both variable NAME
              in I1 and variable NAME in I2 match
      System.out.println(NAME);
                         ^
1 error

There are many more ways to explore with the candidate, e.g. what are the modifiers of interface methods. Are mixins or traits a better solution to the diamand problem than interfaces? Or are they just as bad as multiple inheritance? As I’m no longer very fond of inheritance and think most uses are a code smell, one can also discuss the down side of inheritance – thight coupling for example – with the candidate.

Why?

Why do I ask this question and what do I learn from it? Inheritance is a very basic concept in OO and should be understood by every Java developer. Reflection on his work and going beyond knowing the syntax is an essential trait also, therefor the multiple inheritance question. I prefer questions that spawn many opportunities to explore. The inheritance question is one of them: Mutliple inheritance, language design, code smells, solutions, interfaces, role based development.

If you interview with me, you should know the answers, or tell me you’ve read my blog.

See also:

You can leave a Reply here. Of course, you should follow me on twitter here.

You can share this post!
Do you want to tell others about this article? Use the social bookmark icons to submit this artice to the service of your choice. Thanks.

About the author: Stephan Schmidt has more than 15 years of internet technology experience and 10 years experience in agile. He was head of development, consultant and CTO and is a speaker, author and blog writer. He specializes in organizing and optimizing software development helping companies by increasing productivity with lean software development and agile methodologies. Want to know more? All views are only his own.

9 Tweets

Leave a reply.

Comments

Jonathan

It’s not too hard to simulate multiple inheritance using dynamic proxies. Most AOP frameworks allow something similar via introductions or mixins. I think it would be nice, in an interview, to explore how more recent languages have compensated from Java’s single inheritance limitations, such as with Scala’s traits.

[...] Java Interview questions: Multiple Inheritance I need to take a closer look on mixins and traits [...]

Cedric

If you gave me these answers, I would probably give you a bad grade :-)

First because the question you are asking is vague: what kind of inheritance are you talking about, implementation or interface?

Java doesn’t emulate anything: it supports multiple inheritance of interfaces and it doesn’t support multiple inheritance of implementation. C++ supports both.

@Jonathan: You’re right, the best I have seen is Qi4J which brings it to extremes. Richard is very deep into DDD using mixins.

Jordan Stewart

As I see it, you *implement interface* and you *inherit implementation*.
With an interface there is nothing to inherit because an interface is abstract, it has no substance. An implementation of that interface is concrete and so can be inherited.

So there is no ‘interface inheritance’ – that’s implementation.

Java has multiple implementation / single inheritance.

Jordan Stewart

*I’ll qualify that statement by adding that I was only thinking about ‘class’ definitions at the time. I’m not sure what I’d call the ‘extends’ relationship on an interface – perhaps that *would* be “interface inheritance” :-)

@Stephan – Qi4J looks really interesting – quite a different way to approach things! Have you built anything with it?

bubak

JVM and bytecode supports multiple inheritance. It is just not enabled in Java Compiler

Jonathan

I was going to mention Qi4j specifically, but I didn’t think too many people had heard of it. I really like how it allows an invocation stack to be built similar to using traits in Scala (with a simpler linearization), but I’d prefer to be able to get that functionality without having to use the rest of the framework.

My interest has swayed from proxy based AOP stuff towards compile-time code introduction such as what the Project Lombok guys are doing.

An interesting question to ask the candidate is what’s the difference between using composition and inheritance in Java. What are the advantages and disadvantages in applying the both strategies to design the classes.

[...] 3) 14 Jan 10 – Stephan Schmidt, Java Interview questions: Multiple Inheritance [...]

Yeah these are some good questions to ask in interview.

I think your questions are a notch too superficial. Independently of the kind of code inheritance you use, one big question always is: what do you have to do in order to get _and keep_ it working, assuming you can not control all code in the system?

What problems do you get once specifications and tests enter the field? Does the candidate understand the semantics of subtyping/inheritance (in general and in Java)?

Michael Wu

I think question itself is fine. Just want to get answers. From answers, knowing the concpetion.

public interface I1
{
public void work();
}

public interface I2
{
String name = “I2 name”;
public void work();
}

public class MultipInhe implements I1, I2
{

@Override
public void work()
{
// TODO Auto-generated method stub

}

public static void main(String[] args) {
System.out.println(name);
}

}

From which interface, work () method is interesting?

Leave a Reply

What people wrote somewhere else:

Published a new blog post: “Java Interview questions: Multiple Inheritance” http://tinyurl.com/yhc2a66

This comment was originally posted on Twitter

RT @codemonkeyism: Published a new blog post: “Java Interview questions: Multiple Inheritance” http://tinyurl.com/yhc2a66

This comment was originally posted on Twitter

RT: @codemonkeyism: Published a new blog post: “Java Interview questions: Multiple Inheritance” http://tinyurl.com/yhc2a66

This comment was originally posted on Twitter

Java Interview questions: Multiple Inheritance – http://su.pr/7iQF37

This comment was originally posted on Twitter

Code Monkeyism: Java Interview questions: Multiple Inheritance: Recruiting and interviewing is a never ending task… http://bit.ly/6Ii3By

This comment was originally posted on Twitter

Nice article on being realistic when conducting an interview http://bit.ly/5JXD8z

This comment was originally posted on Twitter

Interesting thoughts on interview questions… but does it have enough meat? http://bit.ly/8Oxl2n

This comment was originally posted on Twitter

@bpfurtado @deserodio o que acham? http://codemonkeyism.com/java-interview-questions-mutliple-inheritance/

This comment was originally posted on Twitter

@bpfurtado @dserodio o que acham? http://codemonkeyism.com/java-interview-questions-mutliple-inheritance/

This comment was originally posted on Twitter

Additional comments powered by BackType

Guide to CodeMonkeyism

Over the last 4 years I wrote many articles on this blog. To make it easier for you to find the relevant ones, I've organized them into topics.

Top 10

6 reasons why my VC funded startup did fail

Go Ahead: Next Generation Java Programming Style

Java Interview questions: Write a String Reverser

The dark side of NoSQL

7 Bad Signs not to Work for a Software Company or Startup

Is Java dead?

Scala vs. Clojure

Never, never, never use String in Java

No future for functional programming in 2008 – Scala, F# and Nu

Clojure vs Scala, Part 2

Java Developer

Is Java Dead?

Go Ahead: Next Generation Java Programming Style

Be careful with magical code

All variables in Java must be final

Never, never, never use String in Java

Bending Java: More readable code with methods that do nothing?

NoSQL Guy

NoSQL: The Dawn of Polyglot Persistence

The dark side of NoSQL

Essential storage tradeoff: Simple Reads vs. Simple Writes

Sharding destroys the goals of your relational database

The unholy legacy of databases

Startup/CTO

Development Dream Teams

6 reasons why my VC funded startup did fail

American vs. European style of Software Development

12 Things to Reduce Your Lead Time and Time to Market

The high cost of overhead when working in parallel

Essential storage tradeoff: Simple Reads vs. Simple Writes

Job Seeker

Another Good (Java) Interview Question

7 Bad Signs not to Work for a Software Company or Startup

Java Interview questions: Write a String Reverser (and use Recursion!)

Java Interview questions: Multiple Inheritance

As a Manager: What I value in developers

Top 10 Tips (+1) to Get a Pay Raise

Agilist

What Developers Need to Know About Agile

5 Practices Better to Change in Your Scrum Implementation

Scrum is not about engineering practices

ScrumMaster and ZenMaster: The joke of certification

What is Trans-Scrum?