Win a copy of Spark in Action this week in the Open Source Projects forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Using Operators.

 
Ranch Hand
Posts: 130
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This question is from Enthuware Practice (IZ0 -803) Test 1 Question number 58

Given the following class definitions, the expression

correctly identifies whether the object referred to by obj was created by instantiating
class B rather than classes A, C and D?


Options.
A.True
B.False

I need help with this question, I do not understand the question and I don't know how to go about it.
 
Ranch Hand
Posts: 207
3
Oracle MySQL Database Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The question is asking from you that whether the object referred by obj was created by making object of class B and not of class A,C,D.
Now see->
as the question says that the object must be of class B so lets see->
1) (obj instanceof A)->
See here that if object is of class B then definately that is also the object of class A. Since B extends A. So the above statement that (obj instanceof A) dosen't gaurantee that obj is always an object of class B, it is also object of class A. So (obj instanceof A) will give you true always whether object is of class A or B. So it creates ambiguity that's why the answer to the problem is false.

2) Second thing that the statement (obj instanceof D)-> this statement is redundant since, if the object is not of class C then definately it is nit an object of class D.
 
Marshal
Posts: 69740
277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is a way to write an expression such that true means the object can only have been created from class B. I shall challenge you to find it.
 
O Shea
Ranch Hand
Posts: 207
3
Oracle MySQL Database Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ya definately and it is like-> if(obj instanceof B)&&!(obj instanceof C)-> Here clear that obj is an object of class B only and !(obj instanceof C) indicates that it is not of class C and hence we can remove the last statement which is causing redundancy i.e,->!(obj instanceof D)
 
Campbell Ritchie
Marshal
Posts: 69740
277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

O Shea wrote:. . . (obj instanceof B)&&!(obj instanceof C) . . .

Until you have class E extends B. . . .

Try again. There is something which will definitely tell you that the object was created from Class B and not anything else.
 
O Shea
Ranch Hand
Posts: 207
3
Oracle MySQL Database Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote: Until you have class E extends B. . . .

Can't understand what you are saying . In question there is no E class.

(obj instanceof B) will indicate that object is of class B. But since B extends A then A also have that object. I think making class A ad final will make the way. Since then we can't make its subclass and then we can have this statement (obj instanceof B) will directly point that object is of class B only .
 
Saloon Keeper
Posts: 12126
258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

O Shea wrote:In question there is no E class.


But the code can't guarantee that there never will be an E class. You must treat these kinds of problems as if somebody can add new definitions.

(obj instanceof B) will indicate that object is of class B. But since B extends A then A also have that object. I think making class A ad final will make the way.


But then the definition of class B would be invalid, because B must extend A.

Campbell is hinting at a method from the standard API that tells you the concrete type an object was instantiated from.
 
O Shea
Ranch Hand
Posts: 207
3
Oracle MySQL Database Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you talking about Java Reflection
 
O Shea
Ranch Hand
Posts: 207
3
Oracle MySQL Database Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No no sorry if is factory method pattern .
 
Greenhorn
Posts: 13
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What about something comparing the class names as a condition? Something like
 
Chris J Allen
Greenhorn
Posts: 13
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
or even
 
Campbell Ritchie
Marshal
Posts: 69740
277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Chris J Allen wrote:What about something comparing the class names as a condition? Something like

No. The == operator will not give the right answer there.

You are getting warmer, but you are also seriously over‑thinking the problem and seriously over‑complicating it.
 
Campbell Ritchie
Marshal
Posts: 69740
277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Chris J Allen wrote:or even

No. An E as I mentioned earlier is assignable to B, so that will give an incorrect result.
 
Campbell Ritchie
Marshal
Posts: 69740
277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

O Shea wrote:No no sorry if is factory method pattern .

No.

It is something I was taught about when I had only been programming Java® for about two months. It is much simpler than what you are suggesting. I shall give you a hint, before I let you out of your misery: look here and note where I said somebody was getting warmer. Now, that's two hints.
 
Bartender
Posts: 3993
156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nevertheless, O Shea gave the correct answer for the correct reason to OP's question.
 
