2

I've been looking into creating a barcode scanner program, and in my research I came across something I've never seen before, and I'm not sure exactly what it is doing...

Here's a snippet of my program, including the line I don't understand:

import javax.comm.*;    

public class InvScan implements SerialPortEventListener {


static CommPortIdentifier portId1;
SerialPort serialPort1;


public InvScan(){
    serialPort1 = (SerialPort) portId1.open("InvScan", 2000); //what does this mean? 
}

See the line where I commented "what does this mean?". SerialPort is an abstract class... I know abstract classes can be instantiated using an anonymous class, but I've never seen this before. This line of code came from a tutorial for a program designed to do something related to what I want to do, but the author didn't explain what is going on here very well (or maybe I just didn't understand?). His notes say:

we are instantiating the SerialPort object by executing portId1’s open() method. Recall portId1 is a ComPortIdentifier object and the open() method comes from ComPortIdentifier.

I've done some research, and I can't even find another example of whatever is going on here (probably because I don't even know what to call it, really).

This isn't even really essential to my program, as I can just redesign it (and not be a script kiddie, haha), but it's really bugging me that I don't know what is going on in that line.

duck1992
  • 23
  • 2
  • 1
    The variable container type can be abstract, the instantiated type (which is retrieved from the open method call) will still be a concrete type deriving from the abstract base type. – ChristopheD Jun 02 '14 at 20:04
  • 1
    What don't you understand exactly? It's just a method that returns an object of type SerialPort. Its actual concrete type is a subclass of Serial Port, which could, or not, be an anonymous class. – JB Nizet Jun 02 '14 at 20:04

3 Answers3

1

The actual object is an instantiation of a non-abstract subclass derived from SerialPort. All this does is provide guarantees that the abstract methods have been fully defined within that instantiated object. Internally, this would look something like:

public class CommPortIdentifier ... {

...

    public CommPort open(java.lang.String appname, int timeout)
            throws PortInUseException {

        CommPort val = new NonAbstractCommPort( ... );

        // Do something here

        return val;
    }

...

}

Being an abstract subclass of CommPort, the cast to SerialPort is a little risky unless there are guarantees the actual return type of the object is a subclass of SerialPort.

andand
  • 17,134
  • 11
  • 53
  • 79
0

Whatever is returned from portId1.open is a subtype of SerialPort. An abstract class may still have concrete subtypes; what this code does is cast the returned object to a known supertype.

Thorn G
  • 12,620
  • 2
  • 44
  • 56
0

The line portId1.open("InvScan", 2000) calls a method that returns some subclass of SerialPort. Then it is cast to SerialPort. (Abstract classes can have non-abstract subclasses.)

Epiglottal Axolotl
  • 1,048
  • 8
  • 17