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
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.
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.
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 } }
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"); } }
public
, the subclass's method must also be public
.@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.super
keyword.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.
Java override annotation:
@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 }
Java method overriding with access modifiers:
// In superclass protected void display() {...} // In subclass @Override public void display() {...} // Valid @Override private void display() {...} // Invalid
Dynamic method dispatch in Java with method overriding:
SuperClass obj = new SubClass(); obj.display(); // Calls the display() method in SubClass
Java abstract class and method overriding:
abstract class AbstractClass { abstract void display(); } class ConcreteClass extends AbstractClass { @Override void display() { // Implementation } }
Java interface and method overriding:
interface MyInterface { void display(); } class MyClass implements MyInterface { @Override public void display() { // Implementation } }
Covariant return types in Java method overriding:
class SuperClass { SuperClass get() {...} } class SubClass extends SuperClass { @Override SubClass get() {...} }