Disabling inherited method on derived class
Solution 1
I don't think it is possible. However you can further refine the Shape class by removing the rotate() method from its specification and instead define another subclass of Shape called RotatableShape and let Circle derive from Shape and all other Rotatable classes from RotatableShape.
e.g:
public class Shape{
//all the generic methods except rotate()
}
public class RotatableShape extends Shape{
public void rotate(){
//Some Code here...
}
}
public class Circle extends Shape{
//Your implementation specific to Circle
}
public class Rectangle extends RotatableShape{
//Your implementation specific to Rectangle
}
Solution 2
You can override the specific method "rotate()" in the class you want to disable this operation, like this
public void rotate() {
throw new UnsupportedOperationException();
}
Solution 3
You have a bad class heirarchy if you need to disable methods in child classes. Any subclass should be able to smoothly use the methods of its superclasses. This is called the "Liskov Substitution Principle" (https://en.wikipedia.org/wiki/Liskov_substitution_principle). You can read more about this in this thread: https://softwareengineering.stackexchange.com/questions/219543/should-a-class-know-about-its-subclasses.
Do what Chandu suggests. Don't put rotate() in Shape. Instead, make a subclass of Shape called RotatableShape, and put rotate() in there. Then Circle can inherit from Shape, and Rectangle can inherit from RotatableShape.
Solution 4
No.
- you can pull the method down in only the
Circle
class (or an interface that is implemented only by that class) - you can provide an empty implementations or ones that throw
UnsupportedOperationException
in the classes that do not support it.
Solution 5
Can you use an empy (does nothing) method for the circle? For the arrow I would reconsider object hierarchy
![Jean-François Corbett](https://i.stack.imgur.com/fK5bc.jpg?s=256&g=1)
Jean-François Corbett
I'm a physicist / software enthusiast working in wind power. https://www.linkedin.com/in/jfcorbett/ I used to code on this, hooked up to our TV with a coaxial: That's color, baby!
Updated on June 09, 2020Comments
-
Jean-François Corbett about 4 years
Is there any way to, in a Java derived class, "disable" a method and/or field that is otherwise inherited from a base class?
For example, say you have a
Shape
base class that has arotate()
method. You also have various types derived from theShape
class:Square
,Circle
,UpwardArrow
, etc.Shape
has arotate()
method. But I don't wantrotate()
to be available to the users ofCircle
, because it's pointless, or users ofUpwardArrow
, because I don't wantUpwardArrow
to be able to rotate. -
helpermethod over 13 years+1 Much better solution than throwing an
UnsupportedOperationException
. -
Bozho over 13 years@Jean-François Corbett that's the eclipse terminology. This means you move it from the superclass to the subclass.
-
Admin over 13 yearsClean solution, easy and elegant +1
-
clwhisk almost 8 yearsLSP is hardly repeating
-
Joshua Pinter over 7 yearsWhat about implementing a
Rotatable
interface or something, likepublic class Circle extends Shape implements Rotatable {
? Is that possible? Not? Preferred? (I'm not an expert) -
Brian Risk about 7 yearsThis answers the more general question where maybe the developer is not also creating the superclass.
-
Paul Rooney about 7 yearsThe mention of liskov substitution principle is a valuable addition to this page. Something the accepted answer (or any of the other answers) fails to mention.
-
Nick Crews about 7 yearsCheck out the Liskov Substitution Principle, which is the reason for your confusion, and the reason for this answer. Basically, it says any subclass had better be able to act just like its super class.
-
Alexander Samoylov over 5 yearsExactly. All the answers posted here at the moment don't consider that case if the base class might be a standard class which you are not able to reimplement. Maybe it is not a really good practical example, but let imagine you want to implement a FastHashMap derived from HashMap, but you want to allow only putIfAbsent() method and disallow put() method (putIfAbsent() exists since Java 8).