Results 1 to 5 of 5
Thread: Multiple Inheritance
- 01-07-2011, 03:56 PM #1
Member
- Join Date
- Jan 2011
- Posts
- 2
- Rep Power
- 0
Multiple Inheritance
Hello,
I have been presented with a design problem that seems to be easily solvable with multiple inheritance and I would appreciate advice on how to resolve this in Java: I would like to descend from two abstract classes.
We have a set of test suites that are run on a set of different web browsers on multiple different platforms. Each platform and browser combo does seem to have its own unique behavior, such as clearing the cache for Safari on a Mac is distinct from doing so on Windows. This makes me want to create an abstract class for Platform and Browser. The nature of the application lends itself to leverage Liskov's substitution principle by making a TestSuite class runnable by a main harness.
The issue that we are facing is how to implement the browser and platform code. Having a TestSuite class descend from two abstract classes, one for browser and one for platform seems to make the most sense but is not allowed by the compiler. Should the browser and platform objects be encapsulated in the TestSuite? It seems the best answer is to subclass TestSuite from abstract platform and browser objects.
What is the Java way to do this?
Thank you,
Gardner
- 01-07-2011, 05:24 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
Java doesn't allow multiple inheritance of implementation (as you have already noticed) but it does allow multiple inheritance of interface (pure type). You should think interfaces, not classes. Suppose you have a class B1 that implements interface I1 and class B2 that implements interface I2. You can implement a class D that implement both I1 and I2:
To actually implement I1 and I2 it can either extend one of B1 or B2 or it can use just composition (both an B1 and B2 object are members of a D object). The D object delegates all implementations of I1 and I2 to their respective B1 or B2 member objects that implement the interfaces. Therefore your D object implements both I1 and I2 (with just the delegator methods) and effectively 'inherits' both the implementations B1 and B2.Java Code:class D implements I1, I2 { ... }
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 01-11-2011, 09:19 PM #3
Member
- Join Date
- Jan 2011
- Posts
- 2
- Rep Power
- 0
I ended up accomplishing this using concrete classes and interfaces. An interface to define a platform, a concrete class for each platform, an interface to define a browser, and a browser class that extends the platform class and implements the browser interface.
Thanks,
Gardner
- 01-12-2011, 08:09 AM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
You're welcome; your solution sounds a bit like a http://en.wikipedia.org/wiki/Bridge pattern. It's quite a versatile pattern.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 01-12-2011, 08:10 AM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
You're welcome; your solution sounds a bit like a Bridge pattern. It's quite a versatile pattern.
kind regards,
Jos
edit: sorry for the double post; I wanted to edit the link but I goofed ;-)When people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
Running multiple threads on multiple CPU cores?
By Dosta in forum Threads and SynchronizationReplies: 2Last Post: 09-19-2010, 03:48 PM -
Usually work with C++, need help replacing multiple inheritance in this case
By Stravant in forum New To JavaReplies: 2Last Post: 12-24-2009, 06:41 PM -
multiple inheritance in java
By pawanspace in forum New To JavaReplies: 2Last Post: 12-31-2007, 04:08 AM -
Multiple Inheritance
By mew in forum New To JavaReplies: 1Last Post: 12-01-2007, 10:04 PM -
Multiple Inheritance in java
By paty in forum New To JavaReplies: 4Last Post: 08-02-2007, 02:25 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks