简体   繁体   中英

Calling non-static unimplemented methods using an interface reference

I'm following a tutorial here https://www.concretepage.com/spring-boot/spring-boot-security-rest-jpa-hibernate-mysql-crud-example to create a basic Spring Boot CRUD project, I'm trying to understand what is happening with a particular piece of code.

My understanding of Interfaces in java is that you cannot call a method signature of an interface that is not static and is unimplemented. But here in ArticleService.java , a reference to an IArticleDAO interface is created, he then uses that reference type to call methods from within that interface IArticleDAO.java

Why does that work? there is no implementation for those methods in that interface so I don't understand how he could call those methods firstly, and secondly that they would do anything or return anything.

An interface is a way to define method signatures. An object that implements an interface can then be assigned to an instance of that interface and you can absolutely call them because they are implemented.

So if class A implements B then you can assign an instance of A to a B variable: B b = new A()

An additioal abstraction in this case is taht the field being used is @AutoWired . This means the Spring Framework will find an instance object that implements the interface and set the field.

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