Friday, June 4, 2010

Differencecs between java array list and vecor.

I have updated inwww.freejavaclass.com

Interviewer: Explain about java collections.

I have updated in http://www.freejavaclass.com/
Collection is a group of objects treated as single unit. Arbitrary objects can be add, delete and modified as element in collection. Java designs frequently handling group of elements as collection. The key interfaces used by the collection framework are List, Set and Map. The List and Set extends the Collection interface.


A Set is a collection with unique elements and prevents duplication within the collection. HashSet and TreeSet are implementations of a Set interface. A List is a collection with an ordered sequence of elements and may contain duplicates. ArrayList, LinkedList and Vector are implementations of a List interface.

The Collection API also supports maps, but within a hierarchy distinct from the Collection interface. A Map is an object that maps keys to values, where the list of keys is itself a collection object. A map can contain duplicate values, but the keys in a map must be distinct. HashMap, TreeMap and Hashtable are implementations of a Map Interface.

Collection framework provides flexibility, performance, and robustness.

• Polymorphic algorithms – sorting, shuffling, reversing, binary search etc.
• Set algebra - such as finding subsets, intersections, and unions between objects.
• Performance - collections have much better performance compared to the older Vector and Hashtable classes with the elimination of synchronization overheads.
• Thread-safety - when synchronization is required, wrapper implementations are provided for temporarily synchronizing existing collection objects.
• Immutability - when immutability is required wrapper implementations are provided for making a collection immutable.
• Extensibility - interfaces and abstract classes provide an excellent starting point for adding functionality and features to create specialized object collections.

Object oriented Java

I have updated in http://www.freejavaclass.com/
The Object Oriented Programming Languages directly represent the real life objects like Car, Jeep, Account, Customer etc. The features of the OO programming languages like polymorphism, inheritance and encapsulation make it powerful. [Tip: remember pie which, stands for Polymorphism, Inheritance and Encapsulation are the 3 pillars of OOPL]



Inheritance: The main purpose of inheritance is code reusability. In java we can achieve this by two ways,


• You can achieve inheritance as well polymorphism in is-a relationship.
• In has-a relationship you can achieve code reusability but can’t achieve polymorphism, the one way to achieve polymorphism is use interface implementation in your design.

Polymorphism: The ability of one object reference will hold the address of another object in the same inheritance hierarchy and capable of executing the method in the address holding object at runtime. The benefit of polymorphism is that it is very easy to add new classes of derived objects without breaking the calling code.

Encapsulation: Refers to keeping all the related members (variables and methods) together in an object. Being able to encapsulate members of a class is important for security and integrity and also improves code modularity by preventing objects interacting with each other in an unexpected way.

How can instantiate Java interface without implementing.

public interface Foo { String method(); } public class Claaa {    public static void main(String[] args) {     Foo fooByIC...