Campbell Ritchie
Marshal
Posts: 69740
277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I added an additional task as a challenge, and I am surprised at not getting the correct answer, though Chris J Allen was thinking on the right lines.
One correct answer is
if (obj.getClass().equals(B.class)) ...
Since Class<T> doesn't override Object#equals(), it uses the idiom obj == this in equals, so the following is another version of the correct answer:-
if (obj.getClass() == B.class) ...
 
Rancher
Posts: 4603
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:No. The == operator will not give the right answer there.



Well, it will give the right answer, but that's a side effect of how the name is stored in a Class object, so both Strings will actually be the same object.
 
Campbell Ritchie
Marshal
Posts: 69740
277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dave Tolls wrote:. . . so both Strings will actually be the same object.

I didn't realise that; sorry that I was mistaken .

That is of course because there is only one Class<T> object per class, so both obj.getClass() and B.getClass() point to the same Class<B> object, so its name field will be the same object in both cases.
My apologies, Chris J Allen.
 
Ranch Hand
Posts: 86
18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Comparing the names won't give the correct answer as two classes with the same name may be loaded; a.getName() == b.getName() does not imply a == b.
 
Dave Tolls
Rancher
Posts: 4603
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tobias Bachert wrote:Comparing the names won't give the correct answer as two classes with the same name may be loaded; a.getName() == b.getName() does not imply a == b.



Yes it does.
It's a fully qualified name.

There's only one foo.bar.SomeClass (well, outside of multiple class loaders, but then a != b in that case anyway).
 
Campbell Ritchie
Marshal
Posts: 69740
277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is that getName or getSimpleName? Surely getName includes the package name?

. . .

No, you are correct; if there are two classes in different .jar files with the same fully qualified name, it is possible for one to be loaded in location X and the other to be loaded in  location Y.
So we are back to the two Class references pointing to the same object.
Can we always be sure the class was loaded by the same class loader? Is that last a silly question?
 
Tobias Bachert
Ranch Hand
Posts: 86
18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dave Tolls wrote:There's only one foo.bar.SomeClass (well, outside of multiple class loaders, but then a != b in that case anyway).


That was my point :) - as soon as you have two class loaders with a foo.bar.SomeClass, you have two different classes with the same name, thus checking the name is not sufficient.
 
Campbell Ritchie
Marshal
Posts: 69740
277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tobias Bachert wrote:. . .

Surely if you are returning true from the == operator it will be the same class? Surely if clazz1 != clazz2, then the Strings representing the name might be equal to the equals() method but not to ==?
 
Chris J Allen
Greenhorn
Posts: 13
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Chris J Allen wrote:What about something comparing the class names as a condition? Something like

No. The == operator will not give the right answer there.

You are getting warmer, but you are also seriously over‑thinking the problem and seriously over‑complicating it.



Putting the two hints together would yield something like this:


which provides this answer (note I called my class OnlyB):


Although I suspect I am still over-complicating this answer.
 
Stephan van Hulst
Saloon Keeper
Posts: 12126
258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's no need to use bounded wildcards. There's no need to construct a new instance of B just to get the class object that belongs to B. Otherwise, you're almost there.
 
Chris J Allen
Greenhorn
Posts: 13
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:There's no need to use bounded wildcards. There's no need to construct a new instance of B just to get the class object that belongs to B. Otherwise, you're almost there.




Alright, something a little simpler:


So adding the constraint back to the original post, the condition would be:
 
Stephan van Hulst
Saloon Keeper
Posts: 12126
258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where does obj come from? Where does b come from? obj.getClass() == b.getClass() is trivially true if obj == b. This doesn't actually give you any new information.

Google for "Class literals".
 
Campbell Ritchie
Marshal
Posts: 69740
277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Chris J Allen wrote:. . .. . .

Have a look at the old Sun style guide. What is wrong with writing this?. . . apart from missing the capital first letter. I would still prefer B.class, what is called a class literal.
 
First, you drop a couch from the plane, THEN you surf it. Here, take this tiny ad with you:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic