Google

Jun 27, 2012

Core Java coding questions frequently asked in written tests and interviews - part 3: method overloading Vs overriding

Core Java Coding Questions and Answers for beginner to intermediate level

Q1 Q2 Q3 Q4 Q5 - Q8 Q9 Q10 Q11 Q12 - Q14 Q15



This part of the Java coding interview question is on the popular method overloading versus overriding concept.

Q. What will be the output of the following code snippet?

public class MethodOverrideVsOverload {
  
 public boolean equals( MethodOverrideVsOverload other ) {
     System.out.println("MethodOverrideVsOverload equals method reached" );
     return true;
   }
 
 public static void main(String[] args) {
  Object o1 = new MethodOverrideVsOverload();
  Object o2 = new MethodOverrideVsOverload();
  
  MethodOverrideVsOverload o3 = new MethodOverrideVsOverload();
  MethodOverrideVsOverload o4 = new MethodOverrideVsOverload();
  
  if(o1.equals(o2)){
   System.out.println("objects o1 and o2 are equal");
  }
  
  if(o3.equals(o4)){
   System.out.println("objects o3 and o4 are equal");
  }
 }
}

A. The output will be

MethodOverrideVsOverload equals method reached
objects o3 and o4 are equal

What concepts does this question try to test?
  • In Java, a class can only extend a single class (i.e. single inheritance), and when it does not explicitly extend a class, it implicitly extends the class Object. So, MethodOverrideVsOverload implicitly extends the class Object. 
  • The majority of the non final Object class methods are meant to be overridden by the sub classes.

    public boolean equals(Object obj); // make note of this method
    public int hashCode();
    public String toString();
    
  • The method overloading takes place at compile time (i.e. static binding) and method overriding takes place at runtime (i.e. dynamic binding). Static binding means the JVM decides, which class or method to call during compile time. Dynamic binding means, the JVM decides, which class or method to call during runtime. The polymorphism is possible because of dynamic binding. Learn more about compile-time vs runtime
  • The method overriding must adhere to the following rules 

    Arguments Must not change.
    Return type Can't change except for covariant (subtype) returns.
    Exceptions The extending class can eliminate or call fewer exceptions than its parent, but must not throw new or broader checked exceptions.
    Access Must not be more restrictive than the class it extends. Can be less restrictive.
    Invocation Which method to call is based on object type, at runtime time (i.e. dynamic binding).

Now, if you look at the above code

The "equals(MethodOverrideVsOverload other)" method in class MethodOverrideVsOverload does not actually override the Object class's "public boolean equals(Object obj)" method. This is because it fails to adhere to the "Arguments" rule as both methods have different arguments as one is of  type "MethodOverrideVsOverload " and the other of type "Object" . So, the two methods are overloaded (happens at compile time) and not overridden.

So, when o1.equals(o2) is invoked, the public boolean equals(Object obj) method of the object class is invoked because during compile time, the o1 and o2 are of type Object.The Object class's equals( ... ) method returns false as it compares the memory address (e.g. Object@235f56 and Object@653af32) of both objects.

When o3.equals(o4) is invoked, the "equals( MethodOverrideVsOverload other )" of the class MethodOverrideVsOverload is invoked as during compile time o3 and o4 are of type MethodOverrideVsOverload, hence you get the above output.


What follow on questions can you expect?

Q. How will you fix the above issue?
A. In Java 5, annotations were introduced and one of the handy compile time annotations is the @override, which will ensure that the methods are overridden correctly. If you had this annotation, when you override it incorrectly as in the above example, a compile time error will be thrown. 

So, to fix it, add the @override annotation to the "boolean equals( MethodOverrideVsOverload other )" of the MethodOverrideVsOverload  class. This will give you a compile time error indicating that the method is not properly overridden. Now, fix the method signature by changing the  argument type in the method signature from "MethodOverrideVsOverload" to "Object" as shown below.


public class MethodOverrideVsOverload {
 
 @Override
 public boolean equals( Object other ) {
     System.out.println("MethodOverrideVsOverload equals method reached" );
     return true;
 }
 
 
 public static void main(String[] args) {
  Object o1 = new MethodOverrideVsOverload(); //during compile time o1 is of type Object
                                              //during runtime o1 is of type MethodOverrideVsOverload
  Object o2 = new MethodOverrideVsOverload(); //during compile time o2 is of type Object
                                              //during runtime o2 is of type MethodOverrideVsOverload
  
  MethodOverrideVsOverload o3 = new MethodOverrideVsOverload(); //o3 is of type MethodOverrideVsOverload
                                                                // during both compile time and runtime
  MethodOverrideVsOverload o4 = new MethodOverrideVsOverload(); //o4 is of type MethodOverrideVsOverload
                                                                // during both compile time and runtime
  
  if(o1.equals(o2)){
   System.out.println("objects o1 and o2 are equal");
  }
  
  if(o3.equals(o4)){
   System.out.println("objects o3 and o4 are equal");
  }
 
 }

}

