• Post Reply Bookmark Topic Watch Topic
  • New Topic

Multiple interchangeable classes  RSS feed

 
Peter Ream
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have written an application that reads a PDF file as input (this is done using class TeeSheet).  I found another way to get the same information from a text file.  I want to develop a new class called TeeSheetText.  These two classes will function interchangeably.  I could make the two classes with the same name and build with one or the other.  I would prefer to have the two classes and use one or the other.

I am not even sure what to search for.  Is this a use of interfaces?  Not sure where to go or start.
 
Tim Holloway
Saloon Keeper
Posts: 18789
74
Android Eclipse IDE Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is, in fact, a classic case for Interfaces.

An Interface is a contract. It says that any class that implements that interface will provide certain methods that have certain attributes. Which class is providing those methods and how it implements those methods is not indicated, but your calling code will still compile if you swap classes.
 
Peter Ream
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you.  Started the implementation and it is working great.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!