Java Override and Overload
Override
Override means a subclass defines a method with the same name, parameter list, and return type as a method in its parent class, and the implementation of the subclass method overrides the implementation of the parent class method. That is, the shell stays the same, but the core is overridden!
The benefit of overriding is that subclasses can define behavior specific to their needs. That is, subclasses can implement the parent class methods as needed. Thus, when calling the method using a subclass object, the method in the subclass is executed rather than the one in the parent class.
An overriding method cannot throw new checked exceptions or exceptions broader than those declared by the overridden method. For example: a parent class method declares a checked exception IOException, but when overriding this method you cannot throw an Exception, because Exception is the parent class of IOException; you can throw IOException or a subclass of IOException.
In object-oriented principles, overriding means any existing method can be overridden. The example is as follows:
TestDog.java file code:
class
Animal
{
public
void
move
(
)
{
System
.
out
.
println
(
"
Animals can move
"
)
;
}
}
class
Dog
extends
Animal
{
public
void
move
(
)
{
System
.
out
.
println
(
"
Dogs can run and walk
"
)
;
}
}
public
class
TestDog
{
public
static
void
main
(
String
args
[
]
)
{
Animal
a
=
new
Animal
(
)
;
//
Animal object
Animal
b
=
new
Dog
(
)
;
//
Dog object
a
.
move
(
)
;
//
Execute the Animal class method
b
.
move
(
)
;
//
Execute the Dog class method
}
}
The compilation and execution results of the above example are as follows:
动物可以移动
狗可以跑和走
In the example above, we can see that although b is of type Animal, it runs the move method of the Dog class.
This is because at compile time, only the reference type of the parameter is checked.
However, at runtime, the Java Virtual Machine (JVM) determines the object's type and runs that object's method.
Therefore, in the example above, it compiles successfully because the Animal class has a move method, but at runtime, the specific object's method is run.
Consider the following example:
TestDog.java file code:
class
Animal
{
public
void
move
(
)
{
System
.
out
.
println
(
"
Animals can move
"
)
;
}
}
class
Dog
extends
Animal
{
public
void
move
(
)
{
System
.
out
.
println
(
"
Dogs can run and walk
"
)
;
}
public
void
bark
(
)
{
System
.
out
.
println
(
"
Dogs can bark
"
)
;
}
}
public
class
TestDog
{
public
static
void
main
(
String
args
[
]
)
{
Animal
a
=
new
Animal
(
)
;
//
Animal object
Animal
b
=
new
Dog
(
)
;
//
Dog object
a
.
move
(
)
;
//
Execute the Animal class method
b
.
move
(
)
;
//
Execute the Dog class method
b
.
bark
(
)
;
}
}
The compilation and execution results of the above example are as follows:
TestDog.java:30: cannot find symbol
symbol : method bark()
location: class Animal
b.bark();
^
The program will throw a compile error because the reference type Animal of b has no bark method.
Method overriding rules
-
The parameter list must be exactly the same as the parameter list of the overridden method.
-
The return type may be different from the return type of the overridden method, but must be a subclass of the parent class's return value (Java 5 and earlier require the same return type; Java 7 and later may differ).
-
The access level cannot be lower than the access level of the overridden method in the parent class. For example: if a method in the parent class is declared public, then overriding it in a subclass cannot be declared protected.
-
A member method of the parent class can only be overridden by its subclasses.
-
A method declared final cannot be overridden.
-
A method declared static cannot be overridden, but can be redeclared.
-
If the subclass and parent class are in the same package, then the subclass can override all methods of the parent class except those declared private and final.
-
If the subclass and parent class are not in the same package, then the subclass can only override the parent class's non-final methods declared public and protected.
-
An overriding method can throw any non-mandatory exception whether or not the overridden method throws exceptions. However, the overriding method cannot throw new mandatory exceptions, or broader mandatory exceptions than those declared by the overridden method; the reverse is allowed.
-
Constructors cannot be overridden.
-
If a class cannot be inherited, its methods cannot be overridden.
Using the super keyword
When you need to call a parent class's overridden method in a subclass, you use the super keyword.
TestDog.java file code:
class
Animal
{
public
void
move
(
)
{
System
.
out
.
println
(
"
Animals can move
"
)
;
}
}
class
Dog
extends
Animal
{
public
void
move
(
)
{
super
.
move
(
)
;
//
Apply the super class method
System
.
out
.
println
(
"
Dogs can run and walk
"
)
;
}
}
public
class
TestDog
{
public
static
void
main
(
String
args
[
]
)
{
Animal
b
=
new
Dog
(
)
;
//
Dog object
b
.
move
(
)
;
//
Execute the Dog class method
}
}
The compilation and execution results of the above example are as follows:
动物可以移动
狗可以跑和走
Overload
Overloading means within a single class, methods have the same name but different parameters. The return type can be the same or different.
Each overloaded method (or constructor) must have a unique parameter type list.
The most common place is constructor overloading.
Overloading rules:
- Overloaded methods must change the parameter list (different number or types of parameters);
- Overloaded methods can change the return type;
- Overloaded methods can change the access modifiers;
- Overloaded methods can declare new or broader checked exceptions;
- Methods can be overloaded in the same class or in a subclass.
- The return value type cannot be used as the criterion to distinguish overloaded functions.
Example
Overloading.java file code:
public
class
Overloading
{
public
int
test
(
)
{
System
.
out
.
println
(
"
test1
"
)
;
return
1
;
}
public
void
test
(
int
a
)
{
System
.
out
.
println
(
"
test2
"
)
;
}
//
The following two have different parameter type orders
public
String
test
(
int
a
,
String
s
)
{
System
.
out
.
println
(
"
test3
"
)
;
return
"
returntest3
"
;
}
public
String
test
(
String
s
,
int
a
)
{
System
.
out
.
println
(
"
test4
"
)
;
return
"
returntest4
"
;
}
public
static
void
main
(
String
[
]
args
)
{
Overloading
o
=
new
Overloading
(
)
;
System
.
out
.
println
(
o
.
test
(
)
)
;
o
.
test
(
1
)
;
System
.
out
.
println
(
o
.
test
(
1
,
"
test3
"
)
)
;
System
.
out
.
println
(
o
.
test
(
"
test4
"
,
1
)
)
;
}
}
Differences between override and overload
| Difference | Overloaded method | Overridden method |
|---|---|---|
| Parameter list | Must be modified | Must not be modified |
| Return Type | Can be modified | Must not be modified |
| exception | Can be modified | Can be reduced or deleted, but must not throw new or broader exceptions |
| Access the | Can be modified | Must not impose stricter restrictions (restrictions can be relaxed) |
Summary
Method overriding and overloading are different manifestations of Java polymorphism. Overriding is a manifestation of polymorphism between parent and child classes, while overloading can be understood as a concrete form of polymorphism.
- (1) Method overloading means multiple methods with the same name are defined in a class, but their number of parameters differs, or the number is the same but the types and order differ; this is called method overloading.
- (2) Method overriding is when a method in a subclass has the same name as a method in the parent class, with the same number and type of parameters and the same return value; this is called overriding.
- (3) Method overloading is a manifestation of polymorphism within a class, while method overriding is a manifestation of polymorphism between a subclass and its parent class.

