Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • 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
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Real alternatives to multiple inheritence

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
This is my first post to javaranch so go easy on me.
I've been coding C++ now for around 7 years, but also doing bits and pieces of Java here and there, hoping to get enough experience to make the leap over to Java fully someday.
Anyway, onto my question - The company that I am currently contracting for make extensive use of multiple inheritence and although I have personally avoided this in the past, I can now see, that with care it can work really really well.
Some of our most heavily used classes are often inherited multiply as what you might call 'mix-ins'. I know that in Java you can use interfaces to achieve similar results to Multiple Inheritence (although without implementation in the interface of course) and that you could also use other alternatives such as aggregation and delegation. But in certain circumstances I just can't see anything nearly as intuitive as multiple inheritence.
I'll give a quick example of one that I was just having a discussion with a collegue about. It's not the best example - as it's fairly simple, but it's the easiest for me to explain here.
A lot of the objects in our system provide statistics to a central controller that sits on the network monitoring the server farm. There is a central Singleton StatisticsManager object along with other objects that are used for transport of data etc. Objects provide statistics by inheriting from the StatableObject interface which contains a small amount of functions - the main one being SetStatistic. Objects call this SetStatistics method when they want to update a particular statistic. How would you implement a similar system in Java, I couldn't think of anything nearly as intuitive.
I suppose one method would be just call the StatisticsManager directly in the class when the stat changes, but this would require knowledge of the StatisticsManager and would mean I would have to send class name and other information that doesn't change with each call rather than just passing it to the StatableObject constructor.
Any thoughts would be much appreciated, could do with some ammunition for my regular C++ vs Java arguments that I seem to be seeing more and more now ;-)
Regards,
Steve
 
Ranch Hand
Posts: 1209
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

hi steve,
I've'nt read many object oriented methodologies books,
did'nt read many which said multiple inheritance is bad / wrong.
I think there's'nt any flaw if a design employs multiple inheritance.

class LandVehicle{
}
class WaterVehicle{
}
class HoverCraft extends LandVehicle, WaterVehicle{
}
makes sense to me.
just my thoughts. But obviously Java designers did'nt like it and yeah they have reasons for it.
karthik.
 
Sheriff
Posts: 6999
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I won't go into the reasons for avoiding multiple inheritance now, but just like pointers (also avoided in Java) it is powerful, but also sneakily dangerous.
If you make a lot of use of mixins, you may want to consider something like Aspect J which "weaves" together "aspects" into combined classes. Very powerful once you get it set up, but I would recommend that you consider incorporating "Ant" into your build process (if you don't already) as it has built-in support for Aspect-J processing.
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Steve Dalton:

Some of our most heavily used classes are often inherited multiply as what you might call 'mix-ins'. I know that in Java you can use interfaces to achieve similar results to Multiple Inheritence (although without implementation in the interface of course) and that you could also use other alternatives such as aggregation and delegation. But in certain circumstances I just can't see anything nearly as intuitive as multiple inheritence.


Steve -
For my play on the extends/implements controversy, take a look at my somewhat coherent post:
http://www.javaranch.com/ubb/Forum1/HTML/001526.html


A lot of the objects in our system provide statistics to a central controller that sits on the network monitoring the server farm. There is a central Singleton StatisticsManager object along with other objects that are used for transport of data etc. Objects provide statistics by inheriting from the StatableObject interface which contains a small amount of functions - the main one being SetStatistic. Objects call this SetStatistics method when they want to update a particular statistic. How would you implement a similar system in Java, I couldn't think of anything nearly as intuitive.


As to your example, it seems more as a look towards patterns rather than language implementation. (Forgive me, I just finished reading John Vlissides' Pattern Hatching.) By using the OBSERVER pattern (or even JV's somewhat preferred MULTICAST pattern), you could have each derived StatableObject interact with the StatisticsManager in a manner where inheritance is not an issue.
Granted, we don't live in a perfect world where we can rewrite code according to "best practices", but alternatives exist to work around inherent implementation (read that: "language") problems.
 
Author
Posts: 6055
8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by karthik Guru:
But obviously Java designers did'nt like it and yeah they have reasons for it.


Actually, Gosling said he didn't include multiple inheritence simply because it was too hard (at last for him and his small team, when they first started creating Java).
--Mark
 
You have to be odd to be #1 - Seuss. An odd little ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic