May 10, 2021 Java
Rewriting is the implementation of a child class to re-write a method that allows access to the parent class! N either the return value nor the parameter can be changed. That is, the shell does not change, the core rewrites!
The advantage of overrides is that sub-classes can define their own behavior as needed.
This means that the child class can implement the parent class's method as needed.
In object-oriented principles, overriding means that you can override any existing method. Here's an example:
class Animal{
public void move(){
System.out.println("动物可以移动");
}
}
class Dog extends Animal{
public void move(){
System.out.println("狗可以跑和走");
}
}
public class TestDog{
public static void main(String args[]){
Animal a = new Animal(); // Animal 对象
Animal b = new Dog(); // Dog 对象
a.move();// 执行 Animal 类的方法
b.move();//执行 Dog 类的方法
}
}
The above examples compile and run as follows:
动物可以移动
狗可以跑和走
As you can see in the example above, although b belongs to the Animal type, it runs the move method of the Dog class.
This is due to the fact that during the compilation phase, only the reference type of the parameter is checked.
At runtime, however, the Java virtual machine (JVM) specifies the type of object and the method by which it is run.
So in the example above, the compilation was successful because there is a move method in the Animal class, but at runtime, the method for a particular object is running.
Consider the following example:
class Animal{
public void move(){
System.out.println("动物可以移动");
}
}
class Dog extends Animal{
public void move(){
System.out.println("狗可以跑和走");
}
public void bark(){
System.out.println("狗可以吠叫");
}
}
public class TestDog{
public static void main(String args[]){
Animal a = new Animal(); // Animal 对象
Animal b = new Dog(); // Dog 对象
a.move();// 执行 Animal 类的方法
b.move();//执行 Dog 类的方法
a.bark();//执行 Animal 类的方法
}
}
The above examples compile and run as follows:
TestDog.java:30: cannot find symbol
symbol : method bark()
location: class Animal
a.bark();
^
The program throws a compilation error because the reference type Animal of a does not have a bark method.
Use the super keyword when you need to call the rewritten method of the parent class in a child class.
class Animal{
public void move(){
System.out.println("动物可以移动");
}
}
class Dog extends Animal{
public void move(){
super.move(); // 应用super类的方法
System.out.println("狗可以跑和走");
}
}
public class TestDog{
public static void main(String args[]){
Animal b = new Dog(); //
b.move(); //执行 Dog类的方法
}
}
The above examples compile and run as follows:
动物可以移动
狗可以跑和走
Overloading is in a class with the same method name and different parameters. W hat about the return type? Can be the same or different.
Each overloaded method (or constructor) must have a unique list of parameter types.
The most common place is the overload of the constructor.
Overload the rule
public class Overloading {
public int test(){
System.out.println("test1");
return 1;
}
public void test(int a){
System.out.println("test2");
}
//以下两个参数类型顺序不同
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));
}
}
The above examples compile and run as follows:
test1
1
test2
test3
returntest3
test4
returntest4
The difference | Overload method | The override method |
---|---|---|
The list of parameters | Must be modified | It must not be modified |
Returns the type | Can be modified | It must not be modified |
Abnormal | Can be modified | Can be reduced or deleted, must not throw new or broader exceptions |
Access | Can be modified | There must be no stricter restrictions (limits can be lowered) |
Overriding and overloading of methods are different manifestations of java polymorphism, and overrides are manifestations of polymorphism between parent and child classes, and overloading can be understood as a concrete manifestation of polymorphism.