Skip to content
Advertisement

Spring implements to kinds of Repository interface

I’m trying to study Spring more specific Spring Data but i have a question about the use of interface Repository and its derivatives classes and i asking you help for clarify them.

@Repository
public interface GuestRepository extends CrudRepository<Guest, Long>, 
         {}

For example i see this code example extends CrudRepository but i see the using of extends keyword but no implements. why ? I thought that extends is for sub classes or if i using abstract classes and implements for interfaces.

the other question trying to test another cases i try to extends from two classes (i know java not allow multiple) but java allowed me to do it.

@Repository
 public interface GuestRepository extends CrudRepository<Guest, Long>, 
 PagingAndSortingRepository<Guest, Long> {}

And i see when i try to use some methods the ide shows to me without any problems and i try to execute i bring to me the data i request to it

enter image description here

So i’m kinda confused, when i talk extends java not allow extend from to or more classes but in this case does, but CrudRepository and PagingAndSortingRepository are interfaces so it should be with implements keyword. maybe some technnical concept in spring or java perhaps unknown to me.

In other post i see that is not posible create a class with two kind of repository but i can’t find any information about that.

I appreciate any help, thanks in advance

Advertisement

Answer

Interfaces CAN extend other interfaces, thus extends (GuestRepository is an interface)

classes implements interfaces, thus implements

You are not implementing anything here, sice Spring AOP will do “the implementation of queries” for you based on method names. This is kinds of Spring magic and is not Java specific. Using it as an example or reference to OOP and polymorphism in Java is rather bad.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement