• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Multiple Inheritance - Reusability

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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();
c.start();
c.stop();

This code works the same way if your vehicule is a car or a truck
 
Ganesh Kumar
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reply.

Are you coming to say that Interfaces are not for replacing Multiple Inheritance? Any relationship between interfaces and multiple inheritance?
 
Ranch Hand
Posts: 190
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
They are not for replacing multiple inheritance, but multiple inheritance is achieved through them in Java.
 
Ganesh Kumar
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
While achieving the Multiple Inheritance, are we losing the code reusability??
 
Swapnil Sonawane
Ranch Hand
Posts: 190
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't think so.
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Kris
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Ganesh Kumar C V ",
Please check your private messages regarding an important administrative matter.
-Ben
 
Ranch Hand
Posts: 320
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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();
or
logMethod = new ScreenLog();
or
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 ]
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic