How we declare a synchronized block? What does this represent in the Declaration of sunchronized block?


When two or more threads need to access to a shared resource they need some way to ensure that the resource will be used by only one thread at a time. The process by which this achieved is called synchronization.

The general form of synchronize statement :

Synchronized (object) {
// statement to be synchronized
}
here the object is a reference to the object being synchronized. A synchronized block ensures that a call to a method that is member of object occurs only after the current thread has successfully entered the object’s moniter

What are the differences between interface and abstract class?


The key difference between an interface and the corresponding abstract class is that a class or interface can have unlimited number of immediate super interface but it can only have one super class.
There are two primary axes of “inheritance” an object-oriented language like java. Implementation inheritance is where the sub-class inherits the actual code implementation from the parent. Interface inhyeritance is where the sub-class adheres to the public interface of the parent.
Also java actally mixes the two notions together a bi…. java interfaces are nice and clean. When you implement an interface, you are stipulating that your class adheres to the “contract” of the interface that you spefified. Java class inheritance isn’t so clean when you sub-class in java you are getting both the code inheritance but you are also stipulating that you sub-class adheres to the contract of the interface of the parent class.

Abstrct class in java are just like regular java classes but with the added constraint that you cannot instance then directly. In terms of that added constraint, they are basically classes which don’t actually implement all lof the code specified by their “contract”.

What is the difference between callable statements, prepare statements , createstatements?


  • CallableStatement: This is used to retrieve the stored procedures from the database. ie., these statements are used when two or more SQL statements are retrieved over and over.
  • PreparedStatement: These are pre-compiled statements which are stored in database. We are using these, if the same query is to be executed over and over.
  • CreateStatement: This is used to execute single SQL statement.

Can we declare final method in abstract class?


If a method is defined as final then we can’t provide the reimplementation for that final method in it’s derived classes i.e overriding is not possible for that method.
An abstract class contains one or mole abstract method. If all the methods ar of type abstract then use we can declare that class as abstract or interface. But in interface all the methods must be of type abstract.
We can declare final method in abstract class suppose of it is abstract too, then there is no used to declare like that.
Final abstract int test(String str);
Here we can’t provide the implementation for the test method as it is declared as final. But at the same time you declare it as abstract. [Abstract means it is responsibility of derived class to provide implementation for this] Here we can’t provide the implementation for the test method. Even we descried to provide implementation totally our meaning for declaring it as abstract is spoiled.

Isolation levels & Transaction levels?


The isolation level measures concurrent transaction’s capacity to view data that have been updated, but not yet committed, by another transaction if other transactions were allowed to read data that are as yet uncommitted, those transactions could end up with inconsistent data were the transaction to roll back, or end up waiting unnecessarily were the transaction to commit successfully.
A higher isolation level means less concurrence and a greater likelihood of performance bottleneck, but also a decreased chance of reading inconsistent data. A good rule of thumb is to use the highest isolation level that yields an acceptable performance level. The following are common isolation levels, arranged from lowest to highest.
1) ReadUncommitted: Data that have been updated but not yet committed by a transaction my be read by other transactions
2) Readcommitted: Only data that have been committed by a transaction can be read by other transactions.
3) Repeatable Read: Only data that have been commited by a trasaction can be read by other transactions, and multiple reads will yield the same result as log as the data have been committed.
4) Serializable: This highest possible iosolation level, ensures a transaction’s execlusive read-write access to data, it includes the conditions of ReadCommitted and Repeatable Read and stiplulates that all transactions run serially to achieve maximum data integrity. This yields the slowest performance and least concurrency. The term serializable in this context is absolutely unrelated to the database.

About “Map” data structures in Collections framework?


A map cannot contain duplicate keys; each key can map to at most one value. The Map interface provides three collection views, which allow a map’s contents to be viewed as a set of keys, collection of values, or set of key-value mappings. The order of a map is defined as the order in which the iterators on the map’s collection views return their elements.The TreeMap class, make specific guarantees as to their order; others, like the HashMap class, do not.

Difference between Hashtable & HashMap?


Hash table is a Collection .To successfully store and retrieve objects from a hashtable, the objects used as keys must implement the hashCode method and the equals method.  Hashtable  is Synchronized and permit not null values only.
Hashtable maps keys to values. Any non-null object can be used as a key or as a value.
The HashMap class is roughly equivalent to Hashtable, except that it is unsynchronized and permits nulls.

What is a singleton class?How do you writer it? Tell examples?


A class which must create only one object (ie. One instance) irrespective of any number of object creations based on that class.

Steps to develop single ton class:-

1.Create a Private construcor, so that outside class can not access this constructor. And declare a private static reference of same class

2.Write a public Factory method which creates an object. Assign this object to private static Reference and return the object

Sample Code:-

class SingleTon

{

private static SingleTon st=null;

private SingleTon()

{

System.out.println(“\nIn constructor”);

}

public static SingleTon stfactory()

{

if(st==null) st=new SingleTon();

return st;

}

}

 

 

For complete Example of Singleton design pattern click here