How to override a method in Java?
If a subclass has a method that have the same signature with a method in its superclass, then it's an overriding method. Override inherited methods allow subclasses to provide specialized implementation for those methods. The overriding method has the same name, number and type of arguments, and return value as the method it overrides.
The overriding method can have a different throws clause as long as it doesn't specify any types not specified by the throws clause in the overridden method. Also, the access specifier for the overriding method can allow more but not less access than the overridden method.
For example, a protected method in the superclass can be made public but not private. A subclass cannot override methods that are declared final in the superclass. A subclass must override methods that are declared abstract in the superclass, or the subclass itself must be abstract.
In the Truck class we override the getCarInfo() method to provide more information about the truck object. We can also add an @Override annotation to an overriding method.