Saturday, August 18, 2012

Using File Resources

Loading a file in Java should be done through, either:
Class.getResource()
Class.getClassLoader().getResourceAsStream();

These two vary in the way they locate the file.
Class.getResource() searches file relative to package location of the class.

ClassLoader search happens by delegating the findResource to parent ClassLoader before this classloader itself does try. Specifying absolute path does not work.

Interface Vs Abstract Class

Interface Vs Abstract Class
A frequent question in interviews is when one should choose Interface and when to go with abstract class?
Interface specifies a "Is-Capable-of" doing something.
Abstract specifies a "Is-a" relation.

I would define a interface when there is necessarily nothing in common but the implementation should do somthing specific.
e.g. Iterator inteface- no specific on what type of data structure and what condition would it iterate.
e.g. Actor who is also a Director.

Abstract is to provide a default behavior. If I expect the system to undergo changes and requires a default behaviour over which special behaviour can be built.

Actor Director example