Thursday, April 25, 2013

What is Dependency Injection(DI)?

The technology that Spring is most identified with is the Dependency Injection (DI) flavor of Inversion of Control. The Inversion of Control (IoC) is a general concept, and it can be expressed in many different ways and Dependency Injection is merely one concrete example of Inversion of Control.

When writing a complex Java application, application classes should be as independent as possible of other Java classes to increase the possibility to reuse these classes and to test them independently of other classes while doing unit testing. Dependency Injection helps in gluing these classes together and same time keeping them independent.

What is dependency injection exactly? Let's look at these two words separately. Here the dependency part translates into an association between two classes. For example, class A is dependent on class B. Now, let's look at the second part, injection. All this means is that class B will get injected into class A by the IoC. 

Dependency injection can happen in the way of passing parameters to the constructor or by post-construction using setter methods. As Dependency Injection is the heart of SpringFramework, so I will explain this concept in a separate chapter with a nice example.

Benefits of Spring Framework

Another question that is related to Java and comes up quite often for those who have already used Spring is why to use it.
  1. Spring enables developers to develop enterprise class applications using POJOs. The benefit of using only POJOs is that you do not need an EJB container product such as an application server but you have the option of using only a robust servlet container such as Tomcat or some commercial product.
  2. Spring is organized in a modular fashion. Even though the number of packages andclasses are substantial, you have to worry only about ones you need and ignore the rest.
  3. Spring does not reinvent the wheel instead, it truly makes use of some of the existing technologies like several ORM frameworks, logging frameworks, JEE, Quartz and JDK timers, other view technologies.
  4. Testing an application written with Spring is simple because environment-dependent code is moved into this framework. Furthermore, by using JavaBean -style POJOs, it becomes easier to use dependency injection for injecting test data.
  5. Spring's web framework is a well-designed web MVC framework, which provides a great alternative to web frameworks such as Struts or other over engineered or less popular web frameworks.
  6. Spring provides a convenient API to translate technology-specific exceptions (thrown by JDBC, Hibernate, or JDO, for example) into consistent, unchecked exceptions
  7. Lightweight IoC containers tend to be lightweight, especially when compared to EJB containers, for example. This is beneficial for developing and deploying applications on computers with limited memory and CPU resources.
  8. Spring provides a consistent transaction management interface that can scale down to a local transaction (using a single database, for example) and scale up to global transactions (using JTA, for example).

Static access

Use of static:
  • Use static methods to implement behaviors that are not affected by the state of any instances.
  • Use static variable to hold data that is class specific as opposed to instance specific.
  • All static members belong to the class, not to any instance
  • A static method cannot access an instance variable directly.
  • static methods cannot be overridden, but they can be redefined.

Encapsulation

Why and how encapsulation is used?
  •  Encapsulation helps hide implementation behind an interface or API.
  • Encapuslated code has two features: 1) Instance variables are kept protected 2) Getter and Setter methods provide access to instance variables.
  • IS-A refers to inheritance or implementation
  • IS-A is expressed with the keyword extends
  • HAS-A means an instance of one class "has a" reference to an instance of another class.

Class access modifiers

There are a lot of access modifiers for declaring a class, a method or a variable and most of them are always asked during the initial interviews as they give the interviewer a first opinion about the level of the candidate.

There are four access levels: public, private, default and protected.
public: All other classes, regardless of the package they belong to, can access the member.
private: can be accessed by code only in the same class.
default: can be accessed by any element in the same package.
protected: It's the same as the default but it can also be accessed by the subclasses.

There are some more modifiers (nonaccess)
  •  Classes can also be modified with final, abstract, or strictfp
  • A class cannot be both final and abstract
  • A final class cannot be subclassed.
  • An abstract class cannot be instantiated.

Abstract Class vs Interface

This is a very common question that you can come across every Java Interview usually when you are looking for your first job.

Interfaces
  • Interfaces are contracts for what a class can do, but they say nothing about the way.
  • Interfaces can be implemented by any class.
  • An interface is 100% abstract class.
  • No concrete methods are allowed.
  • Can have constants, which are always implicitly public, static and final.
  • A class implementing an interface can be abstract and it doesn't have to implement the interface
  • Interfaces can extend one or more interfaces.
  • Interfaces cannot extend a class.
  • Interface methods must not be static.
Abstract Class
  • An abstract class can never be instantiated. Its sole purpose is to be extended.
  • Methods marked abstract end in a semicolon rather than curly brackets.
  • An abstract class can have both abstract and non-abstract methods.

Interface vs. abstract class
Choosing interfaces and abstract classes is not an either/or proposition. If you need to change your design, make it an interface. However, you may have abstract classes that provide some default behavior. Abstract classes are excellent candidates inside of application frameworks. 

Abstract classes let you define some behaviors; they force your subclasses to provide others. For example, if you have an application framework, an abstract class may provide default services such as event and message handling. Those services allow your application to plug in to your application framework. However, there is some application-specific functionality that only your application can perform. Such functionality might include startup and shutdown tasks, which are often application-dependent. So instead of trying to define that behavior itself, the abstract base class can declare abstract shutdown and startup methods. The base class knows that it needs those methods, but an abstract class lets your class admit that it doesn't know how to perform those actions; it only knows that it must initiate the actions. When it is time to start up, the abstract class can call the startup method. When the base class calls this method, Java calls the method defined by the child class.