Results 1 to 5 of 5
  1. #1
    gardner is offline Member
    Join Date
    Jan 2011
    Posts
    2
    Rep Power
    0

    Default 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

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,525
    Blog Entries
    7
    Rep Power
    20

    Default

    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:

    Java Code:
    class D implements I1, 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.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    gardner is offline Member
    Join Date
    Jan 2011
    Posts
    2
    Rep Power
    0

    Default

    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

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,525
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by gardner View Post
    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
    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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,525
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by gardner View Post
    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
    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 ;-)
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Running multiple threads on multiple CPU cores?
    By Dosta in forum Threads and Synchronization
    Replies: 2
    Last Post: 09-19-2010, 03:48 PM
  2. Replies: 2
    Last Post: 12-24-2009, 06:41 PM
  3. multiple inheritance in java
    By pawanspace in forum New To Java
    Replies: 2
    Last Post: 12-31-2007, 04:08 AM
  4. Multiple Inheritance
    By mew in forum New To Java
    Replies: 1
    Last Post: 12-01-2007, 10:04 PM
  5. Multiple Inheritance in java
    By paty in forum New To Java
    Replies: 4
    Last Post: 08-02-2007, 02:25 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •