Jump to content
  • Advertisement
Sign in to follow this  
King of Men

Multiple inheritance vs interfaces

This topic is 5455 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

It's not quite clear to me exactly what the difference is between multiple inheritance in C++, and interfaces in Java. Is it just nomenclature, or is there something more interesting going on?

Share this post


Link to post
Share on other sites
Advertisement
Interfaces can have only declarations so that if you have 15 interfaces, all you have is many functions you must implement. With multiple inheritence, each class that is inherited from can provide an interface AND an implementation, so that a class can inherit from 15 others and need only to overwrite the functions it wants to change (while mostly leaving the default implementations alone).

Interfaces are simmilar to using multiple inheritence, but only inheriting from abstract base classes.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Ignore the whole "interfaces are like abstract base classes" argument; that's one that I swear was originally invented by evil Professors who wished Java would die and they could go back to teaching Modula instead ;D.

Interfaces in java are actually merely "user defined types" - nothing more, nothign less.

Conveniently, a user-defined type is the basis for describing the interfaces between different parts of an application: you make a new unique concrete type for every "interface between different chunks of code"...which is one explanation for the name.

Multiple inheritance has little to do with java interfaces (despite what my first professor said to me in the first week of the java course - I later learnt he didn't really understand java himself). Java simply doesn't do multiple inheritance (which is a HUGE pain if you write libraries) - but this is almost irrelevant when writing applications: it was observed that almost all multiple inheritance in good well-written apps was just an attempt to extend user defined types, and the decision was made to only do the common case and avoid the rare case (multiple code inheritance) because of the ineherent dangers and pain (just like unconstrained pointers).

At least, that's how I heard it, 3rd or 4th hand. The explanation is a bit too pat as far as I'm concerned, but the reasoning is fairly sound. The core to remember is this:

1 java interfaces are just part of the type-system; they are NOTHING to do with code

2 multiple inheritance is about inheriting code from class to class.

3 java interfaces and code inheritance both HAPPEN to use the concept of "inheritance" but for different reasons and in different ways

Share this post


Link to post
Share on other sites
It's a little more than nomenclature, but that's the biggest part of it.

Java doesn't do C++-like multiple-inheritence, but it absolutely inherits from multiple interfaces (it's not called multiple-inheritence because that's a dirty-word).

To have a closer comparison, in C++ you need to use virtual inheritence of abstract base classes, and then it's the same thing that Java does "under-the-hood" for it's interfaces.


Quote:

2 multiple inheritance is about inheriting code from class to class.


This is a false premise. Multiple inheritenace is plainly inheriting from more-than-one base-class - such as interfaces.
In C++, you can inherit multiple implementations as-well-as interfaces. When comparing C++ to Java, "multiple-inheritence" is typically used to refer to the inhertience mechanisms present in C++ but not in Java (e.g. multiple-implementation inheritence). This subset of MI is assumed only for this type of comparison.


Quote:

3 java interfaces and code inheritance both HAPPEN to use the concept of "inheritance" but for different reasons and in different ways


Can you expound on this, to me it seems to be very simliar use of inheritence for similar reasons (to implement or re-implement a given set of interfaces)?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!