Friday, July 2, 2010

The basic Properties of Interface

1. Interface must be declared with the key word ‘interface’.
2. All interface methods are implicitly public and abstract. In another words you dont need to atually type the public or abstract modifiers in the metod declaration, but method is still allways public and abstract.
3. All variables defined in an interface is public, static, and final. In another words, interfaces can declare only constants , not instance variables.
4. Interface methods must not be static.
5. Because interface methods are abstract, they cannot be marked final, strictfp, or native.
6. An interfaces can extend one or more other interfaces.
7. An interface cannot implement another interface or class.
8. interface types can be used polymorphically.

Difference between Abstract Class and Interface

1. Abstract class has the constructor, but interface doesn’t.

2. Abstract classes can have implementations for some of its members (Methods), but the interface can’t have implementation for any of its members.

3. Abstract classes should have subclasses else that will be useless..

4. Interfaces must have implementations by other classes else that will be useless

5. Only an interface can extend another interface, but any class can extend an abstract class..

6. All variable in interfaces are final by default

7. Interfaces provide a form of multiple inheritance. A class can extend only one other class.

8. Interfaces are limited to public methods and constants with no implementation. Abstract classes can have a partial implementation, protected parts, static methods, etc.

9. A Class may implement several interfaces. But in case of abstract class, a class may extend only one abstract class.

10. Interfaces are slow as it requires extra indirection to to find corresponding method in in the actual class. Abstract classes are fast.

11. Accessibility modifier(Public/Private/internal) is allowed for abstract class. Interface doesn’t allow accessibility modifier

12. An abstract class may contain complete or incomplete methods. Interfaces can contain only the signature of a method but no body. Thus an abstract class can implement methods but an interface can not implement methods.

13. An abstract class can contain fields, constructors, or destructors and implement properties. An interface can not contain fields, constructors, or destructors and it has only the property’s signature but no implementation.

14. Various access modifiers such as abstract, protected, internal, public, virtual, etc. are useful in abstract Classes but not in interfaces.

15. Abstract scope is upto derived class.

16. Interface scope is upto any level of its inheritance chain.

Friday, June 4, 2010

What happens to all my social networking information when I die?


What happens to all my social networking information when I die?

U might have faced this question in the past and you may get this question in the future, but the answer might be like account may delete or data may keep as it is with out modifications.

But some times the data you have posted may useful to others or it may be inspiration to the society. Keeping the data for your memory, keeping the data for the society is quite important.

In the fallowing link, the web master is giving more useful information on this question

He has given more information on how to close the Face book account after death, what are the conditions applied, or how to keep the information as memorable.

In the present situation millions of people are using the community sites to share the their details and valuable information. Keeping the useful data in the web is also important.


Thursday, June 3, 2010

Difference between Abstract Class and Interface:

1.  Abstract class has the constructor, but interface doesn’t.
2.  Abstract classes can have implementations for some of its members (Methods), but the interface can’t have implementation for any of its members.
3.  Abstract classes should have subclasses else that will be useless..
4. Interfaces must have implementations by other classes else that will be useless
5. Only an interface can extend another interface, but any class can extend an abstract class..
6.  All variable in interfaces are final by default
7. Interfaces provide a form of multiple inheritance. A class can extend only one other class.
8. Interfaces are limited to public methods and constants with no implementation. Abstract classes can have a partial implementation, protected parts, static methods, etc.
9.  A Class may implement several interfaces. But in case of abstract class, a class may extend only one abstract class.
10. Interfaces are slow as it requires extra indirection to to find corresponding method in in the actual class. Abstract classes are fast.
11. Accessibility modifier(Public/Private/internal) is allowed for abstract class. Interface doesn’t allow accessibility modifier
12.  An abstract class may contain complete or incomplete methods. Interfaces can contain only the signature of a method but no body. Thus an abstract class can implement methods but an interface can not implement methods.
13.  An abstract class can contain fields, constructors, or destructors and implement properties. An interface can not contain fields, constructors, or destructors and it has only the property’s signature but no implementation.
14. Various access modifiers such as abstract, protected, internal, public, virtual, etc. are useful in abstract Classes but not in interfaces.
15.  Abstract scope is upto derived class.
16.  Interface scope is upto any level of its inheritance chain.

Saturday, May 29, 2010

About Interface


