• 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

Multiple inheritance Simple problem

 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi i just want to know how my problem that is stated below could be achieved .the example would be lengthy but vey simple . i have three classes mother,father and child


class mother
{
public void eat()
{
System.out.println("eat:Mother's Style");
}
public void play()
{
System.out.println("play:Mother's Style");
}
public void talk()
{
System.out.println("talk:Mother's Style");
}
public void walk()
{
System.out.println("walk:Mother's Style");
}
public void look()
{
System.out.println("look:Mother's Style");
}
}

class father
{
public void eat()
{
System.out.println("eat:father's Style");
}
public void play()
{
System.out.println("play:father's Style");
}
public void talk()
{
System.out.println("talk:father's Style");
}
public void walk()
{
System.out.println("walk:father's Style");
}
public void look()
{
System.out.println("look:father's Style");
}

}

class child inherits both mother and father
{
int eatStyle;
int playStyle;
int walkStyle;
int lookStyle;
int talkStyle;

child(int a,int b,int c,int d,int e)
{
eatStyle = a;
playStyle = b;
walkStyle = c;
lookStyle = d;
talkStyle = e;

}
public void eat()
{
if(eatStyle == 1)
// call father's eat()
else if(eatStyle == 2)
// call mother's eat()
else
// implement his own
}
public void play()
{
if(playStyle == 1)
// call father's play()
else if(playStyle == 2)
// call mother's play()
else
// implement his own
}
public void talk()
{
if(talkStyle == 1)
// call father's talk()
else if(talkStyle == 2)
// call mother's talk()
else
// implement his own
}
public void walk()
{
if(walkStyle == 1)
// call father's walk()
else if(walkStyle == 2)
// call mother's walk()
else
// implement his own

}
public void look()
{
if(lookStyle == 1)
// call father's look()
else if(lookStyle == 2)
// call mother's look()
else
// implement his own
}


}

hi i have a set of classes like described above so if someone creates child sam = new child(1,1,2,3,3)
then "sam" would eat and play like his father and walk like his mother and others he does in his unique way.
but since java doesnt have multiple inheritance how can this be implemented . argumnets suggests that multiple inheritance is complex and confusing . but this is a obvious case in the real world .but how this can be achieved. this question was in my mind right from the day i studied inheritance in java.
expecting the answer


thanks and regards

ashok
thanks and regards

ashok
 
Ranch Hand
Posts: 332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, I can suggest using interfaces and strategy Pattern.
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This does not look SCJPish to me. Please post such questions in one of our Java In General forums. in this case I am moving it to JIG (Intermediate)...
 
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It seems to me that instead of child sam = new child(1,1,2,3,3), you want:

Child sam=new Child(mother.walk,father.run,mother.play), etc.

Unfortunately, Java doesn't support method references, so my suggested way is to make walk, run, play, look, etc., Runnables.



Then to instantiate the mother:


..etc.

Person mother=new Person(mothersWalk,mothersLook,etc.);

Same kind of thing for a father, then to make a child:

Child sam=new Child(mother.walk,father.run,mother.play)
And to make sam walk, sam.walk.run();

Perhaps walk.run() is a bit confusing, so you might provide wrappers in the Person class, e.g.:

Then sam can still sam.walk();

Don't use magic numbers or magic constants to represent other things, you can almost always use the things directly instead.
 
Ricky Clarkson
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh, and try not to think in terms of inheriting behaviour, you'll only slow down your own development by doing so.
 
ashok ganesan
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi , sorry barry i was bit confused to choose an appropriate forum . and thanks ricky for your implementation . i could achieve wat i really meant . but i need a generalized answer like whenever i use to explain to someone that java don't have multiple inheritance because to avoid the complexity like ambiguity and diamond problem they immediately through up with an example like i cited and like my example there are numerous in this real world where one entity could inherit the behavior from 2 different entities . in general how this problem is solved or programatically how this is achieved in java.
so every time do we need to follow the method posted by ricky or do we have to follow startegy pattern or is there a generalized concept or implementation that we could follow for reflecting multiple inheritance

---
ashok
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What you are showing in your example doesn't look like multiple inheritance to me. What I mean by that is you don't seem to REALLY want all the behaviors of both parent objects - you're just using MI to give your child class the option of selecting one or the other. can you imagine if you had 10 or so possible walk() methods? you'd have these enormous if - else if - else if - else - if... statements. every time someone writes a new walk() method, you have to go and tweak this monstrous block of code.

with the strategy design pattern, you separate the behavior into it's own set of objects. you then in your child class can assign the behavior object to handle the specific type of behavior you need. And what's even cooler is you can CHANGE it at run-time. Once your child object has been around a while, you may want to give it an adult walk style.

Do some reading on Design Patterns. there are many good books, but one that is easily accessible is the "Head First Design Patterns". It is well worth the money.
 
ashok ganesan
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi fred ,
here i m trying to inherit some of the behavior of the parents and for the rest i m providing unique behavior to the child.... so i think this is MI . and as you said the if - else will be a problem if want 10 or more walk methods as what ricky and you suggested i tried the same problem with strategy pattern and it gives me more option like changing it at runtime .... so thanks all for spending your valuable time
 
Ricky Clarkson
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's not inheritance. You're talking about code reuse. Inheritance (more accurately, substitutability) is where you make a new class whose instances can be treated as if they were instances of a superclass.

The next version of Java will likely make my above implementation of the strategy pattern more attractive to write, e.g:

Runnable walk={ => System.out.println("Ok, I'm walking"); };
[ May 14, 2007: Message edited by: Ricky Clarkson ]
 
Goodbye moon men. Hello tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic