File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes general methods to different classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "general methods to different classes" Watch "general methods to different classes" New topic
Author

general methods to different classes

Ricardo Soares
Greenhorn

Joined: Nov 27, 2012
Posts: 9
Hi everyone, im new to java so be gentle

imagine you have three different classes ( they are from different types also ), lets say:
class circle
class dog
class car
each one of this classes will have a method create, edit and remove. The create method will for example assign values to the variables ( which are different in each class ) and add the item to an ArrayList ( three different ArrayLists will contain the three different types ). Same for the edit and remove methods. My question is, is there anyway i can make those methods general ? Have a method create that will work for every class ?

Thanks in advance
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38519
    
  23
Welcome to the Ranch

Are those really different types? Dog Circle and Car look different, but you are telling me they have common behaviour. You would have to explain a bit more. What is the create method doing which is not done in the constructor? Is it creating a new instance?
Can you put wrappers round those objects? Can you make the implement a common interface? Can they inherit from a common (maybe abstract) supertype?
There is something strange about methods which create an object and add the object to a List simultaneously. In fact, doing that in a constructor might cause serious errors. Really, a method should do one thing and only one. Why do the objects have to add themselves to the list, rather than something else adding them to the List?

You would have to explain a lot more about what you want to do, so we can understand the problem.
Ricardo Soares
Greenhorn

Joined: Nov 27, 2012
Posts: 9
The dog car circle was an example, in reality im working with Agent, Artist and Event. They are gonna have similar methods because i need to create, edit, delete and print all of them but they are different.
Example:
create an artist - set name, id - add to artists list
create an event - set name, data, duration ( days ) - add to events list
create an agent - set name, id - add to agents list
I asked if i could make those methods general to avoid code repetition and make it the best way possible.
Then they will have to interact with each other, there will be a contract that will be signed between an artist and an agent. I will have to assign a few artists to a given event, etc
I think they can inherit from a superclass although i cant think how should i do that. Maybe creating an interface could be a good solution.
Im not into that wrapper thing yet, is it what is explained here ? http://docs.oracle.com/javase/tutorial/collections/implementations/wrapper.html
Hope you can understand it better now. Thanks for your time and help
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7718
    
  20

Ricardo Soares wrote:Example:
create an artist - set name, id - add to artists list

I think you're confusing two different things here. An Artist is an Artist; the fact that you're adding him/her to a List is totally irrelevant.

I suppose it would be theoretically possible to define a common interface for all 3 classes called something like WillBePartOfAList, but to be honest I can't see any benefit to it.

Programming is hard enough without dreaming up added complexity simply to save you typing. To paraphrase Albert Einstein: Keep things as simple (←click) as possible, but no simpler.

HIH

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38519
    
  23
What you are describing for create sounds like a constructor. Constructors are not methods. You ought (in my opinion) to initialise all your instance fields in the constructor (but there is not consensus on that point, as you will see from this interesting discussion). As I said before, you ought not to add the object to the List from the constructor. In fact you probably ought not to add the object to a List from inside itself. An object should take care of itself, but things like adding to Lists belong in different classes.
Artists, Events and Agents are different, having little common behaviour. Agree with Winston it is very peculiar design to try to make the three implement a common interface. Even though they all inherit from java.lang.Object.
Ricardo Soares
Greenhorn

Joined: Nov 27, 2012
Posts: 9
This is what i have at the moment without the adding to ArrayList, etc. Is this well structured ? Do i have to do it this way with Agents and Artists ?

Thanks for the time spent









Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38519
    
  23
No! Never use public fields like that. Make all the fields private. In the case of an event, they might beneficially be final, too.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: general methods to different classes