18.04.2015 Views

ArcGIS Engine Developer Guide

ArcGIS Engine Developer Guide

ArcGIS Engine Developer Guide

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

JAVA APPLICATION PROGRAMMING INTERFACE<br />

IFeature feature = featureClass.getFeature(i);<br />

IGeometry geom = feature.getShape();<br />

if (geom.getGeometryType() == esriGeometryType.esriGeometryPolygon){<br />

/* Note: "Polygon p = (Polygon) geom;" will give ClassCastException */<br />

Polygon poly = new Polygon(geom);<br />

doSomeProcessingOnPolygon(poly);<br />

}<br />

The polygon object poly thus constructed will implement all interfaces implemented<br />

by the Polygon class. Consequently, you can call methods belonging to any<br />

of the implemented interfaces on the poly object.<br />

You could write all your code using the object constructors alone, but there are<br />

times when it might be better to cast an object implementing a particular interface,<br />

not to a class type, but to another interface implemented by that object.<br />

Continuing the previous example, suppose you want to use the<br />

doSomeProcessingOnPolygon(Polygon p) method not only on Polygon objects but on<br />

other objects implementing IArea, such as Envelope and Ring. You could write a<br />

generic doSomeProcessingOnArea(IArea area) method that works on all objects<br />

implementing IArea. As Polygon, Envelope, and Ring objects all implement the IArea<br />

interface, you could pass in those objects to this generic method, thereby preventing<br />

the need to write additional methods for each object type, such as<br />

doSomeProcessingOnEnvelope(Envelope env) and doSomeProcessingOnRing(Ring ring). To<br />

accomplish this, you would need to cast from the IGeometry type to the IArea<br />

type. In Java, this is typically done using interface cross-casting.<br />

/* Incorrect usage: will give ClassCastException */<br />

IArea area = (IArea) geom ;<br />

However, for the same reason noted in the class cast above, such a cast would fail<br />

with a ClassCastException. To be able to cast to the ArcObjects interface, you will<br />

need to use the interface proxy classes discussed earlier in this section. In the<br />

<strong>ArcGIS</strong> API for Java, you achieve the equivalent of an interface cross-casting by<br />

using the InterfaceProxy of the interface being casted to.<br />

IArea area = new IAreaProxy(geom);<br />

The following code shows the use of an InterfaceProxy class to cross-cast the geom<br />

object to IArea:<br />

IFeature feature = featureClass.getFeature(i);<br />

IGeometry geom = feature.getShape();<br />

/* Note: "IArea area = (IArea) geom;" will give ClassCastException */<br />

IArea area = new IAreaProxy(geom);<br />

doSomeProcessingOnArea(area);<br />

Using the IAreaProxy class as shown in the code above allows you to access the<br />

object through its IArea interface so that it can then be passed to a method that<br />

takes an argument of type IArea. Thus, in this particular example, one method<br />

can deal with three different object types. However, only methods belonging to<br />

the IArea interface will be valid for the area object. To call other methods of the<br />

object, you will need to either class-cast to the appropriate object type using its<br />

object constructor or get a reference to the other interfaces using the<br />

InterfaceProxy classes.<br />

190 • <strong>ArcGIS</strong> <strong>Engine</strong> <strong>Developer</strong> <strong>Guide</strong>

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!