• Post Reply Bookmark Topic Watch Topic
  • New Topic

Method overriding and overloading  RSS feed

 
Sandy Deb
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I am new to Java and have come across a situation which confused me about method overriding and overloading. I am currently reading Head First Java and I will try to explain what I understand based on what I have read in the book. Code first

Code:


I saw this code and discussion in another forum but I hardly understood the explanation that were given.

From what I can see in the code, the output should be
0
1
2
Inside of Test.equals
3
Inside of Test.equals
4
Inside of Test.equals

Because t1 and t2 have been referenced as "Object" object, their "equals" method should correspond to the "equals" method in the "Object" class which does not print anything. So they are self explainable. But t3 has been referenced as "Test" object and so it should print as the "equals" method in "Test" class declares. However, there is one problem for which I do not have any answer - the "equals" method in "Test" class expects an object of "Test" class and both "t3.equals(o1)" and "t3.equals(t2)" does not satisfy that criteria

So the actual output is
0
1
2
3
Inside of Test.equals
4

Do "t3.equals(o1)" and "t3.equals(t2)" calls actually generate errors which are not fatal and so they are handled by the OS without throwing any exception or do they actually refer to the "equals" method of the "Object" class?

I am not sure about this. Please let me know what is happening here. Also, if some of my "self explainable" things are wrong because I misunderstood the whole concept, please let me know that also.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16058
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

o1 is an Object, so t3.equals(o1) will call the equals(Object) method on t3, which is the one in class Object, not the one in class Test.

Likewise, t2 is an Object, so t3.equals(t2) will call the equals(Object) method on t3, which is the one in class Object, not the one in class Test.

When determining which overloaded version of a method to call, Java looks at the static types of the argument variables, not at the dynamic types. So if you do Object t2 = new Test(), then it looks at the type Object (the type of the variable t2), not at the actual type of the object, which is Test.

You could use a cast to force it to call the equals(Test) method:


Note that a Test object (such as the object that t3 refers to) actually has two overloaded equals() methods: the one that's defined in class Test and that takes a Test object, and the one that's inherited from class Object that takes an Object as its argument.
 
Sandy Deb
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks - it is clear now and is in the same line of what has been explained in the book. I just forgot that when you overload a method in the sub class, the original method from the super class is actually present there to be used although from the perspective of code, it is invisible.
 
Campbell Ritchie
Marshal
Posts: 56530
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, the old method is still visible, even if you don't write it. It is the other way round, which can cause confusion.
It would only be private methods which are invisible elsewhere.
To demonstrate which methods are available in which class I have written a little class with Test (copied almost unchanged from your code) as an inner class. It lists the methods in Test and later on in Object.Run it and count the methods in each class; it should come to 12 and 11. No, it shows 10 and 9; the two methods with protected access are not shown.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!