• 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

Why no mutiple inheritance

 
Ranch Hand
Posts: 8945
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
One of the major benefits of OOP'S is reusability. This is achieved using inheritance.
Unfortunately Java does not support muliple inheritance.
Suppose I want to extend 2 classes A,B in java and create a class called C, it is not possible. I can only extend one class say A but for B I have to copy all methods and variables from class B.What I have done is a copy and a paste approach.Is there reusability here?. If use interface for class B then I need to implement the interface in class C.
Does making class B an instance variable in class
C solves the problem?? I may want to overide some methods of class B. How I do that? Is reusability possible in Java?
 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Pradeep,
I understand that essentially your question is regarding choosing between composition and inheritance. Here is a quote from Thinking in Java by Bruce Eckel ( Chapter 6 ) that precisely answers your doubt.
--------------------------------------------------------------
Choosing composition vs. inheritance
Both composition and inheritance allow you to place subobjects inside your new class. You might wonder about the difference between the two, and when to choose one over the other.

Composition is generally used when you want the features of an existing class inside your new class, but not its interface. That is, you embed an object so that you can use it to implement functionality in your new class, but the user of your new class sees the interface you’ve defined for the new class rather than the interface from the embedded object.
When you inherit, you take an existing class and make a special version of it. In general, this means that you’re taking a general-purpose class and specializing it for a particular need. With a little thought, you’ll see that it would make no sense to compose a car using a vehicle object—a car doesn’t contain a vehicle, it is a vehicle. The is-a relationship is expressed with inheritance, and the has-a relationship is expressed with composition.
----------------------------------------------------------------
As for your specific question, that you want to override some methods of class B, this would mean that you want to keep the interface same, but you want to change the implementation. So inheritance is the one you should choose here.

 
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi rancher
You have asked why we cant have multiple inheritence in Java. Basically problem is that in multiple inheritence if we have any function defined in both parent classes then how can we choose any one of them for e.g. if we hav parent classes P1 and P2 then
class P1
{
public void f1()
{
}
}
class P1
{
public void f1()
{
}
}
now we hav a child class whch extends both of them
class C1 extends P1, extends P2
{
public void f2()
{
f1();
}
}
Now how compiler decides whch one to choose.
Whereas in C++ we dont have ne justification for it in generally although there are some compiler specific implementations for such prb. i.e.
in borland compiler they said that the function from a class from whch child class extends first will be used i.e.
class C1: public P1,P2
{
public void f2()
{
f1();//function from class P1 will be called.
}
}
 
Ranch Hand
Posts: 164
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is a wisdom in not allowing multiple inheritance in Java.
In C++ multiple inheritance creates more problems then it can solve. Though single inheritance cannot match the raw power of multiple inheritance, its more safe.
Conceptually speaking single inheritance makes sense. Your child class is a "type of" the parent class. Now in a proper class design any two classes represent two different entities in your application. They have to be different. Now a child class can "type of" either of the two, but not the both. it doesn't make much sense.
In case of interface the relation is of type "can do this". they specify the actions. Its perfectly OK if your class can perform two sets of actions, which are two different interfaces.
If multiple inheritance is allowed programmers tend to inherit any class (maybe abstract) just to use the actions in that class. Which should be an interface in a proper design.
and moreever single inheritance is much more clean.
I hope this helps.
Kaustubh...
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i've also found that most of the use of multiple inheritance can be gained by simply extending B which extends A instead of extending both A and B. this also avoids the pitfalls of multiple inheritance
 
Pradeep bhatt
Ranch Hand
Posts: 8945
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I dont agree with ur solution.I would extend a class only if wants to acquire the properties of another class.C wants to get the properties of class A and B . In your solution, B extends A..and c extends B but I want to use B some where else that does not want the properties of A.
Ur solution is not helpful.
I am aware of the problem of multiple inhertiance. But one of the important features OF OOP is reusability. Java does not help in this matter. It allows u reuse a single class (Suppose I want to extend 2 classes) and code of the 2nd class is not reusable.

Originally posted by Dani�l Maree:
i've also found that most of the use of multiple inheritance can be gained by simply extending B which extends A instead of extending both A and B. this also avoids the pitfalls of multiple inheritance

 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
IMO this discussion could vastly improve by using a concrete example. Where do you feel the need for mutliple inheritance? I just wonder, because I think in the perhaps 4 years of my java programming experience I missed it, say, one or two times and always found an appropriate workaround...
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When multiple inheritance rears its ugly head, I always use the following example for discussion:
class Appliance { ... }
class Clock extends Appliance { ... }
class AlarmClock extends Clock { ... }
class Radio extends Appliance { ... }
class AlarmClock Radio extends ???
kind regards
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jos A. Horsmeier:
When multiple inheritance rears its ugly head, I always use the following example for discussion:
class Appliance { ... }
class Clock extends Appliance { ... }
class AlarmClock extends Clock { ... }
class Radio extends Appliance { ... }
class AlarmClock Radio extends ???
kind regards


Well, isn't an AlarmClockRadio a Radio that "has an" AlarmClock. This modeling would have the advantage that it would be very easy to instantiate AlarmClockRadios having different AlarmClocks.
 
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But how do you decide if it's a radio that has an alarm clock, or an alarm clock that has a radio?
I'm aware that it's not really important here, but sometimes it's hard to decide which one is the more suitable.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Younes Essouabni:
But how do you decide if it's a radio that has an alarm clock, or an alarm clock that has a radio?


Exactly, that's why I always use this example. Even more, suppose the base class Appliance carries some state, say, a serial number, of its own. One has to decide whether or not an alarmclock radio needs/wants _two_ serial numbers or, on the other hand, should diamond inheritance be applied here?
One could even argue that an alarmclock radio is a _container_ encapsulating more than one appliance. I like this example.
kind regards
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Perhaps we need a container class to hold the actual object. So a container could hold a clock or it could hold a radio or it could hold both. Then you could make the serial number a property of container.
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree. The AlarmClockRadio object has three objects, an alarm, a clock and a radio whose methods and properties could call be contained in one object.
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Younes Essouabni:
But how do you decide if it's a radio that has an alarm clock, or an alarm clock that has a radio?
I'm aware that it's not really important here, but sometimes it's hard to decide which one is the more suitable.


It's especially hard without knowing about the implementation details or typical usage of the AlarmClockRadio. The best solution will probably vary from system to system (or even through time). Sometimes it even doesn't matter at all.
Luckily, we always have the option to refactor if we got it wrong initially...
 
reply
    Bookmark Topic Watch Topic
  • New Topic