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.

SCJP


This is the day i am start posting the topics on scjp. Basically i am planning to prepare for the SCJP (Sun Certified Java programmer), So that I am happy to share my preparation, I am happy to discuss each and every important point which i feel, I want to figure out the success tips to help others.

To day i am happy that  strongly decided to complete the exam in three months. Lets see what will happen.

I hope I will get your support to success.

Lets wish me once for the success.

Differences 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;

 

Other Useful Links:

Difference between Abstract Class and Interface

What are the differences between EAR, JAR and WAR file?

Differences between callable statements, prepare statements, createstatements

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.

Step 1: 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 following 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 common.

C:\>java -jar test.jar

o/p:

Hello world

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.

ListIterator with an example


ListIterator allows the programmer to traverse the list in either direction, modify the list during iteration, and obtain the iterator’s current position in the list. A ListIterator has no current element; its cursor position always lies between the element that would be returned by a call to previous() and the element that would be returned by a call to next().

Example:
 public class sample {
 
  public static void main(String[] args) {
    //create an object of ArrayList
    ArrayList aList = new ArrayList();
 
    //Add elements to ArrayList object
    aList.add("1");
    aList.add("2");
    aList.add("3");
    aList.add("4");
    aList.add("5");
 
    //Get an object of ListIterator using listIterator() method
    ListIterator listIterator = aList.listIterator();
 
      
    System.out.println(" forward direction using ListIterator");
    while(listIterator.hasNext())
      System.out.println(listIterator.next());
 
   
    System.out.println("reverse direction using ListIterator");
    while(listIterator.hasPrevious())
      System.out.println(listIterator.previous());
   }
}
 
try the above code and get back to me :)

Iterator with an example


Using Iterator we can iterate only in forward direction and you cannot add elements while iterating and Here cursor always points to specific index.

Example:
 public class Example {
 
  public static void main(String[] args) {

    ArrayList aList = new ArrayList();
 
    aList.add("1");
    aList.add("2");
    aList.add("3");
    aList.add("4");
    aList.add("5");
 
    Iterator itr = aList.iterator();
 
     while(itr.hasNext())
      System.out.println(itr.next());
 
  }
}

When to go for Sortedset


When you want to sort the data we have to go with sortedset by default the sorting order is ascending.
You can customize the sorting order by Implementing comparator interface.
You can use treeset for sorting and for this you need to use the constructor which accepts comparator as argument.
Example:

public class Example {
public static void main(String[] args) {
SortedSet ss=new TreeSet();

ss.add(“a”);
ss.add(“e”);
ss.add(“g”);
ss.add(“b”);
ss.add(“c”);

Iterator it=ss.iterator();

while(it.hasNext())
{
String value=(String)it.next();
System.out.println(“Value :”+value);
}
}
}

HashSet with Example


If  our 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();
    }
}

Can we declare final method in abstract class?


If a method in a class is defined as final, then we can’t provide the re-implementation for that final method in it’s derived classes i.e overriding is not possible for that method.

An abstract class contains one or more abstract method.

  • We can have concrete(non-abstract) methods declared as final as it contains both definition and declaration in abstract itself.
  • However, we cannot define abstract methods as final, since the class inheriting our abstract class cannot define/re-implement them.

Example:

abstract class Shape
{
    protected String name;

    abstract void draw();
    abstract void paint();

    final String getNameOfShpae() {
        retrun "Name of the Shape is"+name;
    }
}