aspose file tools*
The moose likes Java in General and the fly likes java desing issue Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "java desing issue" Watch "java desing issue" New topic
Author

java desing issue

nimo frey
Ranch Hand

Joined: Jun 28, 2008
Posts: 580
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?

Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

What does MyClass represent? What's its function?


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
nimo frey
Ranch Hand

Joined: Jun 28, 2008
Posts: 580
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?
nimo frey
Ranch Hand

Joined: Jun 28, 2008
Posts: 580
Maybe I have to implement a abstract class or a class with a abstract <T>:



Is this the right way?


Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19722
    
  20

Close: the <T> goes directly after the class name:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
nimo frey
Ranch Hand

Joined: Jun 28, 2008
Posts: 580
okay, I guess, this is the right way to go.

I instantiate MyAssociations <T> with a concrete object:



What is wrong?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19722
    
  20

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.
nimo frey
Ranch Hand

Joined: Jun 28, 2008
Posts: 580
And what about that:



Now, I instantiate the generic object MyAssociations:



What is wrong?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19722
    
  20

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:


nimo frey
Ranch Hand

Joined: Jun 28, 2008
Posts: 580
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?

nimo frey
Ranch Hand

Joined: Jun 28, 2008
Posts: 580
Okay, got it. I assign a direct type instead of the generic type, then it works.

thank you very much!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: java desing issue