简体   繁体   中英

Return Type incompatible with Object.getClass()

the code is generated upon importing a webservice file on eclipse. However, I saw this error upon compiling (return type incompatible with Object.getClass() ).

Any ideas to fix this?

public java.lang.String getClass(){
    return localClass;
}

Add On:

   if (localClass != null){
                                            elementList.add(org.apache.axis2.databinding.utils.ConverterUtil.convertToString(localClass));
                                        } else {
                                           throw new org.apache.axis2.databinding.ADBException("Class cannot be null!!");
                                        }

I take it your web service is given as WSDL file. From this WSDL file some Java files are automatically generated. Apparently, your WSDL file contains a property named "class" and thus the corresponding generated Java class has a getter for this property called getClass() .

However, getClass() is a method which is defined in the Object class and all Java classes inherit from Object . Java thinks you are trying to override this method., which is not allowed, because 1) the method is final, and 2) the return type doesn't match.

You might want to look at the answer to this question , which mention how you can rename the property so that its getter doesn't conflict with standard Java methods.

getClass is a final method of Object which returns a Class . You are getting that error because the compiler thinks you are trying to override getClass while changing the return type to something that is not a Class . If you want to return a String you will need to change the method name to something else or add parameters to the method so its signature does not match Object#getClass . Note that you can't override it even if you want to return a Class because it is final.

 public  java.lang.String getClass(){
              return localClass;
   }

can be changed with

 public  Class<?> getClass(){
              return localClass;
 }

But as getClass is final method this cannot be overidden. If you want your own method then consider changing name.

I would actually prefer:

public final Class<? extends Object> getMyClass(){
    return localClass;
}

Which is generic. Since everything in Java inherits from Object, this matches everything loaded on the classpath. And you probably have issues because you are trying to override the final method .getClass() (so the name here is getMyClass() instead.

From the javadoc of .getClass() you can see that:

The actual result type is Class<? extends |X|> Class<? extends |X|> where |X| is the erasure of the static type of the expression on which getClass is called.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM