Thursday, March 31, 2016

Interfaces Vs Abstract classes



This is a very important general question in interviews. First of all you may look following table to identify main differences between Interfaces and Abstract classes. 


InterfaceAbstract class
If you only have SRS, it is better to use interfaces.
This is the first stage of creating an application.
If application is being created partially,  then it is better to use abstract classes.
Every methods in interfaces are already public and abstract.Abstract classes may have both abstract and concrete methods.
It cannot be used private, protected, static, final, native... with interfacesAny access or non access modifier can be used with abstract classes.
Every variable inside interface are always public, static and finalNo restrictions
Serialization cannot be applied to interfaces.

Because serialization is convert objects into series of bytes. But we cannot create objects using interfaces. Then serialization cannot be applied to interfaces.
No restrictions
private, protected access modifiers cannot be used for variables in interface.No restrictions
Variables should be initialized at the time of declaration.No restrictions
Instance and static blocks are not applicable for interfaces.No restrictions
Constructors are not allowed in interfaces

Because variables are already static and initialized and static. 
No restrictions


Then lets move into real world problem. I'm going to create two concrete classes, one abstract class and one interface. Here you can see them.

Human.java interface

package com.app.demo;

public interface Human {

 public void eat();
 public void walk();
 public void work();


}


OtherHuman.java abstract class

package com.app.demo;

public abstract class OtherHuman {

 public void eat(){};
 public void walk(){};
 public void work(){}; 

}



Employee.java class

package com.app.demo;

public class Employee implements Human {

 @Override
 public void eat() {
 }

 @Override
 public void walk() {
 }

 @Override
 public void work() { 
 }

}



Student.java class

package com.app.demo;

public class Student extends OtherHuman {

 @Override
 public void eat() {
 }

 @Override
 public void walk() {
 }

}



Then you may read this. This is very simple demonstration. 

  • I have mention three methods, eat(), walk() and work() In both Human interface and OtherHuman abstract class.
  • You know, methods are called behaviors of objects.
  • Then Employee can have all those behaviors eat, walk and work. But Student can have only eat and walk. Because I assume student don't have a job.


When to use Interfaces and Abstract classes ?

  • If you implement any interface, you have to override each and every methods which are in implemented interface.
  • In this example if you implement Human interface to Student, then it have to override all those methods including work() also. 
  • You cannot avoid overrinding work() method if you are implementing Human interface.
  • So this is the time to use abstract classes.
  • If you use OtherHuman abstract class instead of using Human interface, then you can choose whatever methods which you need to be included to Student class.
  • That is why I extended OtherHuman abstract class to Student class.
  • Then I can override eat() and walk() without work() method.


So I think now you have a good idea about the difference between interfaces and abstract classes. Hope you can understand. 






Tuesday, March 29, 2016

Spring MVC hello world project




This is the third post under Spring MVC. In my previous posts I've mentioned following things.


In this post I'm gonna talk about, how to create your first Spring MVC hello world program. This is very simple. I'm going to create one controller and jsp page to demonstrate this with xml based configurations. I can deliver steps as follows.

  • Update web.xml file
  • Create front controller (dispatcher-servlet.xml)
  • Adding basic Beans to the front controller
  • Create Hello controller (Your own controller)
  • Create view page (index.jsp)

Update web.xml file

  1. Go to Spring reference Here I'm using Spring 4.1.6.RELEASE.
  2. Then search for dispatcher ( Hint: Ctrl+f and type dispatcher for search )
  3. Under The DispatcherServlet section, you will be able to see url mapping for front controller.
  4. Add this code to web.xml and do changes as follows.

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns="http://java.sun.com/xml/ns/javaee"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
 version="3.0">
 <display-name>SpringTest</display-name>
 <welcome-file-list>
  <welcome-file>index.html</welcome-file>
 </welcome-file-list>

 <servlet>
  <servlet-name>dispatcher</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <load-on-startup>1</load-on-startup>
 </servlet>

 <servlet-mapping>
  <servlet-name>dispatcher</servlet-name>
  <url-pattern>*.html</url-pattern>
 </servlet-mapping>

</web-app>

Create front controller (dispatcher-servlet.xml)


  • Create new Bean Configuration file in WEB-INF folder (Ctrl + n and type spring)

  • Give the name as dispatcher-servlet.xml (dispatcher in web.xml is exactly same as this file name)



  • Then you can add namespaces or you can just finish creating dispatcher-servlet (namespaces can be added later)
  • If you finished creating dispatcher-servlet.xml then you can add namespaces to the dispatcher-servlet using Namespaces tab.





<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/jsp/" p:suffix=".jsp"/>


  • Both are same, you can use whatever you prefer.
  • Finally your dispatcher-servlet.xml may look as below.


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:context="http://www.springframework.org/schema/context"
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">


 <context:component-scan base-package="com.app" />

 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
  <property name="prefix" value="/WEB-INF/jsp/" />
  <property name="suffix" value=".jsp" />
 </bean>

</beans>

  • Now your front controller(disptcher-servlet.xml) is ready :)

Create Hello controller (Your own controller)

  • Create a simple java class in src/main/java folder.
  • I use com.app.controller as my package for that controller class and I name it as HelloController.java


  • Then I create following method to use to view index page(I didn't create index.jsp yet).

package com.app.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class HelloController {
 
 @RequestMapping("/index")
 public String showPage(){
  return "index";
 }

}


  • @Controller - This is used to make this class as a controller.
  • @RequestMapping - This is used to map the browser request. DispatcherServlet(front controller) obtain user request and send it to relevant controller according to this mapping.
  • You can use any name as the method name in controller.
  • That is a String method and it returns a String value and that String value is captured by view resolver in dispatcher-servlet.xml 
  • Then it returns the relevant jsp page, we have to create this jsp page.

Create view page (index.jsp)


  • Create a jsp file in src->main->webapp->WEB-INF->jsp (I've created a folder called jsp in WEB-INF) Because we have defined this location in dispatcher-servlet.
  • You can add something to your file to identify it.
  • Your file name must be index.jsp. 
  • Because we return "index" in controller.


Now everything is ok. Then you can deploy your project.

First run

  • Right click on your project and go to Run As -> Maven build...

  • Then type "clean install" in Goal. Then Run.


  • Then it will clean your target directory and install all dependencies that you want to run the project (Because of this is the first run, you may don't have anything in target directory) 
  • Then update your project (Alt + F5)
  • Then right click on your project and then select Run As -> Run on Server(You have to create a server instance, here I've used Apache Tomcat ).

Now You will be able to see the output. If you are getting any errors, please check again and rebuild.