In Java, overriding refers to the ability of a subclass to provide a specific implementation of a method that is already defined in its parent class. This allows the subclass to customize the behaviour of the inherited method to better suit its own requirements.
To override a method in Java, the subclass must provide a method with the same name, return type, and parameter list as the method being overridden in the parent class. The access level of the overriding method can be the same or less restrictive than the access level of the overridden method. However, it cannot be more restrictive than the access level of the overridden method.
When a method is overridden in Java, the version of the method that is called depends on the type of object that is being referred to at runtime. If the object is an instance of the subclass, the subclass implementation of the method will be called. If the object is an instance of the parent class, the parent implementation of the method will be called.
It is important to note that not all methods in Java can be overridden. Only methods that are marked as “virtual” or “non-final” in the parent class can be overridden by a subclass. Additionally, constructors and private methods cannot be overridden.
Overriding in Java is a powerful feature that allows developers to build hierarchies of related classes with customized behaviour. By carefully designing class hierarchies and method implementations, developers can create software that is flexible, maintainable, and extensible over time.
Java: Socket Programming Simplified
Last Updated: 2022-04-21
Build the foundations for server side programming, a must learn for backend
Rules for Method Overriding in Java
In Java, method overriding is a feature that allows a subclass to provide a specific implementation of a method that is already defined in its parent class. Here are the rules that govern method overriding in Java:
The name of the method being overridden must be the same in both the parent class and the subclass.
- The method signature in the subclass must be the same as the method signature in the parent class. This includes the return type, the parameter types, and the order of the parameters.
- The access level of the overriding method in the subclass cannot be more restrictive than the access level of the overridden method in the parent class. For example, if the parent class method is public, the subclass method can be public or protected, but it cannot be private or package-private.
- The overriding method in the subclass can throw the same exceptions as the overridden method in the parent class, or a subclass of those exceptions. However, it cannot throw any additional checked exceptions that are not declared by the overridden method.
- The behaviour of the method in the subclass must be consistent with the behaviour of the method in the parent class, as far as the caller is concerned. In other words, the subclass method must fulfil the contract of the parent class method.
- Constructors and private methods cannot be overridden in Java.
- Static methods cannot be overridden in Java, but they can be shadowed by a subclass method with the same signature.