<div dir="ltr" style="text-align: justify;">
<p>&#8220;<b>Programming for interfaces than implementation</b>&#8221; is very popular principle in java programming and design pattern. Here I am going to explain some interested facts about interface in java in term of Spring Framework.</p>
<div class="separator" style="clear: both; text-align: center;"><img title="interface in java" src="https://dineshonjava.com/wp-content/uploads/2016/11/interfaceinJava.jpg" border="0" /></div>
<p>Here I am not going to explain that interface is a keyword in java we will focus on beyond the keyword. Actually interface in java is the core part of programming, it is not for hello world type of application, it is using for abstraction and decoupling concept in java. It is simple object oriented term to define contract or rules and abstraction between producer and consumer for applications.</p>
<div style="background-color: #f2f9fc; border: 1px solid #c9e6f2; border-radius: 3px; padding: 16px; line-height: 1.45;">
<p><span style="color: red; font-size: x-large; text-align: center;"><b>Popular Tutorials</b></span></p>
<ul style="text-align: left;">
<li><b><a href="https://dineshonjava.com/spring-tutorial/"><em><strong>Spring Tutorial</strong> </em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-web-mvc-framework-chapter-38/"><strong><em>Spring MVC Web Tutorial </em></strong></a></b></li>
<li><b><a href="https://dineshonjava.com/introduction-to-spring-boot-a-spring-boot-complete-guide/"><strong>Spring Boot Tutorial</strong> </a></b></li>
<li><b><a href="https://dineshonjava.com/spring-security-take-baby-step-to-secure/"><em>Spring Security Tutorial</em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-tutorial-with-example-aspect-advice-pointcut-joinpoint/"><em>Spring AOP Tutorial</em></a></b></li>
<li><b><a href="https://dineshonjava.com/using-spring-jdbc-framework-chapter-32/"><em>Spring JDBC Tutorial</em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-hateoas-hypermedia-driven-restful-web-service/"><em><strong>Spring HATEOAS </strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/microservices-with-spring-boot/"><em><strong>Microservices with Spring Boot</strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/jax-rs-web-service-tutorial/"><strong><em>REST Webservice</em> </strong></a></b></li>
<li><b><a href="https://dineshonjava.com/core-java-baby-step-to-be-best-java-ian/"><em><strong>Core Java </strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/hibernate-3-on-baby-steps/"><em><strong>Hibernate Tutorial</strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-batch-process-with-example/"><strong><em>Spring Batch</em> </strong></a></b></li>
</ul>
</div>
<h3><b>Advantages or key facts of making use of them in Java </b></h3>
<ul style="text-align: left;">
<li style="list-style-type: none;">
<ul style="text-align: left;">
<li>In very basic it allows us for multiple inheritance in java.</li>
<li>In Spring Dependency Injection interface is very powerful to run time injection of various concrete implementations of an interface in the application. By using references to interfaces instead of their concrete implementation classes help to minimize ripple effects, as the user of interface reference doesn&#8217;t have to worry about the changes in the underlying concrete implementation.</li>
<li>Interfaces are a way to declare a contract for implementing classes to fulfill, it&#8217;s the primary tool to create abstraction and decoupled designs between consumers and producers.</li>
<li>In an example we have a service to implement to save employee data to RDBMS and NoSQL database. If we were not using interface, the EmployeeRegistrationService may be implemented with two functions saveToRDBMS() and saveToNoSQL().</li>
</ul>
</li>
</ul>
<pre class="highlight">public class EmployeeRegistrationService { 
 public void saveToRDBMS(Employee employee ) { 
 //save to RDBMS 
 } 
 public void saveToNoSQL(Employee employee ) { 
 //save to NoSQL DB 
 } 
} 
</pre>
<p>In this case, the EmplyeeRegistrationController should be aware of the concrete implementation of these two functions in EmployeeRegistrationService to use them. Suppose we want to add additional functionality to save the information as JSON is required then you will have to add a new function saveToJson() in the Service class as well as make changes in the Controller. This adds lots of complication to maintenance of our huge application with hundreds of controllers and services. To avoid these complications we could use interface instead of implementation of registration service.</p>
<pre class="highlight">interface EmployeeRegistrationService { 
 void save(Employee employee ); 
 } 
