I may be wrong as I have not got too much experience with Java, but here is a question.
I have a class which contains many methods (basically it is a simple library).
I create an object of this class let's say MyLibrary obj = new MyLibrary(parameters);
The parameters set up any necessary functionality for the library to run correctly.
Then I can call obj.getSomething
/obj.setSomething
/obj.createSomething
etc etc...
In my main class I really need only one this kind of library object.
Now... Would it be more useful for me not to use it as an object, but put it as extends
and then create a function inside of the library like a constructor which I would call manually?
EDIT:
The relation between the one class and MyLibrary is very close. Basically, I have many classes which do similar things but have some different higher layer functionality. So I separated method which must be in all those classes.
It seems it is very similar to shape
class and triangle
, circle
, square
example. So MyLibrary
is similar to shape
which contains all the foundation.
With the limited detail that you have provided, you might want to consider the Singleton pattern.
extends
should only be used when one object needs to inherit the characteristics and functionality of another one because they are very closely related. For example, if you have aShape
class, then you would extendShape
to createCircle
,Square
, andTriangle
. Before you useextends
you should learn more about inheritence and when you should and should not use it.Assuming you are just using
MyLibrary
and may not alter it, you should use a wrapper that makes the whole thing a Singleton, as already proposed by Code-Guru.So in your code you would use