1

I have a question in Java, which may be related to multiple inheritance.

A Walker is a person who can walk and breed, A Talker is a person who can talk and breed, A Swimmer is a person who can swim,talk,walk and breed.

What is the best way to model them in Java?

Thank You for your answers, Tejo

3
  • 1
    Every trait that occurs more than once is put in an interface. The trait that occurs in all of them is put in the base class. There's your answer. Commented Mar 15, 2014 at 4:08
  • 1
    To decide how to model inheritance between multiple classes, think about what they have in common. Each class, in your words, represents "a person who can...[do something]...and breed." Because they all have these similar traits in common, you can create a common superclass for them to share, possibly called Person with a breed() method implemented. Commented Mar 15, 2014 at 4:09
  • Can all people breed? Some of your comments below indicate they can, but your problem description states "a ___ is a person who can breed", which implies that there are some people that can't. You should take care to make sure you have a concrete grasp of what your overall structure is. Commented Mar 15, 2014 at 4:39

5 Answers 5

2

You should extract the behaviour into interfaces, not separate classes.

interface Swimmable{
    void swim();
}

interface Talkable{
    void talk();
}

interface Walkable{
    void walk();
}

class Person {
    void breed();
}

Notice how I named them Walkable instead of Walker. This indicates that this interface defines a behaviour and is not a standalone thing. Now your actual object can be called Walker, but lexically spoken walkable also makes more sense if you use it for other classes that want that behaviour.

There's a lot of hype about "composition over inheritance" but really this is just an inappropriate solution to programmers using the two interchangeable when they shouldn't.

Now if you want to create an entity that can swim and walk you can simply define it as such:

class Athlete extends Person implements Swimmable, Walkable {
    @Override
    void swim(){ }

    @Override
    void walk(){ }
}

and there will be no messing around with actual instances of the behaviour that will have to be called through another layer in the actual Athlete class, etc etc.

Sign up to request clarification or add additional context in comments.

3 Comments

+1 this is the way to go (and the "-able" naming convention is a lot less harsh and more traditional than the "I*" I went with).
@JasonC: I believe Java prefers -able while C# prefers I*. Both languages are similar enough to me to not make it a problem either way :)
+1. Like JasonC, I really don't like the "I*" names. I feel that an interface name should be an adjective, but "Walkable", "Swimmable" and so on don't fit the model in which a "Runnable" is something that you run, a "Comparable" is something that you compare, and so on. I would have called these interfaces "Walking", "Swimming" and "Talking". And "Talker implements Talking" has a nice ring to it.
1

A Walker is a person who can walk and breed, A Talker is a person who can talk and breed, A Swimmer is a person who can swim,talk,walk and breed.

I would use interfaces to represent the traits, and use concrete classes to represent the types, e.g.:

public interface IWalker {
    public void walk ();
}

public interface IBreeder {
    public void breed ();
}

public interface ITalker {
    public void talk ();
}

public interface ISwimmer {
    public void swim ();
}

public class Person {
}

public class Walker extends Person implements IWalker, IBreeder {
    @Override public void walk () { ... }
    @Override public void breed () { ... }
}

public class Talker extends Person implements ITalker, IBreeder {
    @Override public void talk () { ... }
    @Override public void breed () { ... }
}

public class Swimmer extends Person implements ISwimmer, ITalker, IWalker, IBreeder {
    @Override public void swim () { ... }
    @Override public void walk () { ... }
    @Override public void talk () { ... }
    @Override public void breed () { ... }
}

This way you can, say, use IWalker everywhere that anything that can walk is expected, or Swimmer anywhere a specific type of person is expected, or Person anywhere a generic person is expected.

For the record, I don't particularly like the I* naming scheme for interfaces; but I picked them to be unique given the word choices.

4 Comments

How would it be if we remove IBreeder interface and put the breed() method in base class Person. Will that be fine?
Here you have multiple implementations of same method in more than one class. breed should be same for all classes. similarly walk and talk.
@Octopus Yes that would work fine for the OP's example, but personally I would put it in the interface for consistency. Also, what if you want to define a new type of person that can walk but not breed? Then you have to go back and refactor a lot of code. Another reason is that with IBreeder, you can make a method that takes an IBreeder and that makes it very clear that the method only cares that the object can breed, and doesn't care about anything else.
@user3422229 I can edit my answer (in which case Octopus' suggestion is more appropriate) -- can you confirm that all people can breed? If so then yes, it would make more sense as a base member. In the problem description in your post, you did not specify that all persons could breed; only that a ___ is a person that can breed (implying that there are people who can't) -- you should be careful to make sure you have a concrete grasp of your structure.
1

I see interface as something that hold the ablity that an entity has, possibly that is why in JavaSE many interfaces are name XXXable, where XXX denotes ability e.g Comparable, 'Serializable' etc.

Here, Breeading, Talking, Walking, Swimming are ability of certain person. So an entity has some ablity. Therefore i make all those independent abilities as interfaces and entity(Swimmer, Talker, Walker) will implement those interface to get those ablities.

interface Breedable {
    public void breed();
}

interface Walkable {
    public void walk();
}

interface Talkable {
    public void talk();
}

interface Swimmable {
    public void swim();
}

class Swimmer implements Swimmable, Walkable, Talkable, Breedable {

    @Override
    public void breed() {
        // TODO Auto-generated method stub

    }

    @Override
    public void talk() {
        // TODO Auto-generated method stub

    }

    @Override
    public void swim() {
        // TODO Auto-generated method stub

    }

    @Override
    public void walk() {
        // TODO Auto-generated method stub

    }

1 Comment

You would not want Swimmable to extend Walkable or Talkable. What if you needed to define a person who could swim but not talk? What if you wanted Swimmer to no longer be able to walk? Having Swimmable extends those interfaces makes it difficult to make those changes without having a lot of side-effects (same goes for extending Breedable). It would greatly simplify things to just have Swimmer implement Walkable, Talkable, Breedable, and Swimmable.
-1

I think what you should have is something like that.

public class Person
//Breed method here

public class Walker extends Person
//Walk method here

public class Talker extends Person
//Talk method here

public class Swimmer extends Person
//Swim, talk & walk method here

But what you should really do is thinking about separation of concerns. Is there enough common points between each of these objects to use inheritance? Maybe not.

1 Comment

If you do it this way, it will be very difficult to implement a method somewhere that, say, doesn't care what type of person it's operating on, as long as it can talk.
-1
class person{
 //breed function
 }
 class walker extends person{
 //walk function
 }
 class talker extends person{
 //talk function
 }
class swimmer extends talker{     
//swim function
//walk function
}

2 Comments

If you do it this way, it will be very difficult to implement a method somewhere that, say, doesn't care what type of person it's operating on, as long as it can talk.
(Also, you could have chosen swimmer extends walker - with this structure you are forced to decide if a swimmer is more of a walker or a talker; there is not a clear boundary.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.