Sunday, November 11, 2012

Difference Between Iterator and Enumeration

Both Iterator and Enumeration provide ways to traverse or navigate through entire collection in java.

  • Enumeration is older and its there from JDK1.0 while iterator was introduced later. 
  • The functionality of Enumeration interface is duplicated by the Iterator interface.
  • Only major difference between Enumeration and iterator is Iterator has a remove() method while Enumeration doesn't. Enumeration acts as Read-only interface, because it has the methods only to traverse and fetch the objects, where as by using Iterator we can manipulate the objects like adding and removing the objects from collectione.g. Arraylist.
  • Also Iterator is more secure and safe as compared to Enumeration because it does not allow other thread to modify the collection object while some thread is iterating over it and throws ConcurrentModificationException. 
  • In Summary both Enumeration and Iterator will give successive elements, but Iterator is new and improved version where method names are shorter, and has new method called remove.

Saturday, November 10, 2012

Difference between HashMap and HashTable

The Following are the differences between HashMaps and HashTables

  • HashMap is unsynchronized and Hashtable is synchronized.Java 5 introduces ConcurrentHashMap which is an alternative of Hashtable and provides better scalability than Hashtable in Java.
  • HashMap allows null values as key and value whereas Hashtable doesnt allow. 
  • HashMap does not guarantee that the order of the map will remain constant over time. 
  • HashMaps have better performance for non threaded applications.
  • Another significant difference between HashMap vs Hashtable is that Iterator in the HashMap is a fail-fast iterator while the enumerator for the Hashtable is not and throw ConcurrentModificationException if any other Thread modifies the map structurally by adding or removing any element except Iterator's own remove() method. But this is not a guaranteed behavior and will be done by JVM on best effort.


Friday, November 9, 2012

Difference Between Abstract Classes and Interfaces

Abstract Classes Vs Interfaces



  • Abstract Classes can have concrete methods while Interfaces can have only abstract methods.
  • Abstract Classes can have fields and constants while Interfaces can have only constants(public static).
  • Abstract Classes can have Private and Protected members while Interfaces can have only Public.
  • A Class can extend from only one Abstract Class but can implement any number of Interfaces
  • Abstract Classes can provide a default implementation while Interfaces can only provide the signature
  • Abstract Classes are used for defining the core functionality of a class while Interfaces are used for defining the peripheral functions of a class.
  • Abstract Classes are faster than Interfaces as Interfaces require time to find the actual method in the corresponding class.
  • Adding/Modifying an Interface will involve searching every implementation and modifying it while this can be avoided for Abstract Classes by providing a default behavior.

Sunday, November 4, 2012

Difference Between Function Overloading and Function Overriding

Function Overloading and Function Overriding are also known as Static and Dynamic Polymorphism

  • Method overriding means having a different implementation of the same method in the inherited class.
  • Method overloading means having two or more methods with the same name but different signatures.

  • Overridden methods would have the same signature, but different implementation. 
  • Overloaded methods have different signature but the same name.

  • One of the overridden methods would exist in the base class and another in the derived class. These cannot exist in the same class.
  • Overloaded methods can exist in base class and derived class or in the same class.

  • Overriding is resolved at runtime.(Dynamic Polymorphism)
  • Overloading is resolved at compile time.(Static Polymorphism)

  • You can not override static, final and private methods
  • You can overload static, final or private methods

  • An overloaded method can throw any exception thrown / not thrown by the method it overloads. 
  • An overriding method can only throw those checked Exceptions and subclasses of these Exceptions, that are thrown by the method it overrides. The overriding method can throw any Runtime Exception.

  • Access Specifier of Overriding method can be only greater than or equal to the access specifier of the overridden method.
  • Overloading methods can have any access specifiers
Here is an excellent example - http://stackoverflow.com/a/154939/602683

Saturday, November 3, 2012

What Are The Differences Between Java References and C Pointers

While the Java specification does not mandate that Java References be implemented using Pointers, it is usually the case. You may have additional layers to help in Garbage Collection but in the End Java References are implemented using C Style Pointers.

However there are some important differences


  • No Pointer Arithmetic
        In C you can increment a Pointer and its valid , such operations are not allowed on Java References.
  • Auto Derefencing
        In C you need to deference the Pointer using '*' to get the value. This happens automatically in Java References.
  • References are Strongly Typed
        In C you can cast a int *  pointer with char * and use it . However Java References can only be of the allowed type. You can cast an object as String only if it is actually of type String.

The differences make C Pointers more powerful but also very dangerous. 

Friday, November 2, 2012

What Are The 4 Major Principles Of Object Oriented Programming

4 Major Principles Of Object Oriented Programming


Four principles of object oriented programming:
  • Abstraction
  • Encapsulation
  • Inheritance
  • Polymorphism
Abstraction :

“An abstraction denotes the essential characteristics of an object that distinguish it from all other kinds of object and thus provide crisply defined conceptual boundaries, relative to the perspective of the viewer.” — G. Booch, Object-Oriented Design With Applications

Abstraction is a way to generalize an object and write code for the generalized object.
Abstraction in Java is implemented using abstract class and interface.

Encapsulation :

Encapsulation is the ability to hide data from users and provide only the necessary details.
Encapsulation in Java is done by making the data member Private.

Inheritance :

A technique to create specialized classes from a general class.
Inheritance in Java is achieved using the keyword extends for classes and implements for interfaces.

Polymorphism :

Refers to an object’s ability to behave differently depending on the context.
Method Overloading is an example of Polymorphism in Java.