When should I return the Interface and when the concrete class?

when programming in Java I practically always, just out of habit, write something like this:

public List<String> foo() {
    return new ArrayList<String>();

Most of the time without even thinking about it. Now, the question is: should I always specify the interface as the return type? Or is it advisable to use the actual implementation of the interface, and if so, under what circumstances?

It is obvious that using the interface has a lot of advantages (that’s why it’s there). In most cases it doesn’t really matter what concrete implementation is used by a library function. But maybe there are cases where it does matter. For instance, if I know that I will primarily access the data in the list randomly, a LinkedList would be bad. But if my library function only returns the interface, I simply don’t know. To be on the safe side I might even need to copy the list explicitly over to an ArrayList:

List bar = foo();
List myList = bar instanceof LinkedList ? new ArrayList(bar) : bar;

but that just seems horrible and my coworkers would probably lynch me in the cafeteria. And rightfully so.

What do you guys think? What are your guidelines, when do you tend towards the abstract solution, and when do you reveal details of your implementation for potential performance gains?


For instance, if I know that I will primarily access the data in the list randomly, a LinkedList would be bad. But if my library function only returns the interface, I simply don’t know. To be on the safe side I might even need to copy the list explicitly over to an ArrayList.

As everybody else has mentioned, you just mustn’t care about how the library has implemented the functionality, to reduce coupling and increasing maintainability of the library.

If you, as a library client, can demonstrate that the implementation is performing badly for your use case, you can then contact the person in charge and discuss about the best path to follow (a new method for this case or just changing the implementation).

That said, your example reeks of premature optimization.

If the method is or can be critical, it might mention the implementation details in the documentation.


How to scale the Quartz scheduler?

I plan to use the Quartz scheduler as I read many good opinions about it.

My problem is as follows: I will have thousands of triggers living in the system at any given time. Most of the triggers will fire just one event and die. In addition, it is very likely I will have to cancel many jobs after their allocation (based on new input).

Can Quartz scale to this? Which JobStore is recommended? I planned to use the JDBC one over mysql.

Added Information: My jobs will either send an e-mail or post data via HTTP post to other services (over WAN).


There are some tips and recommendations in the FAQ:

How many jobs is Quartz capable of running?

An excerpt from the answer:

So let me just say, there are installments of Quartz out there that are managing hundreds-of-thousands of Jobs and Triggers, and that are, at any given moment in time executing several dozens of jobs – without even utilizing Quartz’s load-balancing capabilities.

There is also some discussion in the Quartz Users forum, with experience reports from developers who have used Quartz under similar conditions.

  • Scalability & Performance – Jim Nichols is running a couple of thousand jobs clustered, and volunteers more information by e-mail
  • Quartz performance – Stey Baptiste is running 2 million jobs a day, 10 jobs a second

Update 2015-01-12: Unfortunately, the links in the list above broke when the forum was moved, and I’m unable to find new working links. I’ll leave the links anyway, in case they help digging out the info from or elsewhere. The official Quartz forum is now quartz-scheduler on Google Groups.


javascript inheritance

I know there is a lot of similar questions are tons of great answers to this. I tried to look at the classical inheritance methods, or those closure methods etc. Somehow I consider they are more or less “hack” methods to me, as it doesn’t really what the javascript is designed to do. (Welcome anybody correct me if I am wrong). OK, as long as it works, I satisfy with the classical inheritance pattern like:

PARENTClass = function (basevar) { do something here; };
PARENTClass.prototype = { a: b, c: d}; // prototype is auto gen 

// Inheritance goes here
CHILDClass = function (childvar) { do something; };
CHILDClass.prototype = new PARENTClass(*1); // Actual inheritance to the prototype statement
// Instance
CHILDInstance = new CHILDClass(whatever);

Above is somehow, to my understanding the inheritance of JS. But one scenario I have no idea how to implement, is that what if I want to do some initializing DURING object creation (ie, within constructor), and the new object can be used right away…. My illustration on problem might not be too clear, so let me use the following C# Psuedo to explain what I want to do:

class PARENT {
    public PARENT (basevar) { ... }
class CHILD : PARENT {
    public CHILD (basevar) : PARENT (basevar) // constructor of child, and call parent constructor during construct.
    { ... }

For some reason (like init. UI elements), putting them in constructor seems the best way to do. Anyone have idea on how can I do it.

PS: in the *1, I have no idea what I should put there. PS2: The above situation I DID found the jquery.inherit library can do, I just wonder if not using library can achieve it. PS3: Or my understanding is wrong. Since javascript is not intended to mimick OOP (that’s why i call it hack), what is the “CORRECT” logic to implement this.


It is not a hack as such; JavaScript is a prototyped language, as defined by Wikipedia as where:

..classes are not present, and behavior reuse (known as inheritance in class-based languages) is performed via a process of cloning existing objects that serve as prototypes.

As it says, classes are not used in JavaScript; each object that you create is descended from the JavaScript Object; all objects in JavaScript have the prototype object, and all instances of objects you create ‘inherit’ methods and properties from their object’s prototype object. Take a look at the MDC prototype object reference for more information.

As of this, when you call the line:

CHILDClass.prototype = new PARENTClass();

This allows the CHILDClass object to add methods and properties to its prototype object from the PARENTClass object, which creates an effect similar to the idea of inheritance present in class-based languages. Since the prototype object affects every instance created of that object, this allows the parent object’s methods and properties to be present in every instance of your child object.

If you want to call your parent class’s constructor in your child class’s constructor, you use the JavaScript call function; this allows you to call the parent class’s constructor in the context of the child class’s constructor, therefore setting the newly prototyped properties in your child class to what they are set as in the parent class.

You also do not need to put anything where you have specified the *1, since that line is merely used to add the methods and properties to the child class’s prototype object; however, bear in mind that it calls the parent class’s constructor, so if there are any arguments that are fundamental in the operation of the parent class constructor, you should check that these are present so as to avoid JavaScript errors.


How Do I Clone A JAXB Object

I have some jaxb objects (instantiated from code generated from xsd by jaxb) that I need to clone. The Jaxb class does not appear to provide an interface for doing this easily. I can not hand edit the class and can not extend it – so I need to create a helper/utility method to do this. What is the best approach?


Given the purpose of JAXB, I think the easiest way would be to marshall your object to XML and unmarshall it back.

Lots more discussions on Google.

JAXB FAQ suggests beanlib.

There’s also some discussion (as well as a link to download) of a Cloneable plugin under jaxb2-commons, although I can’t find any reference on the project page.


How do I display a java ResultSet visually?

I’m looking for a way to display a java.sql.ResultSet on the screen. preferably built-into java or swing. If neither of those have a decent method that’s simple, I’d consider spring.



Loop over the results of you ResultSet and put in into a TableModel.

DefaultTableModel resultSetToTableModel(
     DefaultTableModel model,
     ResultSet row) throws SQLException
    ResultSetMetaData meta= row.getMetaData();
    if(model==null) model= new DefaultTableModel();
    String cols[]=new String[meta.getColumnCount()];
    for(int i=0;i< cols.length;++i)
        cols[i]= meta.getColumnLabel(i+1);


        Object data[]= new Object[cols.length];
        for(int i=0;i< data.length;++i)
    return model;

Then you can do something like

JOptionPane.showMessageDialog(null,new JScrollPane(new JTable(model)));

Hopt it helps

Source: stackoverflow
Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. By using this site, you agree to the Privacy Policy, and Copyright Policy. Content is available under CC BY-SA 3.0 unless otherwise noted. The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 © No Copyrights, All Questions are retrived from public domain..