Object vs Extension in Java

Maybe I'm wrong, because I do not have too much experience with Java, but here's the question.

I have a class that contains many methods (basically this is a simple library).

I create an object of this class, say MyLibrary obj = new MyLibrary(parameters);

The parameters set the necessary functionality for the library to work properly.

Then I can call obj.getSomething / obj.setSomething / obj.createSomething etc. etc....

In my main class, I really need only one such library object.

Now ... It would be more useful for me not to use it as an object, but to put it as extends , and then create a function inside the library, such as a constructor, which I would call manually?


EDIT:

The connection between one class and MyLibrary is very close. Basically, I have many classes that do similar things, but have several different higher level functionalities. Therefore, I highlighted the method that should be in all these classes.

It seems to be very similar to the shape and triangle class, circle , square . So, MyLibrary is like a shape that contains all the MyLibrary .

+2
source share
8 answers

What you described strongly resembles a utility class similar to Java Collections . The class has only static methods and a private constructor to prevent instances. This is a well-known idiomatic pattern in Java - you can use it to create your own groups of methods that provide the corresponding functionality.

You should not distribute or even instantiate utility classes at all. Starting with Java-5, you can statically import them so you can use their methods without explicitly referencing their class.

+5
source

extends used when you need an inheritance hierarchy. It seems more logical to place your code in two separate classes, as it is now.

In addition, if your “library class” does several unrelated things, it should probably be divided into several classes — one for each task.

+3
source

You should only use extends if you have an is-a relationship. So you can think my main class is MyLibrary or should have a class MyLibrary.

From the problem described, it seems that MyLibrary is the way to go.

+3
source

With the details you provided, you can consider the Singleton template.

extends should only be used when one object must inherit the characteristics and functionality of another, because they are very closely related. For example, if you have a Shape class, you must extend Shape to create a Circle , Square and Triangle . Before using extends , you should learn more about inheritance, and when you should and should not use it.

0
source

I would use the static class to use. Compatible with javas class MATH API for math class . You can just use class methods without creating it.

0
source

Well, if your class, if you perform utility functions, then you should mark all methods as static and use operations such as

 MyLibrary.doSomething(); MyLibrary.createSomething(); MyLibrary.getSomething(); 

But this will not allow you to store some data members in the class, and if you save them, they will also be static.

I do not think extends suits your case.

Also, if you want to save only an object, you should look at the Singleton A class, for which only one instance can be created.

0
source

Assuming you're just using MyLibrary and can't change it, you should use a wrapper that does all this singleton, as Code-Guru has already suggested.

 public class MyLibraryWrapper { private static MyLibrary instance = null; private MyLibraryWrapper() {} public static MyLibrary getInstance() { if (instance == null) instance = new MyLibrary(); return instance; 

So in your code you would use

 MyLibraryWrapper.getInstance().getSomething(); 
0
source

The best way to create a singleton in java 1.5 or higher is to use ENUM.

 public enum Test { INSTANCE; } 

INSTANCE is the only instance of the Test class.

0
source

All Articles