Wednesday, August 16, 2006

What is serialization? How would you exclude a field of a class from serialization or what is a transient variable?What is the common use?

Serialization is a process of reading or writing an object. It is a process of saving an object’s state to a sequence of bytes, as well as a process of rebuilding those bytes back into a live object at some future time. An object is marked serializable by implementing the java.io.Serializable interface, which is only a marker interface -- it simply allows the serialization mechanism to verify that the class can be persisted, typically to a file.


Transient variables cannot be serialized. The fields marked transient in a serializable object will not be transmitted in the byte stream. An example would be a file handle or a database connection. Such objects are only meaningful locally. So they should be marked as transient in a serializable class.


Serialization can adversely affect performance since it:
1. Depends on reflection.
2. Has an incredibly verbose data format.
3. Is very easy to send surplus data.


When to use serialization? Do not use serialization if you do not have to. A common use of serialization is to use it to send an object over the network or if the state of an object needs to be persisted to a flat file or a database. Deep cloning or copy can be achieved through serialization. This may be fast to code but will have performance implications


The objects stored in an HTTP session should be serializable to support in-memory replication of sessions to achieve scalability. Objects are passed in RMI (Remote Method Invocation)
across network using serialization.

What do you mean by polymorphism, inheritance, encapsulation, and dynamic binding?

Polymorphism : means the ability of a single variable of a given type to be used to reference objects of different types, and automatically call the method that is specific to the type of object the variable references. In a nutshell, polymorphism is a bottom-up method call. The benefit of polymorphism is that it is very easy to add new classes of derived objects without breaking the calling code that uses the polymorphic classes or interfaces. When you send a message to an object even though you don’t know what specific type it is, and the right thing happens, that’s called polymorphism.

The process used by objectoriented programming languages to implement polymorphism is called dynamic binding.


Inheritance : is the inclusion of behaviour (i.e. methods) and state (i.e. variables) of a base class in a derived class so that they are accessible in that derived class. The key benefit of Inheritance is that it provides the formal mechanism for code reuse. Any shared piece of business logic can be moved from the derived class into the base class as part of refactoring process to improve maintainability of your code by avoiding code duplication. The existing class is called the superclass and the derived class is called the subclass. Inheritance can also be defined as the process whereby one object acquires characteristics from one or more other objects the same way children acquire characteristics from their parents.


Encapsulation : refers to keeping all the related members (variables and methods) together in an object. Specifying members as private can hide the variables and methods. Objects should hide their inner workings from the outside view. Good encapsulation improves code modularity by preventing objects interacting with each other in an unexpected way, which in turn makes future development and refactoring efforts easy.

Thursday, August 10, 2006

What are some of the best practices relating to Java collection?

1. Use ArrayLists, HashMap etc as opposed to Vector, Hashtable etc, where possible to avoid
any synchronization overhead. Even better is to use just arrays where possible. If multiple
threads concurrently access a collection and at least one of the threads either adds or deletes
an entry into the collection, then the collection must be externally synchronized. This is
achieved by:

Map myMap = Collections.synchronizedMap (myMap);
List myList = Collections.synchronizedList (myList);

2. Set the initial capacity of a collection appropriately (e.g. ArrayList, HashMap etc). This is because collection classes like ArrayList, HashMap etc must grow periodically to accommodate new elements. But if you have a very large array, and you know the size in advance then you can speed things up by setting the initial size appropriately.

3. Program in terms of interface not implementation

4. Avoid storing unrelated or different types of objects into same collection


Monday, August 07, 2006

Where and how can you use a private constructor?

Private constructor is used if you do not want other classes to instantiate the object. The instantiation is done by a public static method within the same class.
1. Used in the singleton pattern. (Refer Q45 in Java section).
2. Used in the factory method pattern (Refer Q46 in Java section).
3. Used in utility classes e.g. StringUtils etc.

Sunday, August 06, 2006

What are the advantages of Object Oriented Programming Languages ?

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.

Saturday, August 05, 2006

Why there are some interfaces with no defined methods (i.e. marker interfaces) in Java?

The interfaces with no defined methods act like markers. They just tell the compiler that the objects of the classes implementing the interfaces with no defined methods need to be treated differently. Example Serializable, Cloneable etc

Friday, August 04, 2006

What is the difference between C++ and Java?

1. Java does not support pointers. Pointers are inherently tricky to use and troublesome.

2. Java does not support multiple inheritances because it causes more problems than it solves. Instead Java supports multiple interface inheritance, which allows an object to inherit many method signatures from different interfaces with the condition that the inheriting object must implement those inherited methods. The multiple interface inheritance also allows an object to behave polymorphically on those methods.

3. Java does not support destructors but rather adds a finalize() method. Finalize methods are invoked by the garbage collector prior to reclaiming the memory occupied by the object, which has the finalize() method. This means you do not know when the objects are going to be inalized. Avoid using finalize() method to release non-memory resources like file handles, sockets, database connections etc because Java has only a finite number of these resources and you do not know when the garbage collection is going to kick in to release these resources through the finalize() method.


4. Java does not include structures or unions because the traditional data structures are implemented as an object oriented framework.

5. All the code in Java program is encapsulated within classes therefore Java does not have global variables or
functions.


6. C++ requires explicit memory management, while Java includes automatic garbage collection.