3

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

Deb
  • 2,922
  • 1
  • 16
  • 32
Martin
  • 1,977
  • 5
  • 30
  • 67

2 Answers2

4

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

Eugene
  • 117,005
  • 15
  • 201
  • 306
2

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;

}
Karol Dowbecki
  • 43,645
  • 9
  • 78
  • 111
  • this might not be an Option at all, in case you *do* need to inherit all of `User` properties – Eugene Aug 07 '18 at 12:24
  • i indeed need to inherit the user's properties. I renamed my entity class' method to getEncryptedPassword since that's actually what it returns. The compiler is happy now. – Martin Aug 07 '18 at 12:29