Java Tutorial

Operators

Flow Control

String

Number and Date

Built-in Classes

Array

Class and Object

Inheritance and Polymorphism

Exception Handling

Collections, Generics and Enumerations

Reflection

Input/Output Stream

Annotation

Java Method Overriding

Method overriding in Java is a feature that allows a subclass to provide a new implementation for a method that is already defined in its superclass. This allows the subclass to inherit the methods and fields of the superclass, while still being able to customize or extend its behavior. This tutorial will cover the basics of method overriding in Java, including some examples and best practices.

  • Basic method overriding:

To override a method in a subclass, define a method with the same name, return type, and parameter list as the method in the superclass.

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

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

In this example, the Dog class overrides the makeSound() method from the Animal class.

  • Example usage of method overriding:

When calling a method on an object, the Java runtime will automatically select the appropriate overridden method based on the object's runtime type.

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

        myAnimal.makeSound(); // Output: The animal makes a sound
        myDog.makeSound(); // Output: The dog barks
    }
}
  • Using the super keyword:

You can use the super keyword to call the superclass's implementation of a method within an overridden method.

class Dog extends Animal {
    @Override
    public void makeSound() {
        super.makeSound(); // Calls the superclass's makeSound() method
        System.out.println("The dog barks");
    }
}
  • Overriding rules and best practices:
  • The access level of the overriding method cannot be more restrictive than the access level of the overridden method. For example, if the superclass's method is public, the subclass's method must also be public.
  • The return type of the overriding method must be the same as, or a subtype of, the return type of the overridden method.
  • The overriding method cannot throw checked exceptions that are not thrown by the overridden method, but it can throw fewer or narrower exceptions.
  • Use the @Override annotation to indicate that a method is intended to override a method in the superclass. This helps catch errors at compile-time if the method signature does not match the superclass's method.
  • Constructors cannot be overridden, but they can be called from a subclass using the super keyword.
  • Static methods cannot be overridden. If a subclass has a static method with the same signature as a static method in the superclass, this is known as method hiding, not method overriding.

This tutorial introduced the basics of method overriding in Java, including creating overridden methods, using the super keyword, and some best practices to follow. Method overriding is a powerful feature in Java that allows you to create more flexible and expressive class hierarchies while keeping your code clean and organized.

  1. Java override annotation:

    • The @Override annotation is used to indicate that a method in a subclass is intended to override a method in its superclass. This annotation helps catch errors at compile-time.
    @Override
    public void display() {
        // overridden method implementation
    }
    
  2. Java method overriding with access modifiers:

    • The access modifier of an overriding method cannot be more restrictive than the overridden method. It can be the same or less restrictive.
    // In superclass
    protected void display() {...}
    
    // In subclass
    @Override
    public void display() {...} // Valid
    
    @Override
    private void display() {...} // Invalid
    
  3. Dynamic method dispatch in Java with method overriding:

    • Dynamic method dispatch allows the JVM to determine the appropriate method implementation at runtime based on the actual object type.
    SuperClass obj = new SubClass();
    obj.display(); // Calls the display() method in SubClass
    
  4. Java abstract class and method overriding:

    • Abstract methods in an abstract class must be overridden by concrete subclasses.
    abstract class AbstractClass {
        abstract void display();
    }
    
    class ConcreteClass extends AbstractClass {
        @Override
        void display() {
            // Implementation
        }
    }
    
  5. Java interface and method overriding:

    • Interfaces define abstract methods that must be implemented by classes implementing the interface.
    interface MyInterface {
        void display();
    }
    
    class MyClass implements MyInterface {
        @Override
        public void display() {
            // Implementation
        }
    }
    
  6. Covariant return types in Java method overriding:

    • Covariant return types allow a subclass to override a method with a return type that is a subclass of the return type in the superclass.
    class SuperClass {
        SuperClass get() {...}
    }
    
    class SubClass extends SuperClass {
        @Override
        SubClass get() {...}
    }