• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Object Orientation & Polymorphism

 
S Jothimani
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I need animate method in TilePiece & DicePiece class.
But java has no multiple inheritance feature, SCJP book (by Kathy Sierra, Bert Bates) suggest that
we can create one interface as below (Refer pg:100)

If i do like they suggest, then i need to implement animate method in both the classes.
Inheritance is of course for code reuse.

Is implementing the interface, the correct solution for my above requirement ?
Kindly suggest
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you can go for strategy pattern to solve this problem. this pattern uses composition for code reuse.



<edit>Welcome to JavaRanch </edit>
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
use code tag when you post your code next time
 
S Jothimani
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks seetharaman.

But instead of this approach, i can simply use the Animatable class's animate method. Why we go for interface again ?
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
S Jothimani wrote: Why we go for interface again ?

It is good to have an interface as a type. so that in future you can pass another class(say ImprovedAnimatableImpl) to DicePiece . without any changes in DicePiece.
 
S Jothimani
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks again.
But for this scenario, i thought, i don't want any interface.

Kindly clarify, if i interpretted your statement
so that in future you can pass another class(say ImprovedAnimatableImpl)
wrongly.

My interpretation :
If ImprovedAnimatableImpl has improved version of animate() method ,then using interface is useful, but if that improved class has additional methods then i need to change my code obviously.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
S Jothimani wrote: but if that improved class has additional methods then i need to change my code obviously.

but those method not there in that interface. so with interface reference , you cant access these methods. interface wont allow you to use class specifics implementation.
 
S Jothimani
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Seetharaman.

But can we achieve multiple inheritance in Java via implementing the interface.
I can use the classes of that implemented interface type i.e DicePiece can use animate() method using dot operator but cannot inherit it.

Can i use the class AnimatableImpl as a superclass i.e i can say DicePiece is a GameShape but is DicePiece an Animatable ?

Please clarify.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49361
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
S Jothimani wrote:. . . But can we achieve multiple inheritance in Java via implementing the interface. . . .
No, you can't. You can only mimic multiple inheritance with interfaces.

If your superclass implements an interface, yes, all its subclasses implement that interface too, so their instances are instances of that interface.
 
Arun Chidam
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jothimani,

other way around for reusing same code is to use abstract class which implement Animate Interface and reuse animate function in PlayerPiece and TilePiece class
Please see below example of abstract class which extends GamePlan class and provide functionality for Animate method...


 
S Jothimani
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ritchie.

Thanks Arun. Your solution can be a solution for my requirement.
Also, i can simply use normal class instead of abstract class if both TilePiece & DicePiece has same animate property.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49361
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic