Wednesday, October 23, 2013

What happens when we write weaker access privileges for a overridden method compare to super class method?



This is an Interview question, This question raises while asking questions in corejava.

- While overriding any method, we must not write weaker privileges than the parent. If we write, we will get compilation error.


Take an example

A.java
public class A{
      public void go(){}
}

B.java
public class B extends A{
       public void go(){}
}

If we compile the above code, we don't get any compilation error and compiles successful.


Suppose change the access specifier of B's go() method to protected or private or default.

B.java
public class B extends A{
     protected void go(){}  --------->   compilation error
}


we will get compilation error stating that ...

error:
go() in B cannot override go() in A; attempting to assign weaker access privileges; was public
protected void go(){}
              ^
1 error

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

And one more thing we have to remember that we also cannot override private method...

example:

public class A{

        private void go(){
         }
}
public class B extends A{

        private void go(){ //It compiles successfully, but Its not overridding..
         }
}

How:
- If we write annotation in class B's go() method as @Override, Then the compiler will give us error,
- So, we cant override private methods...

Monday, October 21, 2013

The most important difference between Comparable and Comparator interfaces...


Imp Difference:

- Comparable interface is available in java.lang package, and it is used for natural ordering of the objects.

- Comparator interface is available in java.util package, and it is used for custom ordering of the objects (or) custom comparison between objects.


read Comparable tips.

Comparable - some tips...

Tips :
 
                    -  The natural ordering for a class C is said to be consistent with equals if and only if e1.compareTo(e2) == 0 has the same boolean value as e1.equals(e2) for every e1 and e2 of class C.


*******  Since null is not an instance of any class, and e.compateTo(null) should throw NullPointerException even though e.equals(null) returns false.

******* This interface is a member of the Java Collections Framework.

 - The implementor must ensure sign(x.compareTo(y)) == -sign(y.compareTo(x)) for all x and y. 
  ( This implies that x.compareTo(y) must throw an exception iff y.compareTo(x) throws an exception.)

- It is strongly recommended, but not strictly required that (x.compareTo(y)==0) == (x.equals(y))

suppose returning value is 'v'.

value           meaning
v<0 dd="" is="" less="" nbsp="" obj="" specified="" than="" the="" this="">
0                 this obj is equal to specified obj,
v>0             this obj is greater than the specified obj


source: http://docs.oracle.com/javase/6/docs/api/java/lang/Comparable.html

Insurance domain knowledge links to read and learn..

Cross compiling in java .... :P


Cross compilation in java:



Basics: 

Here I have two JDK locations in my system.

1) Default Jdk (1.6) is located in $JAVA_HOME (/usr/lib/jdk1.6.0_45)
2) Lower version Jdk (1.5) is located in ~/jdk_5/jdk1.5.0_22/ 

Now take a sample class 

public class Test{

            public static void main(String nag[]){
                     
                     System.out.println("Hello Nagarjuna");

            }
}//end class

now I am compiling using jdk 1.6 like

                $ $JAVA_HOME/bin/javac Test.java
It will be compile and generate Test.class


And now try to run this Test.class file using jdk 1.6

                $ $JAVA_HOME/bin/java Test
                o/p :  Hello Nagarjuna


lly, Now try to run with  jdk 1.5

           $ ~/jdk_5/jdk1.5.0_22/bin/java Test
         o/p:  
       Exception in thread "main" java.lang.UnsupportedClassVersionError: Bad version number in .class file
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:621)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:260)
at java.net.URLClassLoader.access$100(URLClassLoader.java:56)
at java.net.URLClassLoader$1.run(URLClassLoader.java:195)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:268)
at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)


It is because of higher version compilation and lower version execution.

           But we can compile the above Test.java using jdk 1.5 and can run the generated Test.class file in both jdk1.6 and jdk1.5 . Because lower version compilation can support higher (Latest) version execution.

Now, let us try cross compilation in jdk 1.6


Cross compilation:  

Here we use javac arguments  ( only 'target')

Use 'taget' argument to javac while compiling with jdk1.6.



compilation: 

$ $JAVA_HOME/bin/javac -target 1.5 Test.java

And now, Let us try to run the generated Test.class file in both jdk 1.6 and jdk 1.5

first in jdk 1.6 -
          $ $JAVA_HOME/bin/java Test
          o/p: Hello Nagarjuna

 in jdk 1.5 -
           $  ~/jdk_5/jdk1.5.0_22/bin/java Test
           o/p: Hello Nagarjuna


So, we can do cross compilation using any higher version JDK. 

Try on your own          


Sunday, October 20, 2013

Gererating SerialVersionUID for a serializable class...

Hi All,

In this post I am going to show you how to generate serialVersionUID for a serializable class.(java.io.Serializable)


Purpose of  the serialVersionUID variable : 

Any serializable class will need a unique class identifier called serialVersionUID.

              This long value is used to allow the JVM to know if a version of the class has changed. If you neglect to include it in your class, then one will be generated on the fly. You don’t want that.


Generating serialVersionUId for our custom class : 

  Take an example class A which implements Serializable inteface. 

    public class A implements Serializable{

    } //end class


If we want to generate unique serial version uid, then we have a command in JDK's bin folder with the name serialver.


use below command to generate UID.

$ serialver A
A:    static final long serialVersionUID = -5362330504532103641L;

we can type the above command may times, we will get the same UID all time.


Note: For any library related serializable class  like hibernate or spring related serializable class, while generating UID, all the instance related classes must be in class path. So don't forget to put all compilation related classes in classpath.