Question

I already know that we prefer implementing Runnable interface over extending thread because it provides flexibility to our class, we can extend some other class in future. This is not possible when we extend Thread class since Java does not support multiple inheritance (before Java 8). Now when Java 8 is released we can use multiple inheritance hence both the approaches are flexible. My question is What are the advantages and disadvantages of implementing Runnable interface over extending Thread class in context of Java 8? Thanks a lot in advance :) .

Was it helpful?

Solution

You can not extend multiple class in Java 8 also. In java document, it is clearly written that

One reason why the Java programming language does not permit you to extend more than one class is to avoid the issues of multiple inheritance of state, which is the ability to inherit fields from multiple classes.

and

The Java programming language supports multiple inheritance of type, which is the ability of a class to implement more than one interface. An object can have multiple types: the type of its own class and the types of all the interfaces that the class implements.

Implementing more than one interface containing same default methods is a form of multiple inheritance. and again from the same javadoc...

As with multiple inheritance of implementation, a class can inherit different implementations of a method defined (as default or static) in the interfaces that it extends. In this case, the compiler or the user must decide which one to use.

So In any version of java you can not extend more than one class, Thus answer to your question remains unchanged.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top