I had this same confusion when first interacting with java (using the exact same library) - all the functions I want to use take an IAtomContainer
as their input, but I can only seem to make AtomContainer
objects.
The answers to this question, "Can we instantiate an interface in Java 8?" are relevant here. Particularly,
The quick answer would be “No”. you can never instantiate a interface in java however, you can refer to an object that implements an interface by the type of the interface.
For instance, here is a static method that takes a molecular formula string and returns an IAtomContainer
:
LoadJavaClass["org.openscience.cdk.tools.manipulator.MolecularFormulaManipulator"];
ac = MolecularFormulaManipulator`getAtomContainer[
"C2H4",
DefaultChemObjectBuilder`getInstance[]
]
(* «JavaObject[org.openscience.cdk.AtomContainer]» *)
The method actually returns an AtomContainer
: a concrete class that implements the interface IAtomContainer
.
Just to be clear - the molecule generated from the formula string is just a collection of atoms, with no bond information:
In[12]:= ac@getBondCount[]
Out[12]= 0
I can't speak to why the VABCVolume
descriptor is complaining, but it isn't due to feeding it the wrong type of argument:
sp = JavaNew["org.openscience.cdk.smiles.SmilesParser", getInstance[]];
atom = sp @ parseSmiles @ "C";
LoadJavaClass @ "org.openscience.cdk.geometry.volume.VABCVolume";
VABCVolume`calculate @ atom
During evaluation of In[23]:= Java::excptn: A Java exception occurred: org.openscience.cdk.exception.NoSuchAtomTypeException: The AtomType null could not be found
at org.openscience.cdk.config.AtomTypeFactory.getAtomType(AtomTypeFactory.java:268)
at org.openscience.cdk.geometry.volume.VABCVolume.calculate(VABCVolume.java:96).
Out[24]= $Failed