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

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
Username987654
Posts: 95
Joined: Sat Dec 26, 2015 6:37 pm
Contact:

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

Post 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?

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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!
If you like our products and services, please help us by posting your review here.

OCAJO1
Posts: 221
Joined: Mon Nov 26, 2018 2:43 pm
Contact:

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

Post 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

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

No to both. I haven't heard such a thing and don't believe it makes any sense.
If you like our products and services, please help us by posting your review here.

OCAJO1
Posts: 221
Joined: Mon Nov 26, 2018 2:43 pm
Contact:

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

Post by OCAJO1 »

One less thing to remember for the exam :|

OCAJO1
Posts: 221
Joined: Mon Nov 26, 2018 2:43 pm
Contact:

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

Post 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

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

Added to errata.
thank you for your feedback!
If you like our products and services, please help us by posting your review here.

flex567
Posts: 202
Joined: Mon Apr 02, 2018 8:40 am
Contact:

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

Post 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?
	}
}

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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:"
If you like our products and services, please help us by posting your review here.

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 32 guests