In Java, it is impossible to override static methods. But why the following code works:
Public Class Runner {
Public Static Void Main (String [] Args) {
B.fun ();
}
}
Class A {
Public Static Void Fun () {
System.out.printLN ("A");
}
}
Class B EXTENDS A {
Public Static Void Fun () {
System.out.printLN ("B");
}
}
Print is displayed “B”. Everything looks like this override. Because when overloading, I would have to change something in the method signature. But no, I did not change anything.
Annotation “Override” can not put on the method.
Answer 1, Authority 100%
Let’s start in order
Override method:
Override the method overriding ) is the ability of the language that allows the subclass or a child element to provide a specific implementation of the method that has already been implemented in one of the super-classes or parent class. Override looks like this:
Public Class App {
Public Static Void Main (String [] Args) {
Dog Dog = New Dog ();
Cat Cat = New Cat ();
Dog.Voice (); // Conclusion: Gav
cat.voice (); // Conclusion: Meow
}
}
Class Animal {// Parent Class
Public Void Voice () {
System.Out.print (voice of the animal);
}
}
Class Dog Extends Animal {// Class-Heir
@OVerride // Override the VOICE () method inherited from the Animal class in the heir class DOG
Public Void Voice () {
System.Out.print ("GAV");
}
}
CLASS CAT EXTENDS ANIMAL {// Heir class
@Override // Override the VOICE () method inherited from the class Animal in the heir class Cat
Public Void Voice () {
System.Out.print ("Meow");
}
}
The redefined method must have the same access modifier that its parent, take arguments as his parent, and have a return type is the same as his parent
i.
Public Class App {
Public Static Void Main (String [] Args) {
Dog Dog = New Dog ();
Cat Cat = New Cat ();
Dog.Voice ("Ball", 10); // Conclusion: Gav
cat.voice ("not a ball", 7); // Conclusion: Meow
}
}
Class Animal {
// Parental method has a returned String type. Takes two parameters, like string and int
Public String Voice (String Animal_Name, Int Animal_age) {
System.Out.print ("GAV");
Return Animal_Name;
}
}
Class Dog Extends Animal {
// The redefined method should be like its parent, have a returned String type, and accept the parameters as its parent
@Override
Public String Voice (String Dog_Name, Int Dog_age) {
System.Out.print ("GAV");
Return Dog_Name;
}
}
Class Cat Extends Animal {
@Override
Public String Voice (String Cat_Name, Int Cat_age) {
System.Out.print ("Meow");
RETURN CAT_NAME;
}
}
Hiding the method:
Hiding methods, roughly speaking, represents the “overlap” by the method of the current class, the method of the parent class. It looks like this:
Public Class App {
Public Static Void Main (String [] Args) {
Dog.Voice (); // Conclusion: Gav
}
}
Class Animal {
Public Static Void Voice () {
System.Out.print ("sound");
}
}
Class Dog Extends Animal {
// Method defined in the heir class identical on the signature with the parent class method
Public Static Void Voice () {
System.Out.print ("GAV");
}
}
signatures of the methods of these two classes are identical, i.e. In the classroom of the parent Animal
there is Public Static Void Voice ()
and in the heir class Dog
There are Public Static Void Voice ()
. Therefore, the call Dog.Voice ()
will call the method defined in the class Dog
It is important to note that the overlap requires the same rules as the redefinition of the method.
The redefined method must have the same access modifier that his parent, take arguments as his parent, and have a returned type the same as his parent
i.e.
Public Class App {
Public Static Void Main (String [] Args) {
Dog.Voice (); // Conclusion: Gav
Dog.Voice ("Ball"); // Conclusion: the ball says Gav
}
}
Class Animal {
Public Static Void Voice () {
System.Out.print ("sound");
}
}
Class Dog Extends Animal {
Public Static Void Voice () {// Method overlapping inherited method
System.Out.print ("GAV");
}
Public Static Void Voice (String Name) {// Separate method of class DOG
System.out.Print (name + "says Gav");
}
}
Consider the use of such methods in its classes
- Using without a similar signature method
Public Class App {
Public Static Void Main (String [] Args) {
Dog.Voice (); // Conclusion: Sound
}
}
Class Animal {
Public Static Void Voice () {
System.Out.print ("sound");
}
}
Class Dog Extends Animal {
Public Static Void Dog_Voice () {
VOICE ();
}
}
Since in the heir class there is no implemented as a signature with the parent class of the method, the method is used inherited from the parent class
- Using a similar method of
signature
Public Class App {
Public Static Void Main (String [] Args) {
Dog.dog_Voice (); // Conclusion: Gav
}
}
Class Animal {
Public Static Void Voice () {
System.Out.print ("sound");
}
}
Class Dog Extends Animal {
Public Static Void Voice () {
System.Out.print ("GAV");
}
Public Static Void Dog_Voice () {
VOICE ();
}
}
Since the Dog
class appears “your method” Voice ()
It overlaps the method inherited from Animal
- Using the method inherited from the parent when there is a method overlapping it
Public Class App {
Public Static Void Main (String [] Args) {
Dog.dog_Voice (); // Conclusion: Gavzvuk
}
}
Class Animal {
Public Static Void Voice () {
System.Out.print ("sound");
}
}
Class Dog Extends Animal {
Public Static Void Voice () {// Method overlapping inherited method
System.Out.print ("GAV");
}
Public Static Void Dog_Voice () {
VOICE (); // Calling the VOICE method defined in the DOG class;
Animal.Voice (); // Calling the Voice method defined in Animal class
}
}
I want to supplement that the overlap works not only with class methods, but also with its fields. Small example:
Public Class App {
Public Static Void Main (String [] Args) {
Dog.Animal (); // Conclusion: the name of this animal dog
}
}
Class Animal {
Public Static String Animal_Name = "No name, just an animal";
Public Static Void Animal () {
System.Out.print ("The name of this animal" + animal_name);
}
}
Class Dog Extends Animal {
public static string animal_name = "dog"; // overlaps the field of the parent class Animal
Public Static Void Animal () {
System.Out.print ("The name of this animal" + animal_name);
}
}
Answer 2, Authority 25%
There are cases when we override the method, and there are cases when we exercise the method. In your case, there is a “concealment”, and not “redefinition” of the method.
Static methods are not “redefined” the methods of superclass. Static methods are able to exercise “hiding” the method of superclass. At first glance, it may seem that “redefinition” and “concealment” is the same. here written in what the difference between them.