permaculture playing cards
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Multiple Inheritance - Reusability Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Multiple Inheritance - Reusability" Watch "Multiple Inheritance - Reusability" New topic

Multiple Inheritance - Reusability

Ganesh Kumar

Joined: Dec 18, 2006
Posts: 11
class C{
void funC(){}

class D{
void funD(){}

class E extends C,D{ // Not allowed in java but assume it works


In the above one, the methods in the classes C and D are reused in E. The same C and D can also be "reused" by any other class by extending them.[Assume multiple inheritance is allowed].

Consider the below code

interface C{
void funC();

interface D{
void funD();

class E implements C,D{
public void funC(){}
public void funD(){}

Here C and D are implemented by E. If I need the same funtionality in some other class I need to implement these interfaces in that class too.I feel that reusability is lost.

How Interfaces is an alternate to Multiple Inheritance if the "Reusability" is lost?

Can anyone please clarify?
Antonio Tercero
Ranch Hand

Joined: Jun 05, 2008
Posts: 110
Interfaces are widely used because of polymorfism.
For example :

interface Vehicle{
void start();
void stop();

class Car implements Vehicle{
// start() and stop() implementations

class Truck implements Vehicle{
// start() and stop() implementations

In your main program, if you handle all the logic using Vehicle,
any changes in class Car will not affect the logic.
Vehicle c=new Car(); // or Vehicule c=new new Truck();

This code works the same way if your vehicule is a car or a truck

Ganesh Kumar

Joined: Dec 18, 2006
Posts: 11
Thanks for the reply.

Are you coming to say that Interfaces are not for replacing Multiple Inheritance? Any relationship between interfaces and multiple inheritance?
Swapnil Sonawane
Ranch Hand

Joined: Jan 02, 2008
Posts: 190
They are not for replacing multiple inheritance, but multiple inheritance is achieved through them in Java.

Swapnil S. Sonawane<br /> <br />B.Tech (Expected May 2009)<br />Computer Science, NIT Durgapur, India<br />SCJA 1.0
Ganesh Kumar

Joined: Dec 18, 2006
Posts: 11
While achieving the Multiple Inheritance, are we losing the code reusability??
Swapnil Sonawane
Ranch Hand

Joined: Jan 02, 2008
Posts: 190
I don't think so.
Krishnamoorthy Vuyala Muralidharan
Ranch Hand

Joined: Sep 13, 2005
Posts: 52
Hi Ganesh

Interfaces are normally nothing to do with reusability. Interfaces are meant to have different implementations so its contrary to reusability. After all, its the purpose of the interface, I believe.

If you want code reusability, that means all the classes that extend will have certain same functionality, then you must go for inheritance extending a class rather than implementing an interface.

With reference to your code above, consider the following similar example, if you implement an interface named 'Clickable':

interface Clickable{
void click();

class ClickableButton implements Clickable{
public void click(){
// method implementation code...

If you want the same implementation for another class(lets say SpecialClickableButton), then 'SpecialClickableButton' must not reimplement Clickable interface but it must inherit from 'ClickableButton' as follows:

class SpecialClickableButton extends ClickableButton{
// inherits the same functionality of click() as the superclass
// this way the reusability is NOT lost!!!

//specialised method than the superclass ClickableButton
void anotherMethod(){


If you are coming from a C or C++ background, in my kind opinion, its better that you do not compare with them. Otherwise, it will not help you rather it will just confuse you.

Hope this helps.

Best Regards
Ben Souther

Joined: Dec 11, 2004
Posts: 13410

"Ganesh Kumar C V ",
Please check your private messages regarding an important administrative matter.

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Bob Ruth
Ranch Hand

Joined: Jun 04, 2007
Posts: 320
I have not carefully read each entry so I may be repeating another answer here.

The way that I look at interfaces is that they establish a tightly defined API.

In my mind, an interface exists so that a programmer/developer can define the method calls required to establish a common programming interface that can be called from any number of places in the program. They do not (CAN not!) enforce a particular implementation. In fact, that is the flixibility that they give you. Each class that implements the interface must provide an implementation of each method that fulfills the "contract" advertised by the API but it must do so in a manner that is correct for this type of object. And so it goes that each object will have it's own implementation BUT each one of them accepts the same set of method calls/signatures.

Where polymorphism helps us is that, while each different object that implements the interface is actually a different class/object type, a single reference variable of the INTERFACE type can point to any one of them. The actual class type does not matter because polymorphism allows an interface-type reference to point to any of them.

Perhaps a short example.

If I define an interface for my design called Loggable. That interface has one method, makeLogEntry().

Now, I design three classes, PrinterLog, ScreenLog, and FileLog all three of them implement Loggable and each implements makeLogEntry() in a manner appropriate for that logging type (that is to say, PrinterLog has makeLogEntry() go to the printer, ScreenLog makes it go to the screen, and FileLog makes it go to a specified file.

Down in your program code somewhere you will decide which type of logging you wish to do and instantiate the appropriate object.

However, if you declare a reference variable,

Loggable logMethod;

whichever of the below you use will work:

logMethod = new PrinterLog();
logMethod = new ScreenLog();
logMethod = new FileLog("filename");

After that point, anywhere in your code that you want to make a log entry all you have to do is code in:

logMethod.makeLogEntry("Text of the log entry");

that way, it doesn't matter which class logMethod actually refers to, because of polymorphism and the interface definition, the call will work.

Edited because another thought occurred to me:

Perhaps also, another point of view might be handy....

You haven't lost re-usablility of anything at all. In fact, you are re-using the interface to allow common access to what can be a wide variety of classes!

[ June 13, 2008: Message edited by: Bob Ruth ]
[ June 13, 2008: Message edited by: Bob Ruth ]

SCJP - 86% - June 11, 2009
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15093

One of the reasons why multiple inheritance with classes is not allowed in Java is because it leads to difficult problems such as the diamond problem. The designers of the Java language wanted to get rid of the complexity that makes C++ hard, so they decided to leave multiple class inheritance out of Java.

In practice, you never really need multiple class inheritance. I was a C++ programmer eight years ago, before I started with Java. In the past eight years of programming in Java, I've never missed multiple class inheritance.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
I agree. Here's the link:
subject: Multiple Inheritance - Reusability
It's not a secret anymore!