The output will be

MethodOverrideVsOverload equals method reached
objects o1 and o2 are equal
MethodOverrideVsOverload equals method reached
objects o3 and o4 are equal

This is because now the methods are overridden and this happens at runtime. This is a bit tricky question, and think out loud at the interview to show that you understand the fundamentals.

Labels:

34 Comments:

Anonymous Anonymous said...

Excellent explanation!

6:17 AM, August 16, 2012  
Anonymous Anonymous said...

Funda Clear

11:23 PM, August 21, 2012  
Anonymous Anonymous said...

So nicely exlplained,damn good

10:35 PM, August 24, 2012  
Blogger Ashish Renapurkar said...

Very nice example !!!

5:31 AM, September 03, 2012  
Anonymous Anonymous said...

OMG that's awesome :)

3:30 PM, September 04, 2012  
Anonymous Anonymous said...

Very Well Explained, gr8 :)

6:15 PM, September 05, 2012  
Anonymous Anonymous said...

great example

12:41 AM, September 16, 2012  
Blogger SRIRAMVAMSI said...

very good

7:51 PM, September 16, 2012  
Blogger SRIRAMVAMSI said...

The compile time(overloading) and run time(Runtime) binding was clear!

7:53 PM, September 16, 2012  
Anonymous Anonymous said...

Thanq so much for posting such useful explanations

11:13 AM, September 18, 2012  
Blogger Divya sharma said...

Excellent answer.Thanks

2:42 AM, September 20, 2012  
Blogger chinna said...

Thank you so much.
Awesome...
I understood very well..

5:20 PM, September 22, 2012  
Anonymous Anonymous said...

What a clear explanation!

4:32 PM, October 12, 2012  
Blogger sarabjeet said...

i really like that you are giving information on core and advance java concepts. Being enrolled at http://www.wiziq.com/course/1779-core-and-advance-java-concepts i found your information very helpful indeed.thanks for it.

9:39 PM, October 31, 2012  
Blogger Naveen said...

Excellent question, and excellent explanation...
Really awesome..

Got more confidence on funda

4:24 PM, November 01, 2012  
Blogger sparkling stars said...

awesome explanation

2:08 AM, November 10, 2012  
Anonymous Anonymous said...

You r simply superb.. :)

2:19 AM, November 16, 2012  
Blogger vishal pundir said...

very well thought of............splendid work!!!

7:20 PM, November 18, 2012  
Anonymous Anonymous said...

Thats really very good explanation. Would like some more of this sort. Thanks a lot.

5:32 AM, December 04, 2012  
Blogger Xin Fan said...

Love it.

12:07 PM, December 06, 2012  
Anonymous Anonymous said...

good explanation

1:42 AM, January 11, 2013  
Blogger Vijendra Singh said...

really very good example.............

6:09 PM, June 15, 2013  
Anonymous Anonymous said...

Nicely explained....!

5:42 PM, July 19, 2013  
Blogger Yunus said...

Nice one

5:12 PM, July 23, 2013  
Anonymous Anonymous said...

found the grail here
very good explanation

11:04 AM, July 30, 2013  
Blogger Jeet Holmes said...

In the revised example when for object 03 and 04 equals method is called.Their object type is of methodoverloadvsmethodoverride then how this call didnt give error..Is it not wrong parameter?

5:53 PM, August 02, 2013  
Blogger santosh reddy said...

very good

10:23 PM, October 09, 2013  
Anonymous Anonymous said...

Man you are a Gem....Excellent work love u. from sashikanta mallick(bangalore)

2:58 AM, October 30, 2013  
Blogger venkat said...

Very clear explanation.

1:30 AM, November 15, 2013  
Blogger Amit Arora said...

No its not wrong parameter, as methodoverloadvsmethodoverride passes IS-A relationship. means methodoverloadvsmethodoverride IS-A Object (Parent-Child behavior).

11:17 PM, December 30, 2013  
Anonymous Anonymous said...

superb

3:55 PM, February 06, 2014  
Anonymous Anonymous said...

v. nice explaination

12:56 AM, February 24, 2014  
Anonymous Anonymous said...

Superb Explaination!

2:18 AM, April 30, 2014  
Anonymous Anonymous said...

nice

11:14 PM, May 23, 2014  

Post a Comment

Subscribe to Post Comments [Atom]

Links to this post:

Create a Link

<< Home