• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How do you instantiate this class?

 
Simon Forth
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you have

what syntax is required to instantiate a class Hose object?


 
Paul Clapham
Sheriff
Posts: 20982
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That would depend on what constructors were available.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If there no constructor, then compiler insert default no-argument constructor for your class.
 
Matthew Brown
Bartender
Posts: 4566
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suspect the question is really about the generic types. E.g. you can't exactly go:
new Hose<Hose<Hose<Hose<Hose<.. infinite number of Hoses.....>>>>>();
 
Matthew Brown
Bartender
Posts: 4566
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I haven't worked out a way of instantiating it directly (without cheating and omitting the generics). I'd be interested to see a solution to that. But I can create a non-generic subclass that can then be instantiated:
 
Karthikeyan Kandasamy
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can create a Hose object without type and with type. I think, if you create a Hose object without type, it will use Hose as E.

 
Matthew Brown
Bartender
Posts: 4566
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Karthikeyan Kandasamy wrote:You can create a Hose object without type and with type. I think, if you create a Hose object without type, it will use Hose as E.

No, if you omit the type it's the same as leaving out the type when creating collections. You lose the type-safety completely.

The other approach you've used - creating a non-generic subclass - is the same as mine.
 
Karthikeyan Kandasamy
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I came to that conclusion because of this code snippet. I tried passing an object to the Hose constructor and it gave a compiler error.




Error:

GenericSamples7.java:11: cannot find symbol
symbol : constructor Hose(java.lang.Object)
location: class genericsamples.Hose
Hose h2 = new Hose(new Object());
^
1 error
 
Matthew Brown
Bartender
Posts: 4566
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Apologies, you're right.

If you don't give the generic type, then anywhere the compiler needs to know the type (like in that constructor) it uses the "erasure" of that type. Which, if the type has an upper bound, is that upper bound. So in this case, because it's E extends Hose, will be Hose.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic