Wednesday, January 15, 2025
HomeTechWhat is Overriding in Java

What is Overriding in Java

Method overriding is a feature in Java that allows a subclass to provide a specific implementation of a method that is already defined in its superclass. The method in the subclass must have the same name, return type, and parameters (i.e., the same method signature) as the method in the superclass.

Key Points of Method Overriding:

  1. Same Method Signature: The method in the subclass should have the same name, return type, and parameter list as the method in the superclass.
  2. Inheritance: Overriding happens when a subclass inherits from a superclass and modifies an inherited method.
  3. Runtime Polymorphism: Method overriding is a key feature of runtime polymorphism in Java, meaning that the method that is called is determined at runtime based on the object type (not the reference type).
  4. @Override Annotation: It is not mandatory, but it is a good practice to use the @Override annotation to indicate that a method is overriding a method in the superclass. This helps in catching errors like mismatched method signatures.

Syntax of Method Overriding

class Parent {
    // Method in the superclass
    void display() {
        System.out.println("Display from Parent");
    }
}

class Child extends Parent {
    // Overriding the method in the subclass
    @Override
    void display() {
        System.out.println("Display from Child");
    }
}

public class Main {
    public static void main(String[] args) {
        Parent p = new Parent();
        p.display();  // Output: Display from Parent
        
        Child c = new Child();
        c.display();  // Output: Display from Child
    }
}

Explanation of the Example:

  1. Parent Class: The Parent class has a method display() that prints "Display from Parent".
  2. Child Class: The Child class extends the Parent class and overrides the display() method to print "Display from Child".
  3. Output:
    • When we call p.display() (where p is a Parent object), the method from the Parent class is executed.
    • When we call c.display() (where c is a Child object), the method from the Child class is executed.
See also  AI Techniques of Knowledge Representation

Rules for Method Overriding in Java

  1. Same Method Signature: The method in the subclass must have the same name, return type, and parameter list as the superclass method.
  2. Access Modifier: The access level of the overriding method cannot be more restrictive than the method in the superclass. For example, if the superclass method is public, the subclass method must be public as well.
  3. Return Type: The return type of the overriding method can be the same as the superclass method or a subclass type (covariant return type).
  4. Exceptions: The overriding method can throw fewer or the same exceptions as the method in the superclass, but not more or different exceptions.
See also  What is the best website builder you've used?

Example: Overriding with Return Type and Exceptions

class Animal {
    // Method in the superclass
    public void sound() throws Exception {
        System.out.println("Animal makes a sound");
    }
}

class Dog extends Animal {
    // Overriding method in subclass
    @Override
    public void sound() throws Exception {
        System.out.println("Dog barks");
    }
}

public class Main {
    public static void main(String[] args) {
        Animal myDog = new Dog();
        try {
            myDog.sound();  // Output: Dog barks
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Explanation:

  • Superclass Animal has a sound() method.
  • Subclass Dog overrides sound() to print "Dog barks".
  • The sound() method in both classes has the same signature, and Dog can throw the same type of exception (or fewer exceptions).

Why Use Method Overriding?

  • Dynamic Method Dispatch: Method overriding supports runtime polymorphism, where the method called is determined at runtime based on the object type.
  • Customization: It allows a subclass to customize or extend the behavior of a method inherited from a superclass without changing the superclass code.
  • Code Reusability: By overriding methods, subclasses can reuse the logic in the superclass and add their own specific logic.
See also  Python String Replace: Replace \ with /

Method Overriding vs. Method Overloading

  • Method Overriding:
    • Occurs when a subclass provides a specific implementation of a method inherited from a superclass.
    • It is based on the runtime polymorphism.
    • Method signature must be the same.
  • Method Overloading:
    • Occurs when two or more methods in the same class have the same name but different parameter lists.
    • It is based on compile-time polymorphism.
    • Method signature must be different (parameters vary).

Conclusion

Method overriding in Java is a powerful feature that allows subclasses to provide specific implementations of methods inherited from superclasses. This promotes polymorphism, flexibility, and code reusability in object-oriented programming. By overriding methods, you can customize behavior in subclasses while still maintaining the structure and behavior defined by the superclass.

RELATED ARTICLES
0 0 votes
Article Rating

Leave a Reply

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
- Advertisment -

Most Popular

Recent Comments

0
Would love your thoughts, please comment.x
()
x