Pages

Wednesday, August 22, 2012

interface-Abstract

Difference Between Interface and Abstract Class

  1. Main difference is methods of a Java interface are implicitly abstract and cannot have implementations. A Java abstract class can have instance methods that implements a default behavior.
  2. Variables declared in a Java interface is by default final. An  abstract class may contain non-final variables.
  3. Members of a Java interface are public by default. A Java abstract class can have the usual flavors of class members like private, protected, etc..
  4. Java interface should be implemented using keyword “implements”; A Java abstract class should be extended using keyword “extends”.
  5. An interface can extend another Java interface only, an abstract class can extend another Java class and implement multiple Java interfaces.
  6. A Java class can implement multiple interfaces but it can extend only one abstract class.
  7. Interface is absolutely abstract and cannot be instantiated; A Java abstract class also cannot be instantiated, but can be invoked if a main() exists.
  8. In comparison with java abstract classes, java interfaces are slow as it requires extra indirection.

 

Interface

Java interface

A java class containing all the methods as abstract is called an interface. A method that has no implementation and which is expected to be implemented by a subclass is called an abstract method. Java interface can contain constants. When an interface needs to be instantiated it should be implemented by a class and all its abstract methods should be defined. If all the methods are not implemented in the class then it becomes a java abstract class and so cannot be instantiated. Variables declared in a java interface by default is final

interface interf
{
public void show();
public void display();
}

Filter

What is a filter?

A filter is used to dynamically intercept request and response objects and change or use the data present in them. Filters should be configured in the web deployment descriptor. Filters can perform essential functions like authentication blocking, logging, content display style conversion, etc.

Abstract

What is an abstract method?

A method that has no implementation and which is expected to be implemented by a subclass is called an abstract method.
ex:   public void display();

What is an abstract class?

A class containing atleast one abstract method is called an abstract class. A method that has no implementation and which is expected to be implemented by a subclass is called an abstract method. An Abstract class cannot be instantiated. It is expected to be extended by a subclass. An abstract class may contain static variables declared. Any class with an abstract method must be declared explicitly as abstract. A class may be declared abstract even if it has no abstract methods. This prevents it from being instantiated.

ex:
public abstract class AAA
{
}

Tuesday, August 14, 2012

UsingEnums Example

public class UsingEnums {

    private UsingEnums(Weekdays testday) {

        switch
         (testday) {
            case Saturday:
            case Sunday:
                System.out.println("It's the weekend!");
                break;
            case Wednesday:
                System.out.println("It's Humpday!");
                break;
            case Friday:
                System.out.println("TGIF!");
                break;
            default:
                System.out.println("Backto work!");
        }
    }

    public enum Weekdays {

        Monday,
        Tuesday,
        Wednesday,
        Thursday,
        Friday,
        Saturday,
        Sunday
    };

    public UsingEnums() {
        Weekdays weekDays = null;
        System.out.println(weekDays.Friday);
    }

    public static void main(String[] args) {
        UsingEnums myUE = new UsingEnums();
        UsingEnums myUE1 = new UsingEnums(Weekdays.Monday);
    }
}

StringBuilder Example

public class StringBuilder_IN {

    public static void main(String args[]) {
        String myString = "How";
        StringBuilder myStrBldr = new StringBuilder("How");
        myString += " now";
        myString += " Brown";
        myString += " Cow?";
        myStrBldr.append(" now");
        myStrBldr.append(" Brown");
        myStrBldr.append(" Cow?");
        System.out.println("String= " + myString);
        System.out.println("StringBuilder= " + myStrBldr);
    }
}

StaticImportDemo

public class StaticImportDemo {

    public static void main(String[] args) {
        String intValue = "123";
        String dblValue = "567.80";
        double resultValue = 0;
        try {
            resultValue = parseInt(intValue) + parseDouble(dblValue);
            System.out.println("resultValueis " + resultValue);
        }
        catch (NumberFormatException e) {
            System.out.println("EitherintValueor dblValuenot numeric");
        }
    }
}

Scanner Example

public class NewClass {

    public static void main(String args[]) {
        String lastName;
        System.out.print("Pleaseenter your last name => ");
        Scanner fromkeyboard = new Scanner(System.in);
        lastName = fromkeyboard.next();
    }
}

Formatter Example

public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        double balance = 1234.56;
        System.out.format("Balanceis $%,6.2f", balance);
        Formatter myUSformat = new Formatter();
        Formatter myFRformat = new Formatter();
        String balUS = myUSformat.format("Balanceis $%,6.2f", balance).toString();
        String balFR = myFRformat.format(Locale.ITALY, "Balance is $%,6.2f", balance).toString();
        System.out.println("\nUS" + balUS);
        System.out.println("FRANCE" + balFR);
    }
}

Monday, August 13, 2012

Generic_Example

public class Generic_IN
{
    public static void main(String args[])
    {
       ArrayList newStyle= new ArrayList();
       newStyle.add(new String("abc0"));
     //  newStyle.add(new Int(10));
       newStyle.add("abc2");
       newStyle.add("101");

       Iterator it = newStyle.iterator();

       while(it.hasNext())
       {
           System.out.println(it.next());
       }



       
    }

}

New For example

public class For_IN
{
    public static void main(String args[])
    {
        String[] str = new String[5];
        str[0]="abc0";
        str[1]="abc1";
        str[2]="abc2";
        str[3]="abc3";
        str[4]="abc4";

        for(String newstr : str)
        {
            System.out.println(newstr+" ;");
        }

        int[] intary = {10,11,12,13};
        System.out.println(Arrays.toString(intary));

    }

}

AutoBoxing_Example

public class AutoBoxing_IN
{
    public static void main(String args[])
    {
        Integer intObject= new Integer(123);
        System.out.println(intObject);
        int intintPrimitive= intObject;
        System.out.println(intintPrimitive);
        double doublePrimitive= 123.45;
        System.out.println(doublePrimitive);
        Double doubleObject= doublePrimitive;
        System.out.println(doubleObject);
    }

}