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
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.
Can you use an empy (does nothing) method for the circle? For the arrow I would reconsider object hierarchy
I don't think you can disable a method in the way you suggest.
In your example, lets say you have a method that takes a Shape
public void handleShape(Shape s){
s.rotate();
}
Then you pass a Circle to this method
handleShape(new Circle());
What should happen? Essentially you are asking for a fundamental change to Java's type system.
If Circle is a Shape and shouldn't be rotated then it probably means that Shape was designed poorly and shouldn't have a rotate method. You can add rotate to a different class int the hierarchy like, RotatableShape or possibly use an interface Rotatable.
Declaring the same function in the 'child'-class will overwrite the default function declared in the base class. So in your child-class, make a function called rotate()
which does nothing, that will overwrite the default behaviour