1. Interface must be declared with the key word 'interface'.
2. All interface methods are implicitly public and abstract. In another words you don't need to actually type the public or abstract modifiers in the method declaration, but method is still always public and abstract.
3. All variables defined in an interface is public, static, and final. In another words, interfaces can declare only constants , not instance variables.
4. Interface methods must not be static.
5. Because interface methods are abstract, they cannot be marked final, strictfp, or native.
6. An interfaces can extend one or more other interfaces.
7. An interface cannot implement another interface or class.
8. interface types can be used polymorphically.

Monday, May 17, 2010

Difference between object and instance?

There is a lot of discussions on this topic over the internet. Some of the people says both are same and some of other says both are different. Here I am planning to share my overall view on this topic. As per my knowledge both are looks same but different. Here are the some useful points:

  • Instance is Logical but object is Physical means occupies some memory.
  • We can create an instance for abstract class as well as for interface, but we cannot create an object for those.
  • Object is instance of class and instance means representative of class i.e object.
  • Instance refers to Reference of an object.
  • Object is actually pointing to memory address of that instance.
  • You can't pass instance over the layers but you can pass the object over the layers
  • You can't store an instance but you can store an object
  • A single object can have more than one instance.
  • Instance will have the both class definition and the object definition where as in object it will have only the object definition.
Syntax of Object:
classname var=new classname();
But for instance creation it returns only a pointer refering to an object, syntax is : classname varname;

Thursday, May 13, 2010

Jar file Vs Executable Jar file

Jar file is the combination of compiled java classes.
Executable jar file is also be combination of compiled java classes with Main class.
Normal jar file can be created as
C:\> jar -cvf TestNew.jar . 
But while creating executable jar file we have to specify the Main-Class in a manifest file. Following are the steps to fallow while creating the executable jar file. Here its explained with a simple class in a test package.
Step1: create a java class TestNew under package test
package test;
public class TestNew{
public static void main(String a[]){
System.out.println("Hello world");
}
}
Step 2: compile the class with fallowing command
C:\>javac TestNew.java -d . 
Note: Once you run the TestNew class you have to get “HelloWorld” in the console. To run use fallowing command
C:\>java test.TestNew
Step 3: Create a “mainClass” file in the directory where your test folder is located . This file contains a single line specifying where the main Class is to be found in the jar file. Note that I use the package specification. Here is the single line:
Main-Class: test.TestNew
Note: specify the single line only don't specify anything
Step 4: create the jar using fallowing command
C:\>jar cmf mainClass test.jar test
Where “c” represents the create that you are going to “create”, "m" specifies the manifest file mainClass (which adds information to the jar file on where the main class will be found) and “f ” refers to “file”.
Note: The file has to be created in your folder with the name test.jar

To view the content of the jar file you can use the fallowing command
C:\>jar tf test.jar
It displays as:
META-INF/
META-INF/MANIFEST.MF
test/
test/TestNew.class
To execute the jar file you can use the following commond
C:\>java -jar test.jar
o/p:
Hello world

Friday, May 7, 2010

Integrating Quartz Job Server with JBoss

I am getting the fallowing error while integrating Quartz job server in the JBoss
javax.naming.CommunicationException: Receive timed out
[Root exception is java.net.SocketTimeoutException: Receive timed out]

The steps i fallowed to configure the Quarts

1. I removed the quartz.jar from the {JBoss_Home}/deploy/lib and Quartz-ra.rar from {JBoss_Home}/deploy folder.

2. Added the quartz 1.6.5.jar and quartz-jboss-1.6.5.jar into {JBoss_Home}/deploy/lib .

3. Added the quartz-service.xml with fallowing details
<?xml version="1.0" encoding="UTF-8"?>
<server>
 <mbean code="org.quartz.ee.jmx.jboss.QuartzService" name="user:service=QuartzService,name=QuartzService">  
   <attribute name="JndiName">Quartz</attribute>
   <depends>jboss.jca:service=DataSourceBinding,name=QuartzDS</depends>
   <attribute name="Properties">      
     org.quartz.scheduler.instanceName = DefaultQuartzScheduler
     org.quartz.scheduler.rmi.export = false
     org.quartz.scheduler.rmi.proxy = false
     org.quartz.scheduler.xaTransacted = false
     org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
     org.quartz.threadPool.threadCount = 5
     org.quartz.threadPool.threadPriority = 4
     org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore
   </attribute>
 
 </mbean>
</server>

4. I have written the Servlet to schedule the job
package com.radiant.cisms.help.servlets;

