Java Method Overriding: Customize Inherited Methods for Subclass Behavior
Learn about method overriding in Java, where a subclass redefines a method inherited from its superclass. Discover how overriding allows subclasses to provide specific implementations for methods, enabling behavior customization while maintaining the same method signature.
Java - Overriding
In the previous chapter, we discussed superclasses and subclasses. When a class inherits a method from its superclass, it can override that method, provided it is not marked as final
.
Benefits of Overriding in Java
Overriding allows a subclass to define behavior specific to its type. This means that a subclass can implement a method inherited from its parent class based on its specific requirements.
In object-oriented programming, overriding means altering the functionality of an existing method.
Java Method Overriding
Method overriding enables us to achieve runtime polymorphism and is used to write specific definitions of a subclass method that is already defined in the superclass. The superclass method and the overridden method in the subclass should have the same declaration signature, including the parameter list, type, and return type.
Enhance your Java skills with real-world projects through our Java certification course. Enroll now to become a certified expert and advance your career!
Usage of Java Method Overriding
Method overriding serves two primary purposes in Java:
- Achieving runtime polymorphism.
- Providing a specific definition for a subclass method (known as the overridden method).
Example of Method Overriding in Java
Let’s look at a practical example:
Syntax
class Animal {
public void move() {
System.out.println("Animals can move");
}
}
class Dog extends Animal {
public void move() {
System.out.println("Dogs can walk and run");
}
}
public class TestDog {
public static void main(String args[]) {
Animal a = new Animal(); // Animal reference and object
Animal b = new Dog(); // Animal reference but Dog object
a.move(); // runs the method in Animal class
b.move(); // runs the method in Dog class
}
}
Output
Animals can move
Dogs can walk and run
In the example above, even though b
is of type Animal
, it executes the move
method from the Dog
class. This happens because, at compile time, the type of reference is checked, but at runtime, the JVM determines the object type and executes the method belonging to that object.
Another Example
Consider the following example:
Syntax
class Animal {
public void move() {
System.out.println("Animals can move");
}
}
class Dog extends Animal {
public void move() {
System.out.println("Dogs can walk and run");
}
public void bark() {
System.out.println("Dogs can bark");
}
}
public class TestDog {
public static void main(String args[]) {
Animal a = new Animal(); // Animal reference and object
Animal b = new Dog(); // Animal reference but Dog object
a.move(); // runs the method in Animal class
b.move(); // runs the method in Dog class
b.bark(); // this line will cause an error
}
}
Output
TestDog.java:26: error: cannot find symbol
b.bark();
^
symbol: method bark()
location: variable b of type Animal
1 error
This program will throw a compile-time error because the reference type of b
, which is Animal
, does not have a method named bark
.
Rules for Method Overriding
- The argument list must match exactly with that of the overridden method.
- The return type should be the same or a subtype of the return type declared in the original overridden method in the superclass.
- The access level cannot be more restrictive than the overridden method's access level. For example, if the superclass method is
public
, the overriding method in the subclass cannot beprivate
orprotected
. - Instance methods can only be overridden if they are inherited by the subclass.
- A method declared as
final
cannot be overridden. - A method declared as
static
cannot be overridden but can be re-declared. - If a method cannot be inherited, it cannot be overridden.
- A subclass in the same package as the superclass can override any superclass method that is not declared as
private
orfinal
. - A subclass in a different package can only override non-final methods declared as
public
orprotected
. - An overriding method can throw any unchecked exceptions, regardless of whether the overridden method throws exceptions. However, it should not throw checked exceptions that are new or broader than those declared by the overridden method. It can throw narrower or fewer exceptions.
- Constructors cannot be overridden.
Java Method and Constructor Overriding
In Java, each class has a unique name, and a constructor's name is the same as the class name. Therefore, we cannot override a constructor as they cannot have the same name.
Java Method Overriding: Using the super
Keyword
To invoke a superclass version of an overridden method, the super
keyword is used.
Example: Using the super
Keyword
Consider the following example:
Syntax
class Animal {
public void move() {
System.out.println("Animals can move");
}
}
class Dog extends Animal {
public void move() {
super.move(); // invokes the super class method
System.out.println("Dogs can walk and run");
}
}
public class TestDog {
public static void main(String args[]) {
Animal b = new Dog(); // Animal reference but Dog object
b.move(); // runs the method in Dog class
}
}
Output
Animals can move
Dogs can walk and run