Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing a method that is also overriden

I am adding spring-security into my app and came across an issue. My implementation of UserDetails implements org.springframework.security.core.userdetails.UserDetails but also extends my User entity class. Both of these have a getPassword() method, the spring security interface's method returns a String and mine returns a byte array since the password is encrypted.

I want my implementation to implement the interface's method and not override my entity class' method but Netbeans keeps giving an error that the return type is invalid. I would like to avoid renaming my getPassword() method to work around this problem.

Is there a way of telling the compiler to implement the interface's method instead of overriding the superclass?

Thanks

like image 702
Martin Avatar asked Dec 02 '25 09:12

Martin


2 Answers

Return type is not taken into consideration when overloading, so essentially you can't really do that. I would simply rename my implementation.

like image 91
Eugene Avatar answered Dec 05 '25 00:12

Eugene


You can't do it, the return type can't be the only difference between two otherwise identical Java method signatures.

The easiest way to fix it is to declare User as a field instead of extending it:

public class MyUserDetails implements UserDetails {

  private final User user;

}
like image 44
Karol Dowbecki Avatar answered Dec 04 '25 23:12

Karol Dowbecki