Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Java Override and Overload

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


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:

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.