aspose 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

bakiyalakshmi dhanraj
Greenhorn

Joined: Nov 02, 2007
Posts: 24
hi all,
why java does not support multiple inheritance,i need exact answer..
Talha Bin Hussain
Greenhorn

Joined: Feb 12, 2006
Posts: 6
Hi,
you know when one class inherits from another then it can have access to public members of it.
Lets have a look of an example that why Java does not support multiple inheritance.

class A {
String s;
}
class B extends A{
String s;
}

// Now if java support multiple inheritence (which it does not)

class C extends A,B {

public static void main(String [] args){
C c=new C();
System.out.println(c.a);
}
}

Which a does this will print?
This is known as diamond problem.
Thats why java don't use multiple inheritance

Hope now you got it your exact answer.
Sunny Jain
Ranch Hand

Joined: Jul 23, 2007
Posts: 433

A short, friendly explanation of "Deadly Diamond of Death"DDD


Thanks and Regards,
SCJP 1.5 (90%), SCWCD 1.5 (85%), The Jovial Java, java.util.concurrent tutorial
Raj Kumar Bindal
Ranch Hand

Joined: Apr 15, 2006
Posts: 418
java provides multiple inheritance with the help of interfaces.But if a class implements 2 interfaces,then also ambiguity of data is there.So,what is the main purpose behind the existence of interfaces.
Amod Mulay
Ranch Hand

Joined: Apr 06, 2006
Posts: 33
2 interfaces with the same name does not make any sence buddy.
interfaces do not have implementation of methods. and even if you do this thing "2 interfaces with same method name" still in the concrete class you wil lhave to have only one method. only advantage you will get out of this is that if you are creating an object of that class using reflection then you can easily type cast it in anywhich way you want
Gavin Tranter
Ranch Hand

Joined: Jan 01, 2007
Posts: 333
I think, and this is my personal view, that thinking of interfaces as a method of multiple inheritance is "wrong".

In my view, interfaces should be used to allow a class to implement behaviour that is outside of the natural behaviour of that class, or behaviour that is of such a "generic" nature that it could be useful to several class, similar to the idea of a cross cutting concern from AOP, these you would want to implement as interfaces to "wrap" around the class that wants to make use of them.

The other use of Interfaces is to extend the types an object can be considerd as be of. In its must basic form, this could be a simple marker interface, (an interface with no methods) or it could include additional behaviors.

I personally I am not sure there is a need for multiply inheritance, and for me i usual use it as a clue that perhaps I have miss understood something in my problem domain and need to reconsider part of my design.

Just some of my thoughts.

Thanks
Gavin
[ February 05, 2008: Message edited by: Gavin Tranter ]
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11314
    
  16

Originally posted by bakiyalakshmi dhanraj:
hi all,
why java does not support multiple inheritance,i need exact answer..

Why do you think java SHOULD support multiple inheritance? just because it's been done before doesn't mean it's a good idea.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18845
    
  40

Originally posted by bakiyalakshmi dhanraj:
hi all,
why java does not support multiple inheritance,i need exact answer..


The other issue of needing an "exact answer", is that I don't think it is possible to know what went through Gosling head when he designed the language.

We can only speculate that Gosling, like many developers at the time, thought that multiple inheritance was evil. I would speculate that it is probably one of the reasons that Smalltalk and Objective-C were getting tons of traction as well.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Raj Kumar Bindal
Ranch Hand

Joined: Apr 15, 2006
Posts: 418
I agree with the thoughts of Gavin and i appreciate the way he has presented his thoughts.But, both wrapper and marker things can be provided by an abstract class also.
I will appreciate if any one can tell even one thing that interface can provide us and abstract class can not provide us or is there anything that interfaces provide in an easy way as compared to abstract classes.I don't think that interfaces are a good idea.
David McCombs
Ranch Hand

Joined: Oct 17, 2006
Posts: 212
Originally posted by Gavin Tranter:
I think, and this is my personal view, that thinking of interfaces as a method of multiple inheritance is "wrong".


I agree. It is possible for a class to implement 2 interfaces with the same method signature, so the chances of the DDD is still possible.


"Should array indices start at 0 or 1? My compromise of 0.5 was rejected without, I thought, proper consideration."- Stan Kelly-Bootle
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11314
    
  16

Originally posted by David McCombs:


I agree. It is possible for a class to implement 2 interfaces with the same method signature, so the chances of the DDD is still possible.


I disagree. interface 1 says "you will have a method called THIS that takes THESE parameters"

interface 2 says "you will have a method called THIS that takes THESE parameters"

Where is the conflict? the problem with the DDD is that it's hard to know which of the two implemented methods to actually run. but with interfaces, the method is only implemented once, so there is no ambiguity.
David McCombs
Ranch Hand

Joined: Oct 17, 2006
Posts: 212
Originally posted by Fred Rosenberger:


I disagree. interface 1 says "you will have a method called THIS that takes THESE parameters"

interface 2 says "you will have a method called THIS that takes THESE parameters"

Where is the conflict? the problem with the DDD is that it's hard to know which of the two implemented methods to actually run. but with interfaces, the method is only implemented once, so there is no ambiguity.



I am not saying this is good programming, but it is possible to have 2 interfaces that do different things, with the same method(s) signature.

The method is implemented once, but if the contracts are different, you don't exactly have the DDD, but you have just as big of a mess.

It is a moot point given that the odds of it happening are low, and it would mean that one(or both) of the interfaces haven't been thought through well enough.
Amod Mulay
Ranch Hand

Joined: Apr 06, 2006
Posts: 33
I have read all you comments and would like to add some things or re-iterate it;
1. Interfaces declare(signeture) and not define(implement) the method.
2. you can implement multiple methods with same method signetures and it will not cause DDD.
3. and yes people do use such interfaces so that when they are type casting a object of a class implementing the interfaces they can type cast it to whichever type they want depending on the object use.

So there is nothing wrong in it. it is an added advantage
Amod Mulay
Ranch Hand

Joined: Apr 06, 2006
Posts: 33
I have read all you comments and would like to add some things or re-iterate it;
1. Interfaces declare(signeture) and not define(implement) the method.
2. you can implement multiple methods with same method signetures and it will not cause DDD.
3. and yes people do use such interfaces so that when they are type casting a object of a class implementing the interfaces they can type cast it to whichever type they want depending on the object use.

So there is nothing wrong in it. it is an added advantage
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: multiple inheritance