Search This Blog

Loading...

Saturday, June 27, 2009

Java Annotations - @Override Example

Java Annotations - Override

Annotaion @Override indicates that the method is going to be overridden by the method of the super class. If we used @Override and does not override by the super class gives compilation error. The following example demonustrate @Override.

Super Class:

package examples;

/**
*
* @author raghava
*/
public class SuperClass {
int add(int a,int b){
return a + b;
}
}


Sub Class
/**
* @author raghava
*/
public class SubClass extends SuperClass{

@Override
public int add(int a,int b){
return a + b;
}

}


Here @Override indicates the add method of the sub class is going to override by the super class.

If SuperClass does not contain the override method then the compiler gives the below error.

"method does not override or implement a method from a super type"

Wednesday, June 24, 2009

Overriding - Access Specifier can not be more restrictive

When we are overiding methods, we need to follow some rules. While overriding the access specifier can not be more restrictive.

The following are the allowed access specifiers when we are overriding.

Super class - Sub Class
---------------------------------------------------------
protected - protected,default and public
default - default and public
public - public

Saturday, June 20, 2009

What is the difference between doGet() and doPost() methods?



  • In doGet (), the parameters are appended to the URL and sent along with header information. In doPost() parameters are sent in separate line in the body
  • In doGet (), parameters are not encrypted but in doPost () parameters are not encrypted.
  • doGet() is faster compared to doPost()
  • doGet should be idempotent. i.e. doGet () should be able to be repeated safely many times. doPost method does not need to be idempotent. Operations requested through POST can have side effects for which the user can be held accountable.
  • The amount of data which we send is limited in doGet () where as in doPost () it is not limited. In doGet (), maximum size of data that can be sent is 240 bytes. In doPost () there is no maximum size of data


Friday, June 19, 2009

NavigableSet - pollFirst and pollLast example

pollFirst(), pollLast() methods of NavigableSet can be used to remove first and last element of the Set respectively. The below program explains the pollFirst() and pollLast()


package examples;

import java.util.NavigableSet;
import java.util.TreeSet;

/**
*
* @author Net4JAVA
*/
public class NavigableSetExample {

public static void main(String args[]){
NavigableSet navigableSet = new TreeSet();

navigableSet.add(1);
navigableSet.add(2);

System.out.println("Set : " + navigableSet);

navigableSet.pollFirst();

System.out.println("Set After pollFirst " + navigableSet);

navigableSet.add(3);
navigableSet.add(4);

System.out.println("Set " + navigableSet);

navigableSet.pollLast();

System.out.println("Set after pollLast " +navigableSet);

}

}

output:


Set : [1, 2]
Set After pollFirst [2]
Set [2, 3, 4]
Set after pollLast [2, 3]


Template by - Abdul Munir | Daya Earth Blogger Template