Method Overriding in JavaIf subclass (child class) has the same method as declared in the parent class, it is known as method overriding in Java. In other words, If a subclass provides the specific implementation of the method that has been declared by one of its parent class, it is known as method overriding. Usage of Java Method Overriding
Rules for Java Method Overridingh2h3>Let's understand the problem that we may face in the program if we don't use method overriding. Bike.java Output: Vehicle is running Explanation "Vehicle is running" is printed by the run() function of the Vehicle class. We construct an instance of the Bike class and use it to invoke the run() method within the Bike class. As a result of Bike deriving from Vehicle, the run() function of the Vehicle class is overridden in the Bike class, resulting in the print "Vehicle is running" when the method is used on a Bike object. This demonstrates how method overriding, in which the method specified in the subclass overrides the method in the superclass with the identical signature, can result in polymorphic behaviour. Example of Method OverridingIn this example, we have defined the run method in the subclass as defined in the parent class, but it has some specific implementation. The method's name and parameters are the same, and there is an IS-A relationship between the classes, so there is method overriding. Bike2.java Output: Bike is running safely Explanation A method in a subclass (Bike2) overrides the same method in its superclass (Vehicle), as this Java program illustrates. The run() method in this example is shared by both types, but the Bike2 class implements it differently, outputting "Bike is running safely." The overridden function in the Bike2 class gets executed when we create an instance of Bike2 and use the run() method on it, proving that the implementation of the subclass takes precedence over the implementation of the superclass. This demonstrates how Java's dynamic polymorphism feature allows methods with the same signature to behave differently in various classes, even if they are part of the same inheritance tree. A real example of Java Method OverridingConsider a scenario where Bank is a class that provides functionality to get the rate of interest. However, the rate of interest varies according to banks. For example, SBI, ICICI and AXIS banks could provide 8%, 7%, and 9% rate of interest. Test2.java Output: SBI Rate of Interest: 8 ICICI Rate of Interest: 7 AXIS Rate of Interest: 9 Explanation This Java programme uses a real-world scenario where three classes-SBI, ICICI, and AXIS-override a method from their parent class, Bank, to demonstrate the idea of method overriding. The getRateOfInterest() function of the Bank class yields 0. With their own implementation, each of the child classes overrides this method: SBI returns 8, ICICI returns 7, and AXIS returns 9. Each child class object is created in the Test2 class, and then each object's getRateOfInterest() method is used to print out the corresponding interest rates for each bank. This illustrates how polymorphic behaviour dependent on the type of object at runtime is made possible by method overriding, which enables each subclass to give its own implementation of a method inherited from the superclass. Can we override the static method?No, a static method cannot be overridden in Java. When a subclass defines a static method with the same signature as a static method in its superclass, it is simply hiding the superclass method, not overriding it. This means that the method invoked is determined at compile time based on the reference type, not at runtime based on the object's type. Therefore, static methods do not exhibit polymorphic behavior like instance methods do. Why can we not override static method?Because static methods in Java are linked to the class itself rather than any specific instance of the class, we are unable to override them. Java's dynamic dispatch mechanism, which determines the method to be called at runtime depending on the object's actual type, forms the foundation for method overriding. Static methods cannot be overridden in the same manner as instance methods since they are not subject to dynamic dispatch because they are resolved at compile time based on the reference type and are part of the class. Furthermore, while instance methods are kept in the object's heap area and are specific to each instance of the class, static methods are kept in the method region of the JVM's memory, which is shared by all instances of the class. Static methods cannot be altered due to the basic differences in their behaviour and memory allocation between instance and static methods. Can we override Java main() method?No, because the Java main() method is designated as static, we are unable to override it. The main function in Java is declared as public static void main(String[] args) and acts as the program's starting point. The method in question is part of the class itself, not any particular instance of the class, as indicated by the use of the static keyword. It is not possible to override the main method in a subclass since static methods are not overridable. As the initial point of execution, each Java program must contain exactly one main method that has the required signature. Method Overloading Vs. Method Overriding
Java Access Modifiers with Method OverridingIf you are overriding any method, overridden method (i.e. declared in subclass) must not be more restrictive. Simple.java Output: Explanation Class A declares a method called msg() with the protected access modifier in the provided Java code. By extending class A, class Simple aims to replace the message() function with a default access modifier. But because the default access modifier is more restricted than protected, this leads to a compile-time error. When overriding a method in Java, the overridden method's access level in the subclass needs to be at least as liberal as the method's access level in the superclass. Thus, in order to fix the issue, either the class Simple method's access modifier-such as protected or public-should be as liberal as protected, or the class A method should have a less permissive access modifier-such as default or public. ConclusionIn Java, method overriding is essential to creating polymorphism at runtime and to promote modularity and reusability of programs. A subclass improves the codebase's flexibility and adaptability by offering a particular implementation of a method stated in its superclass. By enabling subclasses to modify inherited methods' behavior to suit their unique needs, method overriding encourages more modular and maintainable design. However there are limitations to overriding methods, such as not being able to override static methods and having to follow access modifier limits. These restrictions, which are a reflection of inheritance and encapsulation, are built into the Java language design. In conclusion, Java's method overriding feature enables subclasses to offer customized implementations of inherited methods, enabling programmers to write more adaptable, modular, and maintainable code. Through strict adherence to method-overriding guidelines and best practices, developers can fully use the benefits of object-oriented programming concepts to create dependable and expandable software systems. Next TopicCovariant Return Type |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India