Disabling inherited method on derived class

后端 未结 10 1719
野趣味
野趣味 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: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
    }
    

提交回复
热议问题