Does a subclass need to rewrite each method of the superclass or it isn't mandatory to do so but the subclass can override some methodes of the superclass. I am a bit confused.
Subclasses are not required to override any parent methods, even though they can. The only way to force a child class to override a method is to make that method abstract (and the parent class has to be abstract as well).
If a subclass wishes to introduce unique behavior for invocation of a method that it inherited, then that method must be overridden.
The most common cases for this: toString
, equals
and hashCode
are all eligible to be overridden for all of your custom classes, given that your custom class doesn't want to leverage Object#toString
, Object#equals
or Object#hashCode
, which are all "unhelpful" for your custom implementations.
If a subclass does not wish to introduce unique behavior for invocation of a method that it inherited, then this is unnecessary. You can rely on the parent class' behavior instead.
If your parent class is abstract
, then you have no choice but to implement what methods the parent class chose not to implement.
If you're implementing an interface, the same principle as abstract classes applies - because the interface does not implement anything itself, you must implement the methods that the interface prescribes.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.