Monday, January 13, 2014

Java: Generics and Inheritance- Important things

Generics helps to write code in easy way.
-Generics makes code robust by adding strong type check at compile time check.
- Casting is not required as you define type while creating objects.


There is very important point to note related to Generics and Inheritance concept of OOPS.

Consider following java class:


import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class TestGenericList {

/**
* @param args
*/
public static void main(String[] args) {
List< String > strList  = new ArrayList < String >();
 strList .add("a");
 strList .add("b");
 strList .add("c");
TestGenericList t = new TestGenericList();
// Can we use below code to call printList?
// t.printList(strList );

}

public void printList(List < Object > list  ){
for (Iterator iterator = list.iterator(); iterator.hasNext();) {
Object object = (Object) iterator.next();
System.out.println(object);

}
}
}


We know that String extends Object so you assume that you can pass strList object to printList function.
WRONG
You can not pass strList to printList function as function accept list of Object.

Even though String extends Object but List does not extend List< Object >



You can refer java tutorials for more details at:
http://docs.oracle.com/javase/tutorial/java/generics/inheritance.html

Tuesday, December 31, 2013

How to download source of dependencies jars using Maven

To download source of dependencies jars in local repository while building project using Maven, you need to add "dependency:sources" goal while building your project.

Run following command for your project and it will download all sources for dependencies if it exists.
Command : mvn dependency:sources

You can find more information related to dependency plugin at Maven Dependency Plugin project.


Sunday, December 1, 2013

Java: List of books you must read

Here is list of must read books if you are java developer.
1. Head first OOAD
2. Effective java
3. Java puzzlers
4. Java concurrency in practice
5. Design patterns by GOF
6. Spring in action
Your comments are welcome if you want to add to this list.