Thursday, December 24, 2009

Short Notes on OOPS in java

Short Notes on OOPS:

Abstraction:

It denotes the essential characterstics of an object which differentiate from other object and thus provide crisply defined conceptual boundaries relative to the perspective viewer.

Encapsulation:

It is the mechanism that binds together the code and the data it manipulates and keeps both safe from outside misuse and interference.

Inheritance:

One object acquired the properties of another Object.

Polymorphism:

It is an ability of an object to take on many forms.

Ex: Compile time polymorphism – method over loading

Run time polymorphism – method overriding


Wednesday, December 23, 2009

Interview questions(like diff bettween two) in java


S.No

Abstract

Interface

1

Single inheritance

Multiple inheritance

2

Non abstarct mtd also

Only abstarct mtds

3

Non-static and non-final variables also.

Variables must be static and final(implicitly)

4

Non public members also

Only public mbrs

5

Using extends keyword

Using implements keyword

6

It can invoke if main exists

Pure abtract

7

Faster

Flexible



S.No

Constructor

Method

1

Member function of class.

Ordinary member function.

2

Same as class name.

Own name

3

Invoke using new operator

Invoke using dot operator.

4

No-return type.

It have.



S.No

ArrayList

Vector

1

Its a growable array.

Synchronized ArrayList.

2

Not thread safe

Thread safe.




S.No

HashMap

HashTable

1

Un ordered and un sorted map.

Not thread safe.

Synchronized HashMap. Thread safe.

2

It allows one null key and multiple null values

It wont allow no null key/values.



S.No

AWT

SWING

1

Heay Weight Component.

Light Weight Component.

2

Component use native methods.

Use the methods that r written in java.

3

It have their own viewport which sends the output to the screen.

It does not write itself to the screen, but redirect it to the component it builds

4

Platform dependent.

Platform independent.

5

Old

Its advanced AWT.

1.Its provide additional components like

Jtable,JTabbedPane

2. can add icons and tooltips.

3. built on double buffering.

6

Support Event Delegation Model

MVC architecture,

7

Static Look and feel.

Dynamic Look and feel.


S.No

InvokeAndWait

InvokeLater

1

Synchronous.

Asynchronous.

2

It blocks until runnable task is complete.

It posts an actionevent to the event queue and returns immediately.



S.No

Font

FontMetrics

1

It is used to render text data onto the screen

It provides access to attributes of Font Objects, thing such as charwidth, charheight ,ascents and descent of a font Object.




S.No

String

StringBuffer

1

Immutable(cant change content )

Mutable(can change content using append mtd)

2

Not thread safe.

Thread safe.



S.No

Jlist

JcomboBox

1

Its dont have Editor Component.

It have Editor Component.

2

It allows single,single interval and multiple interval selection.

It allow only single selection.



S.No

Encapsulation

Abstraction

1

It is the mechanism that binds together the code and the data it manipulates and keeps both safe from outside information and misuse.

It denotes the essential characterstics of an object which differentiate from other object.

2

Hiding irrevelents details of an object.

Showing essential things of an object.


Collections in java

Collection:

It is an object that groups multiple elements on a single unit. It is used to store,

retrive, manipulate and communicate the data.

List :

It care about index. ordered based on the index . we can specify the index of elements or it stored default by end .

ArrayList:

Its a growable array. Fast Iteration and Random Access.

Vector:

Synchronized ArrayList . Random access.

Linked List:

Like Array list ordered by index position and also doubly linked.

Fast Insertion and Deletion.


SET:

Its care about uniqueness-its does not allow duplicate.

Hashset:

un ordered and un sorted . Efficient implementation of hasCode() . While iterate we can't predict this order.

LinkedHashSet:

ordered Hashset. Doubly linked. While iterate we can get which order we r inserted.

TreeSet:

Sorted set . sorted by natutral order.

Map:

Its care about unique identifiers. U map a unique id to a specific value, where key and value both r objects.

HashMap:

Un ordered and un sorted. One null key and multiple null values allowed.

Hashtable:

Synchronized HashMap. No null key/values allowed.

LinkedHashMap:

Ordered hashmap by insertion.doubly linked.

TreeMap:

Sorted Map.


Thursday, December 10, 2009

JDBC

What is JDBC?

Java Database Connectivity is a technique of connect java front end to back end database and allowing the retrieval and manipulation of data in the database using java.

---------------------------------------------------------------------------------------------
How do you connect to the Database?

The process of using JDBC to connect to the database is as follows:

1. Register the driver:

Class.forName(”driverName”);
for example, sun.jdbc.odbc.JdbcOdbcDriver


2. Making the connection using DriverManager Class’s
getConnection method :

Connection con = DriverManager.getConnection(”url,”myLogin”,myPassword”);
For example url may be jdbc:odbc:dsn_name.

3. Creating the JDBC Statement and Retreiving:

Statement stmt = con.createStatement();


ResultSet rs = stmt.executeQuery(query);

4. Handling SQL Exception

---------------------------------------------------------------------------------------------

What are the drivers and when it will use?

JDBC-ODBC Bridge Driver for Java and Database (Java Application)

