This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Disabling a method in child class

 
Ismael Upright
Ranch Hand
Posts: 166
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Sierra's book [Chapter 2, p. 96] it is mentioned that there is a possibility of disabling the method from parent class in some of the the child classes. How it is possible? How to do that?


The example is following:

 
Panseer Kaur
Ranch Hand
Posts: 44
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not sure what you mean by disabling, though I suppose one possible way to disable it would be to do it would be

class GameShape {
public void animate() {/*here goes the animating code*/}
}

class PlayerPiece extends GameShape {
public void animate() {//just do nothing here and the child class won't be
// have any access to it
}
}


Sorry if that isn't what you meant though
 
Ismael Upright
Ranch Hand
Posts: 166
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm also not sure, I'm trying to figure out that Sierra had on mind
 
Julio Eneriz
Greenhorn
Posts: 15
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the book it's said that you could do that but it's not a good design... so don't waste too much effort trying to learn bad designs (but I understand curiosity).

I guess you could do it like in the posted example, or you can put a void code in each class that doesn't really use the method, or you could make it final so children could never implemented it...
 
Vidhya Ramaswamy
Ranch Hand
Posts: 65
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ismael,
The answer to your question is right in the book itself: Using interfaces.
Given on Page 96:
You could simply put the animate() code in GameShape, and then disable the method in classes that can't be animated. But that's a bad design choice.

The code for the Animatable interface is also given.

Hope your question is answered.
 
Ismael Upright
Ranch Hand
Posts: 166
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vidhya, there are two solutions for the problem of disable the animate() method code given at page 96. One is not directly given. It is only named as 'bad design'. Second is the one about using interfaces, which cannot be named as 'bad design'. I believe you talk about the second solution, while I'm interested in first one


Julio, I'm not sure if making the method final in parent class would be a good solution since the goal is to disable it only to some of the children classes.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic