• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Can I avoid having to create a new object here?

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello, gentlepeople of CodeRanch! This is some code I have trouble with:



Everything this code does works as expected. However, you see me creating a separate object casting "this" down to an interface it's definitely implementing if this method has been reached. I need to apply the cast so I can call the getCsvDelimiter() method later.

Can anyone explain to me why I have to create the separate object "parser" for this, when the object executing the readCsv(InputStream inputStream, String filename) is also capable of executing getCsvDelimiter()? I feel it's a little odd to create an entire separate Parser just to use its interface method.

Some more explanation:
The class executing this method is an abstract class SuperParser. It's realized in a subclass ArticleParser. Because the ArticleParser handles files in .csv format, it implements the interface CSVParser.

I hope this was clear enough for you to formulate an answer! If anything's not, let me know.

-Erik Kan
 
Erik Kan
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Some more clarification:

I and O are generic types representing input and output objects. In this case: I is an Iterable<CSVRecord> and O will be null.

Also, I realize I'm not using the parameter filename for anything yet, please ignore that!
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Two answers. One, it is *not* creating an object. It is merely declaring a reference variable to the same this instance. So, there is nothing to avoid here... Additionally, if you don't want to even declare the reference variable either, then you can also code the explicit cast inline -- meaning cast and call the method together (in the same expression).

And Two, this is only needed if the method is in a base class, or in an interface (default), that needs access to the subclass. Since, the method doesn't know the existence of the subclass, it can't possibly implicitly cast to the subclass, hence, you must explicitly cast.

Also, this is a real bad design. What would happen if you have another subclass that derives from the base class? It would be a different subclass, and when the method is called, you would get a class cast exception. In other words, this method is only useful for one particular subclass (or if the cast is to an interface, useful for subclasses that implement that interface).

Henry
 
Erik Kan
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:Additionally, if you don't want to even declare the reference variable either, then you can also code the explicit cast inline -- meaning cast and call the method together (in the same expression)



Do you mean:

this? Because that doesn't seem to compile: Eclipse says a CSVParser is not a valid argument for withDelimiter().

Henry Wong wrote:Also, this is a real bad design. What would happen if you have another subclass that derives from the base class? It would be a different subclass, and when the method is called, you would get a class cast exception. In other words, this method is only useful for one particular subclass (or if the cast is to an interface, useful for subclasses that implement that interface).


Is it correct that as long as the subclass implements CSVParser, the ClassCastException won't be thrown? Because that is my intention.
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Erik Kan wrote:
Do you mean:

this? Because that doesn't seem to compile: Eclipse says a CSVParser is not a valid argument for withDelimiter().


A method call has higher precedence than a cast. You need to add a pair of parenthesis, so that the cast has higher precedence.
Henry
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Erik Kan wrote:
Is it correct that as long as the subclass implements CSVParser, the ClassCastException won't be thrown? Because that is my intention.



Understood... regardless, it is a bad design.

Henry
 
Erik Kan
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the help, Henry! Looks like I'll be asking for a copy of Clean Code for christmas. :p
 
Erik Kan
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Closure: this is what I'm using now:

 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Erik Kan wrote:Looks like I'll be asking for a copy of Clean Code for christmas. :p


I agree with Henry, it's a bad design. You really shouldn't wait until Christmas for the off-chance that someone will give you a copy of "Clean Code"; go and gift yourself one right now!  Then read Chapter 2 on Names to see why I and O are horrible names.
 
Ranch Hand
Posts: 76
3
IntelliJ IDE Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:Then read Chapter 2 on Names to see why I and O are horrible names.



Well, they are generic type parameters after all. Would you also say names like T, R, K, V and E are also horrible?
 
We don't have time for this. We've gotta save the moon! Or check this out:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic