30 June 2023

Method Overriding In Java - learngreen.net

 

Method Overriding In Java


Method overriding is a feature in object-oriented programming languages, that lets in a subclass to furnish a one-of-a-kind implementation of a method that is already described in its superclass.


In Java, when a subclass inherits a method from its superclass, it can redefine that approach with the equal name, return type, and parameters in the subclass. The overridden method in the subclass replaces the implementation of the approach inherited from the superclass.


When the overridden method is referred to as on an instance of the subclass, the subclass's implementation is performed as an alternative of the superclass's implementation.


This approves the subclass to customize the conduct of the method to suit its specific needs, while still maintaining the approach signature defined in the superclass.


Method overriding permits polymorphism, which ability that a variable of a superclass type can refer to an object of both the superclass or any of its subclasses, and the appropriate overridden technique will be invoked based totally on the actual type of the object.


Some necessary points to have in mind about method overriding in Java are:-


1. The method in the subclass must have the identical name, return type, and parameters as the method in the superclass.


2. The get admission to stage of the overridden method in the subclass can't be greater restrictive than the access level of the superclass method. It can be the equal or more permissive (e.g., a public method in the superclass can be overridden as public or protected in the subclass).


3. The overriding method in the subclass can use the @Override annotation to point out that it intends to override a method from the superclass. This annotation helps become aware of mistakes at compile-time if the approach signature does not in shape any approach in the superclass.


4. The super key-word can be used internal the subclass to explicitly call the overridden method from the superclass, permitting both implementations to be executed.


5. The final key-word can be used to prevent a method from being overridden. A remaining method in a superclass cannot be overridden by means of any subclass.


Method overriding lets in for flexibility and extensibility in object-oriented programming with the aid of enabling subclasses to grant their very own implementation of methods defined in their superclass. It promotes code reusability, modular design, and the implementation of particular conduct in subclasses while keeping a constant interface across the inheritance hierarchy.




 package Interview;
 public class MyClass {
 // Method in the superclass
 public void eat() {
 System.out.println("David eats mango ");
    }

 // Inner class extending the superclass
 public class Shyam extends MyClass {
 // Method overriding the superclass method
 @Override
 public void eat() {
 System.out.println("Mack eats grapes");
        }
    }
 public static void main(String[] args) {
 // Create an instance of the superclass
 MyClass obj = new MyClass();
 // Call the eat() method of the superclass
 obj.eat();
 /*
  *  Create an instance of the inner
  *  class using the outer class instance 
  */
 Shyam obj1 = obj.new Shyam();
 /*
  * Call the eat() method of the
  * inner class (overridden version) 
  */
 obj1.eat();
 // Output->
 // David eats mango 
 // Mack eats grapes
    }
}

 



No comments:

Post a Comment