java method rewrite and super keyword example detail
- 2020-06-15 08:54:57
- OfStack
java methods override and super keywords
In inheritance, it's the child class that defines the method with the same name as the parent class
Methods, properties are the same
Rewrite restrictions:
Methods overridden by subclasses cannot have more stringent permissions than superclass methods
super: Forces the execution of a superclass method to be invoked
What's the difference between overloading and overwriting?
Reloading occurs in a class where permissions are not required and the overloaded method parameters can be different
Overrides occur when methods overridden by subclasses cannot have more stringent permissions than methods overridden by superclasses, whose parameter names are identical
Example code:
class A{
public void tell(){
System.out.println(" I am a tell methods ");
}
//private( with 1 A class of ) < default( In the same 1 The package can be accessed below ) < public( The whole project is accessible )
// The default default
void say(){
}
}
class B extends A{
// This approach is called method rewriting
public void tell(){
//super Don't 1 Used in overrides, it can also mean that those methods are inherited from the parent class.
super.tell(); // through super Keywords can be invoked in the parent class tell methods
System.out.println(" I rewrote the tell methods ");
}
}
class HelloWorld{
public static void main(String[] args){
B b = new B();
b.tell();
}
}
Thank you for reading, I hope to help you, thank you for your support to this site!