File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes multiple inheritance Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "multiple inheritance" Watch "multiple inheritance" New topic
Author

multiple inheritance

Saad Ahmed
Greenhorn

Joined: Aug 03, 2001
Posts: 4
Hi, you always see people say Java doesn't support multiple inheritance but can someone explain what is the difference if a class implements an interface that extends from multiple interfaces? Isn't that the same thing?
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

Well, nothing can extend from multiple anythings.

It might *implement* multiple interfaces, but any given class can only *extend* a single other class.

Also, if my class does something like this:
The Car class implements two interfaces, Vehicle and Motorized. But this is not the same as multiple inheritance. Multiple inheritance generally means that you inherit not only the interface but the implementation. But in Java you can only 'implement' multiple interfaces.

So in C++, if my Vehicle and Motorized where classes, then yes, my Car class could extend both of them, and I'd have to write no additional code, if I were satisifed with the behaviour provided by my two parent classes.

But in Java, I only 'implement' the interface and I have to provide this implementation for both of those interfaces.
Saad Ahmed
Greenhorn

Joined: Aug 03, 2001
Posts: 4
Not exactly what I meant, let's say:
public interface A {
String getMesg();
}
public interface B {
String getMesg();
}
public interface AB extends A, B {
}
public class implements AB {
String getMesg(} { return "hello"; }
}
This is valid...so Java does support multiple inheritance by interface...I want to know what are the major benefits of class-based inheritance?
kyle amburn
Ranch Hand

Joined: Jul 29, 2001
Posts: 64
Hi-
Consider this:
interface Controls
class Car
class VWBug extends Car implements Controls
class Airplane implements Controls
Now the interface Controls has methods for braking(), accelerating(), etc. If these methods were put in a class then a implementation of these methods would be possible. However, you would not want to allow a basic implementation of braking() for a Airplane as opposed to a Car since they each do these operations differently. You cannot have a Car that is a vehicle and at the same time is a Airplane. If Airplane were an interface then the methods would only be methods that you implement.
Kyle
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: multiple inheritance
 
Similar Threads
why interface
Difference Between Abstract Class and Interface
MultipleImplementation Inheritance using Inner Class
Multiple Inheritance - Reusability
What makes a language good or better?