Data Types and Operators
(Non-Primitive Data Types)
Control Flow Statements
Conditional Statements
Looping Statements
Branching Statements
Object-Oriented Programming (OOP)
Exception Handling
Collections Framework
Overview of Collections
Java I/O
Multithreading
GUI Programming with Swing
Advanced Topics
JAVA CODE
Java Basics
Working with Objects
Arrays, Conditionals, and Loops
Creating Classes and Applications in Java
More About Methods
Java Applet Basics
Graphics, Fonts, and Color
Simple Animation and Threads
More Animation, Images, and Sound
Managing Simple Events and Interactivity
Creating User Interfaces with the awt
Windows, Networking, and Other Tidbits
Modifiers, Access Control, and Class Design
Packages and Interfaces
Exceptions
Multithreading
Streams and I/O
Using Native Methods and Libraries
Under the Hood
Java Programming Tools
Working with Data Structures in Java
Advanced Animation and Media
Fun with Image Filters
Client/Server Networking in Java
Emerging Technologies
appendix A :- Language Summary
appendix B :- Class Hierarchy Diagrams
appendix C The Java Class Library
appendix D Bytecodes Reference
appendix E java.applet Package Reference
appendix F java.awt Package Reference
appendix G java.awt.image Package Reference
appendix H java.awt.peer Package Reference
appendix I java.io Package Reference
appendix J java.lang Package Reference
appendix K java.net Package Reference
appendix L java.util Package Reference

Method overriding in Java is a feature that allows a subclass (child class) to provide a specific implementation for a method that is already defined by its superclass (parent class). This is a fundamental aspect of object-oriented programming that supports polymorphism and dynamic method dispatch. Here’s a detailed explanation of method overriding in Java:

Key Concepts of Method Overriding

  1. Inheritance: Method overriding can only occur in the context of inheritance. A subclass inherits methods from a superclass and can override those methods to change their behavior.

  2. Same Method Signature: The method in the subclass must have the same name, return type, and parameters as the method in the superclass. If there is any difference in the method signature, it is not considered an override but an overload.

  3. Access Modifiers: The access level of the overriding method cannot be more restrictive than the method it overrides. For example, if the superclass method is protected, the overriding method in the subclass cannot be private. It can, however, be protected or public.

  4. Annotations: The @Override annotation is commonly used in Java to indicate that a method is intended to override a method in the superclass. This helps catch errors at compile time, such as if the method signature does not match.

  5. Instance Methods: Only instance methods can be overridden. Static methods belong to the class rather than any instance, so they cannot be overridden but can be hidden (though this is generally discouraged).

  6. Constructor: Constructors cannot be overridden.

Example of Method Overriding

Here’s a simple example to illustrate method overriding:

				
					// Superclass
class Animal {
    void makeSound() {
        System.out.println("Animal makes a sound");
    }
}

// Subclass
class Dog extends Animal {
    @Override
    void makeSound() {
        System.out.println("Dog barks");
    }
}

public class TestOverride {
    public static void main(String[] args) {
        Animal myAnimal = new Animal();
        Animal myDog = new Dog();

        myAnimal.makeSound(); // Output: Animal makes a sound
        myDog.makeSound();    // Output: Dog barks
    }
}

				
			

Detailed Points

  1. Polymorphism: In the example above, ‘myDog‘ is of type ‘Animal‘ but points to an instance of ‘Dog‘. When ‘makeSound()‘ is called on ‘myDog‘, the overridden method in ‘Dog‘ is executed. This demonstrates polymorphism, where the call to’ makeSound()‘ resolves to the’ Dog‘s version of the method at runtime.

  2. Super Keyword: Sometimes, it is useful to call the superclass method within the overriding method. This can be done using the ‘super‘ keyword.

				
					class Dog extends Animal {
    @Override
    void makeSound() {
        super.makeSound(); // Calls the method from Animal class
        System.out.println("Dog barks");
    }
}

				
			

3. Final Methods: If a method is declared as ‘final‘ in the superclass, it cannot be overridden by subclasses.

				
					class Dog extends Animal {
    @Override
    void makeSound() {
        super.makeSound(); // Calls the method from Animal class
        System.out.println("Dog barks");
    }
}

				
			

3. Final Methods: If a method is declared as ‘final‘ in the superclass, it cannot be overridden by subclasses.

				
					class Animal {
    final void makeSound() {
        System.out.println("Animal makes a sound");
    }
}

class Dog extends Animal {
    // Compilation error: cannot override final method
    @Override
    void makeSound() {
        System.out.println("Dog barks");
    }
}

				
			

4. Abstract Methods: Abstract methods in an abstract class must be overridden in the subclass.

				
					abstract class Animal {
    abstract void makeSound();
}

class Dog extends Animal {
    @Override
    void makeSound() {
        System.out.println("Dog barks");
    }
}

				
			

5. Covariant Return Types: Since Java 5, it is allowed to override a method and change its return type to a subtype of the original return type.

				
					class Animal {
    Animal getInstance() {
        return new Animal();
    }
}

class Dog extends Animal {
    @Override
    Dog getInstance() {
        return new Dog();
    }
}

				
			

Best Practices

  • Always use the @Override annotation when overriding a method. It improves code readability and helps catch errors at compile time.
  • Ensure the overriding method provides additional functionality or modifies the behavior of the superclass method in a meaningful way.
  • Be cautious with the use of super to call superclass methods, as it can lead to tightly coupled code if not used judiciously.
  • Consider the use of final and abstract keywords to control which methods can and cannot be overridden in subclasses.

Method overriding is a powerful feature in Java that, when used correctly, allows for flexible and reusable code. It is a cornerstone of polymorphism, enabling objects to be treated as instances of their parent class while still behaving in ways specific to their actual subclass.

Scroll to Top