import java.io.IOException;
import java.util.Date;
import javax.naming.InitialContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.quartz.CronTrigger;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.SimpleTrigger;
import org.quartz.impl.StdScheduler;
import org.quartz.impl.StdSchedulerFactory;


/**
* Servlet implementation class for Servlet: JobSeduleServlet
*
*/
public class JobSeduleServlet extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
  static final long serialVersionUID = 1L;
 
   /* (non-Java-doc)
    * @see javax.servlet.http.HttpServlet#HttpServlet()
    */
   public JobSeduleServlet() throws SchedulerException {
       super();
       sedulingUsingJboss();
   }     
 

   protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       System.out.println("under doGet method.................");
       sedulingUsingJboss();
   }    
 
 
   protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       System.out.println("under doPost method.................");
       sedulingUsingJboss();
   } 
 

   public void sedulingUsingJboss(){
       System.out.println("under...sedulingUsingJboss................. ");
        try {
              InitialContext ctx = new InitialContext();
              System.out.println("Intial Context........."+ctx);
              System.out.println("..................."+ctx.lookup("Quartz"));
              StdScheduler scheduler = (StdScheduler) ctx.lookup("Quartz");
            
             JobDetail jd=new JobDetail("myjob",scheduler.DEFAULT_GROUP,TestJob.class);
              CronTrigger ct = new CronTrigger("cronTrigger","group2","0 0/5 * * * ?");
              scheduler.scheduleJob(jd,ct);
            
              } catch (Exception exc) {  
                 exc.printStackTrace();
              }
   }
}

5. Above servlet has been configured in web.xml as fallowes
<servlet>
       <servlet-name>JobSeduleServlet</servlet-name>
       <servlet-class>com.radiant.cisms.help.servlets.JobSeduleServlet</servlet-class>
       <load-on-startup>1</load-on-startup>
   </servlet>

<servlet-mapping>
       <servlet-name>JobSeduleServlet</servlet-name>
       <url-pattern>/servlet/JobSeduleServlet</url-pattern>
   </servlet-mapping>
According to console generated by the server Quartz service is loading successfully.
But while calling the JobScheduleServlet it is throwing the exception as fallows
javax.naming.CommunicationException: Receive timed out [Root exception is java.net.SocketTimeoutException: Receive timed out]


Sunday, April 25, 2010

What is the difference between EAR, JAR and WAR file?


In J2EE, application modules are packaged as EAR, JAR and WAR based on their functionality.
Each type of file (.jar, .war, .ear) is processed uniquely by application servers, servlet containers, EJB containers, etc


JAR:
Jar file (file with a .jar extension) is the normal Java Application archive and intended to hold generic libraries of Java classes, resources, auxiliary files, etc
This can be added in classpath for compilation and to run java program. Generally in web applications we keep these files in lib directory of the application.

Ex: ojdbc14.jar – This contains all the classes to connect the oracle database
Servlet-api.jar – contains servlet related classes

WAR:
Web modules which contains Servlet class files,JSP Files,supporting files, GIF and HTML files are packaged as JAR file with .war( web archive) extension
War files (files with a .war extension) are intended to contain complete Web applications. In this context, a Web application is defined as a single group of files, classes, resources, .jar files that can be packaged and accessed as one servlet context.

EAR:
All above files(.jar and .war) are packaged as JAR file with .ear ( enterprise archive) extension and deployed into Application Server.
Ear files (files with a .ear extension) are intended to contain complete enterprise applications. In this context, an enterprise application is defined as a collection of .jar files, resources, classes, and multiple Web application. 

Wednesday, April 21, 2010

HashSet with Example


If your requirement is frequent insert then we go with HashSet.

For efficiency objects added to a HashSet need to implement the hashCode() method in a manner that properly distributes the hash codes. While most system classes override the default hashCode() implementation of the Object.

It is generally faster to add elements to the HasSet then convert the collection to a TreeeSet for sorted traversal.

To optimize HashSet space usage , you can tune initial capacity and load factor.

Example:

public class HashSetExample {

    public static void main(String[] args) {

        HashSet<String> hs=new HashSet<String>();

        // duplicate element is not permitted
        hs.add("b");
        hs.add("a");
        hs.add("c");
        hs.add("d");
        hs.add("d");

        Iterator it=hs.iterator();

        while(it.hasNext())
        {
            String value =(String)it.next();

            System.out.println("Value :"+value);
        }

        System.out.println("Size :"+hs.size());

        hs.remove("d");

        hs.clear();
    }
}