Showing posts with label OOP. Show all posts
Showing posts with label OOP. Show all posts

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.


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.