File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Class Design: Advise

 
Percy Frankland
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Supposing I was modeling a collection of projects. I suppose that I would have a two classes; one to represent the collection of Projects and a class for an individual Project. The collection of projects would have its properties and methods and a project would have its properties and methods. Now I think I know that there is a hashtable class. Can I base the Projects class as an extension of hashtable and would that be a sensible thing to do. The extended hashtable (class Projects) would contain the collection of Project objects. Does any of this make sense?
 
Anantha Sharma
Ranch Hand
Posts: 43
Android Hibernate jQuery
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if you are looking to have a collection of Projects then you can use any of the collection Classes

you would require a class (POJO) to hold the Project (as you mentioned) & another class which has a collection class to hold Project objects.



// now you can add each individual project objects to the collection projectSet
 
Campbell Ritchie
Sheriff
Pie
Posts: 47229
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Find the Java™ Tutorials section about Collections, and start by reading that.
 
Percy Frankland
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Anantha and Campbell.

And can I create projectSet so that its an extension of HashSet so that it can have its own properties an methods in addition to those it would inherit?

 
Rob Spoor
Sheriff
Pie
Posts: 20372
44
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you tried it?
 
Sagar Rohankar
Ranch Hand
Posts: 2904
1
Java Spring Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Percy Frankland wrote:
And can I create projectSet so that its an extension of HashSet so that it can have its own properties an methods in addition to those it would inherit?

Some points to ponder before extending any class which is not in direct control of your development.
1. Favor composition over inheritance,
2. Carefully override the methods and read the docs for the same.
Read #16 and #17 from "Effective Java", Joshua Bloch explain it beautifully.
 
Rob Spoor
Sheriff
Pie
Posts: 20372
44
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Who says HashSet isn't meant to be extended? It's methods are well enough documented for this. As a matter of fact, it's already extended - by LinkedHashSet.
 
Percy Frankland
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Rob and Sagar

My reasoning is that:

1. Am I right that basing a new class on extending an existing one doesn't affect the original Class in any way?
1.5 Are there classes that you can't extend?

2. If I created a class myProjs (not extending anything) and it owned a container for my child objects, say cProjs then that seems clumsy. You have to refer to myProjs.cProjs.oChild to access a child object. Logically myProjs.cProjs is just a collection of oChild objects.

3. If you extend Hashtable to create myProjs then you can access a child object by myProjs.oChild. It removes a level of "nesting" that provides nothing. myProjs is independant of the original Class, except that it inherits all it's properties and methods. I then just have to add any extra methods and propeties I need to manage my myProj object.

4. If this approach is correct than wouldn't eveyone creating a class that represented a collection of objects base their collection class on an existing standard container class.

Rob is right, I should try it; I just wanted to check whether what I was planning to do was stupid.

Thanks again, Percy
 
Rob Spoor
Sheriff
Pie
Posts: 20372
44
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Percy Frankland wrote:1. Am I right that basing a new class on extending an existing one doesn't affect the original Class in any way?

Yes. If you extend HashSet, and anybody creates a new HashSet, he will use the original version.

1.5 Are there classes that you can't extend?

Classes that are final, and classes that do not have an accessible constructor - a constructor that's private, or has default access but is in a package you can't add classes to (e.g. java.util).
 
Sagar Rohankar
Ranch Hand
Posts: 2904
1
Java Spring Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:Who says HashSet isn't meant to be extended? It's methods are well enough documented for this. As a matter of fact, it's already extended - by LinkedHashSet.

May be I haven't put it in right words, but corrected my post now.
 
Rob Spoor
Sheriff
Pie
Posts: 20372
44
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please don't update posts after they have been replied to. It changes the context of all posts after that, possibly making them no longer make sense.

But yes, your point 1) is very accurate. In this case, the object shouldn't be turned into a HashSet simply because it needs to be able to store data. It should contain a Set instead. If needed a few methods can be added to allow the editing of that Set - adding, removing, getting the size and iterating are the only ones that are usually needed.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic