Disabling inherited method on derived class

32,843

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

Share:
32,843
Jean-François Corbett
Author by

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, 2020

Comments

  • Jean-François Corbett
    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 a rotate() method. You also have various types derived from the Shape class: Square, Circle, UpwardArrow, etc.

    Shape has a rotate() method. But I don't want rotate() to be available to the users of Circle, because it's pointless, or users of UpwardArrow, because I don't want UpwardArrow to be able to rotate.

  • helpermethod
    helpermethod over 13 years
    +1 Much better solution than throwing an UnsupportedOperationException.
  • Bozho
    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
    Admin over 13 years
    Clean solution, easy and elegant +1
  • clwhisk
    clwhisk almost 8 years
    LSP is hardly repeating
  • Joshua Pinter
    Joshua Pinter over 7 years
    What about implementing a Rotatable interface or something, like public class Circle extends Shape implements Rotatable {? Is that possible? Not? Preferred? (I'm not an expert)
  • Brian Risk
    Brian Risk about 7 years
    This answers the more general question where maybe the developer is not also creating the superclass.
  • Paul Rooney
    Paul Rooney about 7 years
    The 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
    Nick Crews about 7 years
    Check 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
    Alexander Samoylov over 5 years
    Exactly. 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).