Page 1 of 1

[HD Pg 415, Sec. 11.5.2 - impact-of-polymorphism-on-and-equals-method]

Posted: Sun Jan 20, 2019 8:52 pm
by Username987654
Since the declared type of the variable x1 is Object
should be
Since the declared type of the variable x1 is X
since
X x1 = new X();
?

If so, does the provided explanation (still) stand?

Re: [HD Pg 415, Sec. 11.5.2 - impact-of-polymorphism-on-and-equals-method]

Posted: Mon Jan 21, 2019 12:53 am
by admin
The explanation and logic is correct. The mistake is that it should say x2 instead of x1, "Since the declared type of the variable x2 is Object,...".
The variable being passed as an argument is x2 and its declared type is Object. The declared type of the variable passed as an argument determines which one of the two overloaded methods will be invoked.
Added to errata.
thank you for your feedback!

Re: [HD Pg 415, Sec. 11.5.2 - impact-of-polymorphism-on-and-equals-method]

Posted: Thu Mar 07, 2019 7:26 pm
by OCAJO1
I read somewhere that overloading or hiding a static method is compile time polymorphism, while overriding a method or overloading an instance method is run time polymorphism.

Question, 1. Is this accurate? 2. When method variables hide member fields, is that compile time polymorphism too?

Thanks

Re: [HD Pg 415, Sec. 11.5.2 - impact-of-polymorphism-on-and-equals-method]

Posted: Thu Mar 07, 2019 9:41 pm
by admin
No to both. I haven't heard such a thing and don't believe it makes any sense.

Re: [HD Pg 415, Sec. 11.5.2 - impact-of-polymorphism-on-and-equals-method]

Posted: Fri Mar 08, 2019 1:14 pm
by OCAJO1
One less thing to remember for the exam :|

Re: [HD Pg 415, Sec. 11.5.2 - impact-of-polymorphism-on-and-equals-method]

Posted: Tue Mar 26, 2019 6:21 pm
by OCAJO1
if( ! x instanceof X ) return false;

Should be

if( ! (x instanceof X) ) return false;

Also in the last paragraph of the section,

Since the declared type of the variable x1 is Object

should be

Since the declared type of the variable x2 is Object

Re: [HD Pg 415, Sec. 11.5.2 - impact-of-polymorphism-on-and-equals-method]

Posted: Tue Mar 26, 2019 9:36 pm
by admin
Added to errata.
thank you for your feedback!

Re: [HD Pg 415, Sec. 11.5.2 - impact-of-polymorphism-on-and-equals-method]

Posted: Sun Jun 09, 2019 7:00 am
by flex567
The Object version returns false because x1 and
x2 are pointing to two different objects.
This program returns true not false as stated in the book.

Code: Select all

class X{
	int val;
	public boolean equals(Object x){
		if( ! (x instanceof X) ) return false;
		//now compare the values of instance fields of this and x and return true/falseaccordingly
		return this.val == ((X) x).val;
	}
}

public class TestClass extends Test{
	
	
	public static void main(String[] args){

		X x1 = new X(); x1.val = 1;
		Object x2 = new X(); ((X) x2).val = 1;
		System.out.println(x1.equals(x2)); //what will it print?
	}
}

Re: [HD Pg 415, Sec. 11.5.2 - impact-of-polymorphism-on-and-equals-method]

Posted: Sun Jun 09, 2019 7:15 am
by admin
You are using wrong code. Use the code given in Red color, just above where this is written, "On the face of it, the equals method written above doesn’t seem to make much of a difference when you try to compare two X objects. But let’s change the code inside the main method as follows and
see what happens:"

Re: [HD Pg 415, Sec. 11.5.2 - impact-of-polymorphism-on-and-equals-method]

Posted: Sun Jun 09, 2019 7:20 am
by admin
Then read the explanation carefully that is given below the code that you are trying:
If you have understood the rules of method selection that we discussed in the “Working with
Methods and Encapsulation” chapter, you should be able to figure out that the above code will print
false. The compiler sees two versions of the equals method in class X to choose from when it tries
to bind the x1.equals(x2) method call - the version that class X inherits from java.lang.Object,
which takes Object as an argument and the version that class X implements itself, which takes X
as an argument. Since the declared type of the variable x2 is Object, the compiler binds the call
to the Object version instead of the X version. The Object version returns false because x1 and
x2 are pointing to two different objects.