• Post Reply Bookmark Topic Watch Topic
  • New Topic

A simple class design problem  RSS feed

 
Jacob Thomas
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • 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
 
Svend Rost
Ranch Hand
Posts: 904
  • Mark post as helpful
  • send pies
  • 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
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • 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
  • 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?
 
Kaydell Leavitt
Ranch Hand
Posts: 690
Eclipse IDE Firefox Browser Mac OS X
  • Mark post as helpful
  • send pies
  • 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 ]
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • 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 ]
 
Vj Kamath
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • 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 ?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!