• 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
  • Liutauras Vilda
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Peter Rooke
  • Himai Minh
Bartenders:
  • Piet Souris
  • Mikalai Zaikin

multiple inheritance

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all,
why java does not support multiple inheritance,i need exact answer..
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A short, friendly explanation of "Deadly Diamond of Death"DDD
 
Ranch Hand
Posts: 418
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
lowercase baba
Posts: 13086
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
author
Posts: 23937
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Raj Kumar Bindal
Ranch Hand
Posts: 418
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
fred rosenberger
lowercase baba
Posts: 13086
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
reply
    Bookmark Topic Watch Topic
  • New Topic