java desing issue
posted 16 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Hello,
I have something like this:
So you see, I in my class I have redundancy: Abstract and you see a objects, object and a findObject (X's, X and findX).
Now, I want to improve that via abstraction:
Now, I want to instatiate a MyClass and assign a defined Object (Dog, Cat, Mouse) in it:
But now, I want to access the properties of my Dog-Class and this does not work as I cannot add a cast to my getter/setter.
Maybe there is a far better way to implement that.
How can I solve that and what is the best solution?
I have something like this:
So you see, I in my class I have redundancy: Abstract and you see a objects, object and a findObject (X's, X and findX).
Now, I want to improve that via abstraction:
Now, I want to instatiate a MyClass and assign a defined Object (Dog, Cat, Mouse) in it:
But now, I want to access the properties of my Dog-Class and this does not work as I cannot add a cast to my getter/setter.
Maybe there is a far better way to implement that.
How can I solve that and what is the best solution?
posted 16 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
What does MyClass represent? What's its function?
nimo frey
Ranch Hand
Posts: 580
posted 16 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
MyClass is a type of an intermediate class.
Look at that:
I have the same name of properties in each Space:
the tasks (findObject, objects, object) represents type of objects depending on my space:
in catSpace the findObject, objects and object should be of type Cat.
in dogSpace the findObject, objects and object should be of type Dog.
How can I implement such a scenario?
Look at that:
I have the same name of properties in each Space:
the tasks (findObject, objects, object) represents type of objects depending on my space:
in catSpace the findObject, objects and object should be of type Cat.
in dogSpace the findObject, objects and object should be of type Dog.
How can I implement such a scenario?
nimo frey
Ranch Hand
Posts: 580
posted 16 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Maybe I have to implement a abstract class or a class with a abstract <T>:
Is this the right way?
Is this the right way?
posted 16 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Close: the <T> goes directly after the class name:
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
nimo frey
Ranch Hand
Posts: 580
posted 16 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
okay, I guess, this is the right way to go.
I instantiate MyAssociations <T> with a concrete object:
What is wrong?
I instantiate MyAssociations <T> with a concrete object:
What is wrong?
posted 16 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
MyAssociations<Cat> myAssociations= new MyAssociations<Cat>;
The T can only be used inside the class. Outside of it, it does not exist; it is replaced by Cat, Dog or Mouse.
Also be careful; your code will throw a NullPointerException because myAssociations.getObject() will return null until you've actually set the object.
The T can only be used inside the class. Outside of it, it does not exist; it is replaced by Cat, Dog or Mouse.
Also be careful; your code will throw a NullPointerException because myAssociations.getObject() will return null until you've actually set the object.
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
nimo frey
Ranch Hand
Posts: 580
posted 16 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
And what about that:
Now, I instantiate the generic object MyAssociations:
What is wrong?
Now, I instantiate the generic object MyAssociations:
What is wrong?
posted 16 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
When you use <?>, you are indicating you know nothing at all about the generic type. That means that test.getMyAssociations().getObject() can only return Object - because whatever it may return, it's always an instanceof of Object or a subclass of Object.
Likewise, test.getMyAssociations().setObject(x) will not compile, whatever x is. That's because the compiler cannot check if x matches the generic type - because it doesn't know it. The following example will show you why:
Likewise, test.getMyAssociations().setObject(x) will not compile, whatever x is. That's because the compiler cannot check if x matches the generic type - because it doesn't know it. The following example will show you why:
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
nimo frey
Ranch Hand
Posts: 580
posted 16 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
oh now I guess, I understand.
So I have to assign a explicit type:
then it works.
Is there way to assign the explicit type via the constructor of Test?
and then I assign a explicit type via constructor:
I guess, this does also not work, am I right?
So I have to assign a explicit type:
then it works.
Is there way to assign the explicit type via the constructor of Test?
and then I assign a explicit type via constructor:
I guess, this does also not work, am I right?
nimo frey
Ranch Hand
Posts: 580
posted 16 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Okay, got it. I assign a direct type instead of the generic type, then it works.
thank you very much!
thank you very much!
| I don't get it. A whale wearing overalls? How does that even work? It's like a tiny ad wearing overalls. Paul Wheaton's 16th Kickstarter: Gardening playing cards for gardeners and homesteaders https://coderanch.com/t/889615/Paul-Wheaton-Kickstarter-Gardening-playing |












