File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Disabling a method in child class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Disabling a method in child class" Watch "Disabling a method in child class" New topic

Disabling a method in child class

Ismael Upright
Ranch Hand

Joined: Feb 15, 2007
Posts: 166
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

Joined: Nov 01, 2007
Posts: 44
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

Joined: Feb 15, 2007
Posts: 166
I'm also not sure, I'm trying to figure out that Sierra had on mind
Julio Eneriz

Joined: Nov 28, 2007
Posts: 15
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

Joined: Oct 10, 2007
Posts: 65
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

Joined: Feb 15, 2007
Posts: 166
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:
subject: Disabling a method in child class
jQuery in Action, 3rd edition