1. rewrite (Override)
rewrite , Write it again . That is to rewrite the method of the parent class in the subclass . A subclass inherits The original non private method of the parent class , But I don’t want to completely follow the function of the parent method . So in Method name 、 parameter list 、 Return types are the same Under the circumstances , Modify or override a parent method , That’s what we’re talking about rewrite (Override). But pay attention to the rewriting rules :
-
It happens between the class and the parent class
-
Private methods in a parent class cannot be overridden
-
When overriding a superclass method , Access rights cannot be lower than the parent class , Throw an exception cannot be broader than the parent class’s exception or throw a new exception
-
When the parent class has static methods , Subclasses must be overridden by static methods ( It’s not rewriting strictly )
summary : When subclasses override methods , It’s better to be consistent with the statement .
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
Dog dog = new Dog();
dog.eat();
}
}
class Animal {
public void eat() {
System.out.println(" Animals are eating ");
}
}
class Dog extends Animal {
@Override
public void eat() {
System.out.println("Dog Eating ");
}
}
2. heavy load (Overload)
stay In the same class , Methods with the same name have different parameter lists ( Different parameter types , The number of parameters is different , Even the order of the parameters is different ) Heavy load , Overloading of There is no requirement for return type , Can be the same , It can be different , But not Overload is determined by whether the return type is the same .
summary :
- heavy load (
Overload
) Is a manifestation of polymorphism in a class - Overloading requires different parameter lists for methods with the same name ( Parameter type , Number, even the order of parameters )
- Heavy load , Return value types can be different , It can be the same . The return value type cannot be used as a criterion for overloading
public class Test01 {
public static void main(String[] args) {
Test01 test = new Test01();
test.eat();
test.eat("FEEL");
}
public void eat() {
System.out.println(" Who's eating ");
}
public void eat(String name) {
System.out.println(name + " Eating ");
}
}
3. The difference between rewriting and overloading
Override
It’s rewriting ,Overload
It’s heavy load. , Both are ways to achieve polymorphism , The difference is that overloading is Compile time polymorphism , And rewriting is Run time polymorphism .Overload
The premise is that in the same class , As long as the function name is the same , Different parameter list , That is, heavy load , Independent of the return valueOverride
In subclasses and superclasses , The subclass must declare the same method and return value type as the parent class . And it can’t declare more exceptions and have more access rights than the parent class is overridden .- Overloading does not require a return type , Overload identification cannot be based on the return type .