• 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

A simple class design problem

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I was asked the following question in one of my recent interviews:

There are three classes namely Animal, Fish and Amphibian




The Animal and Fish classes are concrete classes. The Amphibian class should implement the functionalty of both the Animal and Fish classes. What is the best way to do this Java? Thank you
 
Ranch Hand
Posts: 904
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How about:

Let Fish implement a "WaterAnimal" interface (which would have a walk method)
Let Animal implement a "WalkingAnimal" interface (which would have a swim method)
Let Amphibian implement the two interfaces.

?

/Svend Rost
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Assuming you can't change "Fish" or "Animal" in any way, what you could do would be to hold an instance of "Fish" and an instance of "Animal" as private members in Amphibian, and "delegate" method calls like this:

 
Jacob Thomas
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is easy to implement multiple interface inheritance in Java. But I am asking if there is any hack or work-around solution to do this in Java without the use of interfaces? ie. can you achieve this using classes alone?
 
Ranch Hand
Posts: 694
Mac OS X Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
interfaces are good things. Why make up a problem that constrains you so that you can't use them. One of the objectives of the SCJA (Sun Certified Java Associate) -- the first Java certification -- is to learn how to "Program to an interface". In the SCJA Study Guide,

The SCJA Study Guide

it teaches that whenever you are thinking about using an abstract class, you should consider using an interface instead of, or addition to, using an abstract class.

-- Kaydell
[ December 06, 2006: Message edited by: Kaydell Leavitt ]
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ernest's answer meets the "implement the functionalty" requirement, but doesn't give us polymorphism. If I have a method that expects a Fish argument, you can't call it with an Amphibian. The interviewer was probably looking for a discussion of the pros & cons of "implement the functionality" vs polymorphism.
[ December 07, 2006: Message edited by: Stan James ]
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How about having a fish interface and animal interface. Then a amphibian interface that extends both fish and animal interfaces. And then a class which implements the amphibian interface ?
 
reply
    Bookmark Topic Watch Topic
  • New Topic