JDBC-Net pure java driver for Applet and Database(Applet application)


Native-API partly Java driver for Native class and Database
(for network application)

Native Protocol pure java driver for vendor specfic application

(EJB application)

Serialization in java

What is Serialization?

The serialization is a kind of mechanism that makes a class or a bean
persistence by having its properties or fields and state information saved and
restored to and from storage.

why Serialization ?
The serialization mechanism has been added into the Java language for two reasons:
(1) the JavaBeans mechanism uses serialization.
(2) remote method invocation (RMI) allows you to automatically use objects located at another host in the network just like any local objects.
---------------------------------------------------------------------------------------------

How to make a class or a bean serializable?

By implementing the java.io.Serializable interface.

There is no method in the Serializable interface. The Serializable
interface acts as a marker, telling the object serialization
tools that your class is serializable

or

By implementing the java.io.Externalizable interface .

There are two methods in the Externalizable interface. You have
to implement these two methods in order to make your class
externalizable. These two methods are readExternal() and writeExternal().

---------------------------------------------------------------------------------------------

What is the difference between Serializalble and Externalizable interface?

When you use Serializable interface, your class is serialized automatically
by default. But you can override writeObject() and readObject() two methods to
control more complex object serailization process. When you use Externalizable
interface, you have a complete control over your class’s serialization
process(for example, when writing and reading a specific file format).

Serialization is a Marker interface -Marker Interface is used by java runtime engine (JVM) to identify the class for special processing.
Use serialization when you need to add data to the serialization stream
that is not an object data member.

Externalization allows you to customize how serialization is done. By implementing externalization you are controlling what gets serialized ( and what doesnot ) as versus default serialization where all non-transient attributes get serialized.
For “fat data” classes with a large number of attributes only a few of which needs to persisted, externalization will help you reduce the size of serialized stream and the time taken to serialize the object. But there will be an overhead involved because the runtime has to call your methods to read/write objects.

Performance issue
1. Further more if you are subclassing your externalizable class you will want to invoke your superclass’s implementation. So this causes overhead while you subclass your externalizable class.
2. methods in externalizable interface are public. So any malicious program can invoke which results into lossing the prior serialized state.

---------------------------------------------------------------------------------------------

Note : If you don’t want some field not to be serialized, you can mark
that field transient or static.
---------------------------------------------------------------------------------------------

Example for Serializable :


import java.io.Serializable;

public class PersonDetails implements Serializable {
private String name;
private int age;

public PersonDetails(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
}

import java.io.*;
class SerializeandDeserialize
{
// create a Persondetails and serialize it to a file persondetails.txt
private static void serializeObject()
{
PersonDetails amir = new PersonDetails();

try
{
FileOutputStream fos = new FileOutputStream("persondetails.txt");
ObjectOutputStream os = new ObjectOutputStream(fos);
os.writeObject(amir);
os.close();
fos.close();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
}

// For Deserialized object from a file persondetails.txt
private static void deserializeObject()
{
PersonDetails amir = null;
try
{
FileInputStream fis = new FileInputStream("persondetails.txt");
ObjectInputStream is = new ObjectInputStream(fis);
amir = (PersonDetails)is.readObject();
is.close();
fis.close();
}
catch (ClassNotFoundException e)
{
e.printStackTrace();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}

System.out.println("Name : "+amir.getName());
System.out.println("Age : "+amir.getAge());

}

public static void main(String arg[])
{
serializeObject();
deserializeObject();
}
}


Example for Externizable:
import java.io.*;

public class PersonDetails_Ext implements Externalizable {

private String name;
private int age;

public PersonDetails_Ext(String name, int age) {
this.name = name;
this.age = age;
}
public PersonDetails_Ext() {
}

public String getName() {
return name;
}
public int getAge() {
return age;
}

public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}

public void writeExternal(ObjectOutput out)
throws IOException
{
out.writeObject(this.name);
out.writeInt(this.age);
}

public void readExternal(ObjectInput in)
throws IOException, ClassNotFoundException
{
this.name = (String)in.readObject();
this.age = in.readInt();
}
}

import java.io.*;
class SerializeandDeserialize1
{
// create a PersonDetails_Ext and serialize it to a file persondetails.txt
private static void serializeObject()
{
PersonDetails_Ext amir = new PersonDetails_Ext("Amirtharaj", 27);

try
{
FileOutputStream fos = new FileOutputStream("persondetails.txt");
ObjectOutputStream os = new ObjectOutputStream(fos);
os.writeObject(amir);
os.close();
fos.close();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
}

// For Deserialized object from a file persondetails.txt
private static void deserializeObject()
{
PersonDetails_Ext amir = null;
try
{
FileInputStream fis = new FileInputStream("persondetails.txt");
ObjectInputStream is = new ObjectInputStream(fis);
amir = (PersonDetails_Ext)is.readObject();
is.close();
fis.close();
}
catch (ClassNotFoundException e)
{
e.printStackTrace();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}

System.out.println("Name : "+amir.getName());
System.out.println("Age : "+amir.getAge());

}

public static void main(String arg[])
{
serializeObject();
deserializeObject();
}
}