</pre>
<p>Now controller doesn&#8217;t care about the concrete implementation of service, it is only aware of this interface, which has a save method.</p>
<pre class="highlight">public class EmployeeServiceRDS implements EmployeeRegistrationService { 
 @Override 
 public void saveToRDBMS(Employee employee ) { 
 //save to RDBMS 
 } 
} 
public class EmployeeServiceNoSQL implements EmployeeRegistrationService { 
 @Override 
 public void saveToNoSQL(Employee employee ) { 
 //save to NoSQL DB 
 } 
} 
</pre>
<pre class="highlight">@Controller 
Class EmployeeController { 
 
 @Resource(name="employeeServiceRDS ") 
 EmployeeRegistrationService registrationService ; 
 
 @RequestMapping("/emp-save") 
 public void saveEmployee(Employee employee) { 
 registrationService.save(employee); 
 } 
 } 
</pre>
<p>This highly reduces the software modification and extension cost. As changes in one layer does not effect other layer and new functionalities are made available to other layer immediately. Thus using interface gives you more power over extending and maintaining your application, utilize abstraction and implement good software development practices.</p>
<p> ;</p>
<div style="background-color: #f2f9fc; border-radius: 3px; border: 1px solid #c9e6f2; line-height: 1.45; padding: 16px;">
<p><span style="color: red; font-size: x-large; text-align: center;"><b>Spring Related Topics you may like</b></span></p>
<ul>
<li><b><a href="https://dineshonjava.com/interview-questions-on-spring/"><span style="color: red;">Spring Interview Questions and Answers</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-interview-questions-and-answers/"><span style="color: red;">Spring AOP Interview Questions and Answers</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-mvc-interview-questions-and-answers/"><span style="color: red;">Spring MVC Interview Questions</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-security-interview-questions-and-answers/"><span style="color: red;">Spring Security Interview Questions and Answers</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-rest-web-services-interview-questions-and-answers/"><span style="color: red;">Spring REST Interview Questions and Answers</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-boot-interview-questions-and-answers/"><span style="color: red;">Spring Boot Interview Questions and Answers</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-boot-microservices-interview-questions-answers/"><span style="color: red;">Spring Boot Microservices Interview Questions and Answers</span></a></b></li>
<li><b><a href="https://dineshonjava.com/dependency-injection-in-spring/"><span style="color: red;">Dependency Injection (DI) in Spring </span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-ioc-container/"><span style="color: red;">Spring IoC Container</span></a></b></li>
<li><b><a href="https://dineshonjava.com/what-is-bean-factory-in-spring/"><span style="color: red;">What is Bean Factory in Spring</span></a></b></li>
<li><b><a href="https://dineshonjava.com/application-context-in-spring/"><span style="color: red;">ApplicationContext in Spring</span></a></b></li>
<li><b><a href="https://dineshonjava.com/bean-autowiring-in-spring/"><span style="color: red;">Bean Autowiring in Spring</span></a></b></li>
<li><b><a href="https://dineshonjava.com/understanding-bean-scopes/"><span style="color: red;">Spring Bean Scopes</span></a></b></li>
<li><b><a href="https://dineshonjava.com/create-custom-bean-scope-in-spring-example/"><span style="color: red;">Create Custom Bean Scope in Spring Example</span></a></b></li>
<li><b><a href="https://dineshonjava.com/using-applicationcontextaware-in-spring/"><span style="color: red;">Using ApplicationContextAware in Spring</span></a></b></li>
<li><b><a href="https://dineshonjava.com/bean-lifecycle-and-callbacks/"><span style="color: red;">Spring Bean Life Cycle and Callbacks</span></a></b></li>
<li><b><a href="https://dineshonjava.com/writing-beanpostprocessor-in-spring/"><span style="color: red;">BeanPostProcessor in Spring</span></a></b></li>
<li><b><a href="https://dineshonjava.com/writing-beanfactorypostprocessor-in/"><span style="color: red;">BeanFactoryPostProcessor in Spring</span></a></b></li>
<li><b><a href="https://dineshonjava.com/annotations-in-spring-and-based/"><span style="color: red;"> Annotations in Spring and Based Configuration</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-jsr-250-annotations/"><span style="color: red;">Spring JSR-250 Annotations</span></a></b></li>
<li><b><a href="https://dineshonjava.com/jsr-330-annotations-in-spring/"><span style="color: red;">JSR 330 Annotations in Spring</span></a></b></li>
<li><b><a href="https://dineshonjava.com/stereotype-annotations-in-spring/"><span style="color: red;">Spring @Component, @Repository, @Service and @Controller Stereotype Annotations</span></a></b></li>
<li><b><a href="https://dineshonjava.com/method-injection-with-spring-using-lookup-method-property/"><span style="color: red;">Method injection with Spring using Lookup method property</span></a></b></li>
<li><b><a href="https://dineshonjava.com/introduction-to-aop-in-spring/"><span style="color: red;">Spring AOP-Introduction to Aspect Oriented Programming</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aspect-annotation/"><span style="color: red;">@Aspect Annotation in Spring</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-aspectj-before-annotation-advice-example/"><span style="color: red;"> Spring AOP AspectJ @Before Annotation Advice Example<br />
</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-before-advice-example-using-xml-config/"><span style="color: red;"> Spring AOP Before Advice Example using XML Config</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-aspectj-after-annotation-advice-example/"><span style="color: red;"> Spring AOP AspectJ @After Annotation Advice Example</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-after-advice-example-using-xml-config/"><span style="color: red;"> Spring AOP After Advice Example using XML Config</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-aspectj-after-returning-annotation-advice-example/"><span style="color: red;"> Spring AOP AspectJ @AfterReturning Annotation Advice Example</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-after-returning-advice-example-using-xml-config/"><span style="color: red;"> Spring AOP After-Returning Advice Example using XML Config</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-aspectj-after-throwing-annotation-advice-example/"><span style="color: red;"> Spring AOP AspectJ @AfterThrowing Annotation Advice Example</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-after-throwing-advice-example-using-xml-config/"><span style="color: red;"> Spring AOP After Throwing Advice Example using XML Config</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-aspectj-around-annotation-advice-example/"><span style="color: red;"> Spring AOP AspectJ @Around Annotation Advice Example</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-around-advice-example-using-xml-config/"><span style="color: red;"> Spring AOP Around Advice Example using XML Config</span></a></b></li>
<li><b><a href="https://dineshonjava.com/understanding-aop-proxies-chapter-31/"><span style="color: red;">Spring AOP Proxies in Spring</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-transaction-management-in/"><span style="color: red;"> Spring AOP Transaction Management in Hibernate</span></a></b></li>
<li><b><a href="https://dineshonjava.com/transaction-management-in-spring/"><span style="color: red;">Spring Transaction Management</span></a></b></li>
<li><b><a href="https://dineshonjava.com/declarative-transaction-management/"><span style="color: red;">Spring Declarative Transaction Management Example</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-ordering-of-aspects-with-example/"><span style="color: red;">Spring AOP-Ordering of Aspects with Example</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-security-java-based-configuration-with-example/"><span style="color: red;">Spring Security Java Based Configuration with Example</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-security-xml-namespace-configuration-example/"><span style="color: red;">Spring Security XML Namespace Configuration Example</span></a></b></li>
</ul>
</div>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/async-in-spring/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/what-is-meant-by-application-context-and-how-do-you-create-one/">Next</a> 
									 </div> 
									</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
 $.post('https://dineshonjava.com/wp-admin/admin-ajax.php', {action: 'mts_view_count', id: '101'});
});
</script>
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…