Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

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 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.