• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

java desing issue

 
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?

 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What does MyClass represent? What's its function?
 
nimo frey
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe I have to implement a abstract class or a class with a abstract <T>:



Is this the right way?


 
Sheriff
Posts: 22504
122
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Close: the <T> goes directly after the class name:
 
nimo frey
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
okay, I guess, this is the right way to go.

I instantiate MyAssociations <T> with a concrete object:



What is wrong?
 
Rob Spoor
Sheriff
Posts: 22504
122
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And what about that:



Now, I instantiate the generic object MyAssociations:



What is wrong?
 
Rob Spoor
Sheriff
Posts: 22504
122
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay, got it. I assign a direct type instead of the generic type, then it works.

thank you very much!
reply
    Bookmark Topic Watch Topic
  • New Topic