Disabling inherited method on derived class

后端 未结 10 1696
野趣味
野趣味 2020-12-03 06:59

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 Shap

相关标签:
10条回答
  • 2020-12-03 07:09

    You can override the specific method "rotate()" in the class you want to disable this operation, like this

    public void rotate() {
        throw new UnsupportedOperationException();
    }
    
    0 讨论(0)
  • 2020-12-03 07:10

    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.
    0 讨论(0)
  • 2020-12-03 07:13

    So there are two possibilities here. One is that you have an object that can rotate, but shows no change in the process. The other is that you have an object that is defined by it's direction. Each ought to be handled separately and in their own way.

    So for circle, we have a stub for a rotate function since there is no change in the system. Don't like it, too bad. I'm putting off my quiz on point groups to write this.
    For upward arrow, we throw an exception since we don't want to drive the wrong way on a one-way street and this ought to be constant anyway.

    In my case, I'm defining a point in 3 dimensional space by inheriting operations from a class defining a quaternion. The 3 dimensional point only uses the 3 imaginary coordinates of the quaternion and if the quaternion has a non-zero value for the real component, it will ruin the vital operations of the rendering algorithm inherited from the quaternion via the 3 dimensional coordinate.

    Excuse me for being overly philosophical, however, this question raises a valid point in CS about how this should be handled. As Stephen notes, being able to hide, disable or remove the inherited function in the child class "...would violate the substitutability principle and would break polymorphism."

    My case falls under the upward arrow, except I'm pointing into imagination. I am attempting to truncate a derived class, which means any 'grandchild' class (possibly inheriting spin operators) would no longer necessarily be quaternions. It would be more elegant to avoid resorting to throwing exceptions, by trimming the base class at the typecast for a truncating derived class. The alternative would be defining the 3 dimensional coordinate as a separate class with a quaternion member variable; the drawback is that the implementation has too much glue.

    Then I can put the new spin operations into the derived class for the 3 dimensional coordinates for the three planes (ij, jk, ik) and then the objects with 3d vertices can be rotated via a loop and an identical member function call for each vertex. You might could keep track of the symmetry on your own, before you start rotating the points defining the surface of sphere, which is why you need to use the stub function and possibly implement your own 'constant' flag.

    0 讨论(0)
  • 2020-12-03 07:17

    One way to deal with this is to define a second method called (say) boolean isRotatable(), and use this to determine whether the rotation controls are made available to the user.

    Another option would be to introduce a Rotatable interface and use shape instanceof Rotatable to make the determination. (However, I think the isRotatable() approach is more flexible.)

    In either case, you can implement the rotate() method on a class that should never be rotated as:

    public void rotate() {
        throw new UnsupportedOperationException("rotate");
    }
    

    The Java language doesn't provide a way to "remove" or "disable" a method in a subclass. That would violate the substitutability principle and would break polymorphism. A subclass cannot remove visible members from the parent classes API.

    0 讨论(0)
  • 2020-12-03 07:19

    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
    }
    
    0 讨论(0)
  • 2020-12-03 07:22

    I needed the same thing for a custom alertdialog. I needed to pass an argument that became available only at the show moment, so I implemented my show(argument) method, but show() worked too because of the superclass. There was a risk of that I could invoke myDlg.show() by accident, without an argument, and that would cause a crash. To make sure the superclass method is never called outside my CustomAlertDialog class, I added the method private static void show(){} to it. That's it.

    So, to disable a supermethod, make it private and static in the subclass. In your case, it would be:

     private void rotate(){}
    
    0 讨论(0)
提交回复
热议问题