wood burning stoves 2.0*
The moose likes Java in General and the fly likes Object Orientation & Polymorphism Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Object Orientation & Polymorphism" Watch "Object Orientation & Polymorphism" New topic
Author

Object Orientation & Polymorphism

S Jothimani
Greenhorn

Joined: Jun 29, 2011
Posts: 9

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

Joined: Jan 28, 2008
Posts: 5575

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

Joined: Jan 28, 2008
Posts: 5575

use code tag when you post your code next time
S Jothimani
Greenhorn

Joined: Jun 29, 2011
Posts: 9
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

Joined: Jan 28, 2008
Posts: 5575

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

Joined: Jun 29, 2011
Posts: 9
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

Joined: Jan 28, 2008
Posts: 5575

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

Joined: Jun 29, 2011
Posts: 9
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

Joined: Oct 13, 2005
Posts: 38353
    
  23
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

Joined: May 08, 2006
Posts: 79
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...



SCJP 1.4, SCWCD 1.4
S Jothimani
Greenhorn

Joined: Jun 29, 2011
Posts: 9
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

Joined: Oct 13, 2005
Posts: 38353
    
  23
You're welcome
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Object Orientation & Polymorphism