package com.codegym.task.task13.task1306;
/*
Buggy initializeIdAndName
*/
public class Solution {
public static void main(String[] args) throws Exception {
System.out.println(Matrix.NEO);
System.out.println(Matrix.TRINITY);
}
static class Matrix {
public static DBObject NEO = new User().initializeIdAndName(1, "Neo");
public static DBObject TRINITY = new User().initializeIdAndName(2, "Trinity");
}
interface DBObject {
DBObject initializeIdAndName(long id, String name);
}
static class User implements DBObject {
long id;
String name;
@Override
public User initializeIdAndName(long id, String name) {
this.id = id;
this.name = name;
System.out.println(this.getClass().getSimpleName());
return this;
}
@Override
public String toString() {
return String.format("The user's name is %s, id = %d", name, id);
}
}
}
Why do I need to change return type for User?
public DBObject initializeIdAndName(long id, String name) {
this.id = id;
this.name = name;
System.out.println(this.getClass().getSimpleName()); - PRINTS OUT User!!!
return this;
}
Why do I need to change return type?
Under discussion
Comments (8)
- Popular
- New
- Old
You must be signed in to leave a comment
Nouser
27 November 2020, 13:22
My guess is cause it can be done and you should see that it is possible?
Imagine some sort of storage that just can hold DBObjects. Now you stored users in it. They are still users and you can call all of the User types methods on that objects. Quite good, isn't it?
Now you can add eg. a Coder class. When you want to save objects of that type in your storage system, then just implement the DBObject interface and you're ready to go.
Of course you could let the implementation of initializeIdAndName return a DBObject. But then the only methods you can call on that objects are the one of the interface. Means you probably have to cast the object to the user/ coder class somewhen later.
0
Dmitri
28 November 2020, 10:48
Yes, I understand, but the validator told that initializeIdAndName must return User object, and it indeed returns - System.out.println(this.getClass().getSimpleName()); - PRINTS OUT User!!!
0
Misiu
28 November 2020, 11:11
DBObject initializeIdAndName() - returns User of type DBObject (partly User)
User initializeIdAndName() - returns User of type User (full User)
I think the condition says - return full User.
0
Dmitri
28 November 2020, 11:26
I actually tried return (User) this; and that did not work.
0
Misiu
28 November 2020, 12:05useful
"this" is User. Full User.
For: return (User) this; IntelliJ says: Casting 'this' to 'User' is redundant.
But DBObject initializeIdAndName() returns User of DBObject type (partly User).
You can cast NEO variable.
+1
Nouser
28 November 2020, 12:41useful
Don't confuse object and reference variables. If you are in non static method of a class (here User), then an object of this class has to exist. And that object has to be of type User and that object always will be of that type.
This object inside that class is referenced to as this. Means the reference this is of the same type as the object. Now you could change the reference variable eg. to Object like
Object o = this;
As said, that doesn't mean the User object is now of type Object. No, it's still User. But the reference variable is of type Object.
Here's code trying to expain that a little bit better. It won't run unless you fix the return type error.
+3
Nouser
28 November 2020, 12:41
And don't forget, the object itself is always a cat.
+2
Misiu
28 November 2020, 14:22
Great explanations Nouser.
Don't confuse object and reference variables.
Took some time I noticed that. I think most students have the problem.
+1