ImportNew Note: This article is one of the Java interview series compiled by ImportNew. You can view all the Java interview series from here.
The common Interview Questions introduced in this article are about the overloading method and overriding method.
Q.What is the output result of the following code snippet?
View Code
1 public class MethodOverrideVsOverload { 2 3 public boolean equals( MethodOverrideVsOverload other ) { 4 System.out.println("MethodOverrideVsOverload equals method reached" ); 5 return true; 6 } 7 8 public static void main(String[] args) { 9 Object o1 = new MethodOverrideVsOverload();10 Object o2 = new MethodOverrideVsOverload();11 12 MethodOverrideVsOverload o3 = new MethodOverrideVsOverload();13 MethodOverrideVsOverload o4 = new MethodOverrideVsOverload();14 15 if(o1.equals(o2)){16 System.out.println("objects o1 and o2 are equal");17 }18 19 if(o3.equals(o4)){20 System.out.println("objects o3 and o4 are equal");21 }22 }23 }
A.The output result is:
MethodOverrideVsOverload equals method reached
Objects o3 and o4 are equal
What concepts have this question explored?
- In Java, a class can only inherit from one class (that is, the single inheritance structure). If the class inherited is not explicitly indicated, the class automatically inherits fromObjectObject.
- Most non-final object class methods will overwrite the quilt class (overridden ):
Public boolean equals (Object obj); // make note of this method
Public int hashCode ();
Public String toString ();
- The overload method isCompile timeFor example, static binding.RuntimeFunction (for example, dynamic binding ). Static binding means that the JVM decides the class or method to call during compilation. During dynamic binding, the JVM decides to call classes or methods at runtime. Dynamic Binding design is the basis of polymorphism. Learn more about compile time and runtime.
- The corresponding method for overwriting the parent class in the subclass must follow the following rules:
Parameters |
Immutable (Translator's note: includes parameter types and numbers ). |
Return type |
Immutable, except for the covariant return type or its subtype (covariant (subtype) returns ). |
Exception |
A subclass can throw fewer exceptions, but it cannot throw checked exceptions not defined in the parent class. |
Access permission |
It is looser than the corresponding method in the parent class. |
Call |
The specific method called is determined based on the object type during running (that is, dynamic binding. |
Now, let's look back at the above Code,MethodOverrideVsOverload"Equals (MethodOverrideVsOverload other )"The method does not overwrite"Public boolean equals (Object obj )"Method. This is because it violates the parameter rules, one of which isMethodOverrideVsOverloadType, while the other isObjectType. Therefore, the two methods are:Reload Link(Occurs during compilation) InsteadRewrite relationship.
Therefore, whenO1.equals (o2)In the object classPublic boolean equals (Object obj)Method. This is becauseCompile timeBoth o1 and o2 are of the Object type, while the equals (... ) The method is to compare the memory address (for example, Object @ 235f56 and Object @ 653af32), so false is returned.
WhenO3.equals (o4)Actually calledMethodOverrideVsOverloadClassEquals (MethodOverrideVsOverload other)Method. This is because both o3 and o4 areMethodOverrideVsOverload.
What can I do next?
Q. How can we solve the problem above?
A. Added annotations in Java 5, including useful compile-time annotations (Compile time annotations) @ Override to ensure that the parent class method is overwritten correctly. If an annotation is added to the Code above, the JVM will throw a compilation error.
Therefore, the solution isMethodOverrideVsOverloadAdd the @ override annotation to the boolean equals (MethodOverrideVsOverload other) method of the class. In this case, an error is thrown during compilation, prompting the developer that a method does not correctly rewrite the parent class method. Then, you also need to modify the parameters of the method from ObjectMethodOverrideVsOverload, As follows:
View Code
1 public class MethodOverrideVsOverload { 2 3 @Override 4 public boolean equals( Object other ) { 5 System.out.println("MethodOverrideVsOverload equals method reached" ); 6 return true; 7 } 8 9 public static void main(String[] args) {10 Object o1 = new MethodOverrideVsOverload(); //during compile time o1 is of type Object11 //during runtime o1 is of type MethodOverrideVsOverload12 Object o2 = new MethodOverrideVsOverload(); //during compile time o2 is of type Object13 //during runtime o2 is of type MethodOverrideVsOverload14 15 MethodOverrideVsOverload o3 = new MethodOverrideVsOverload(); //o3 is of type MethodOverrideVsOverload16 // during both compile time and runtime17 MethodOverrideVsOverload o4 = new MethodOverrideVsOverload(); //o4 is of type MethodOverrideVsOverload18 // during both compile time and runtime19 20 if(o1.equals(o2)){21 System.out.println("objects o1 and o2 are equal");22 }23 24 if(o3.equals(o4)){25 System.out.println("objects o3 and o4 are equal");26 }27 28 }29 30 }
Output:
MethodOverrideVsOverload equals method reached
Objects o1 and o2 are equal
MethodOverrideVsOverload equals method reached
Objects o3 and o4 are equal
In the code above, the equals method at run time correctly overwrites the corresponding method in the Object. This is a confusing question, and related concepts need to be explained in detail during the interview.
Java Success, Compilation: ImportNew-zheng Wen
Http://www.importnew.com/2228.html.