Re: [mdr-users] proplem with the custom implementation of abstract classes

Martin Matula <[email protected]>
Newsgroups gmane.comp.java.netbeans.modules.mdr.user
Message-ID <[email protected]>
Hi Ludger,
Richard is correct in his reply. Compiler cannot compile this since 
createPort() method in the factory interface is declared to return Port 
- compiler cannot know what gets returned during the runtime of your 
application. All that's guaranteed is that it will be a subtype of Port 
interface. So, it cannot let you call methods that do not exist in Port 
interface. You have to do explicit cast to call the methods in other 
classes - i.e. if you are sure that the returned class will be a subtype 
of StructuralFeatureImpl (which it will be in this case), you can call:

// cast port to StructuralFeatureImpl
StructuralFeatureImpl sf = (StructuralFeatureImpl) port;
// call getText() method
sf.getText();

You probably come from Visual Basic world where these explicit casts are 
not necessary. In Java it is different.
Martin

ludger goeke wrote:

>Hi Martin,
>
>I got still a proplem with the custom implementation of abstract classes.
>I got the class Port that extends the abstract class StructuralFeature.
>I create the custom implementation StructuralFeatureImpl 
>that owns the method getText( ).
>
>When I try to run the following code 
>
>Port port =factory.createPort();  
>port.getText();
>
>
>
> cannot resolve symbol
>    [javac] symbol  : method getText ()
>    [javac] location: interface uml2module.ports.Port
>    [javac]          port.getText();
>
>__________________________________________________
>
>When I try to run the following code 
>
>StructuralFeatureImpl feature =  factory.createPort();    
>
>I get the following compiler error
>incompatible types
>    [javac] found   : uml2module.ports.Port
>    [javac] required: impl.uml2module.kernel.StructuralFeatureImpl
>    [javac]          StructuralFeatureImpl feature =  factory.createPort();
>
>
>It seems that the Custom Implementation don`t get mentioned. 
>Do you have an idea what could be the error ?
>
>Regards Ludger
>
>  
>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.