Saturday, December 24, 2011

Abstract Class in JAVA (final,exends)

Definition
An abstract class is a class that is declared by using the abstract keyword. It may or may not have abstract methods. Abstract classes cannot be instantiated, but they can be extended into sub-classes.

Points of abstract class :
  1. Abstract class contains abstract methods.
  2. Program can't instantiate an abstract class.
  3. Abstract classes contain mixture of non-abstract and abstract methods.
  4. If any class contains abstract methods then it must implements all the abstract methods of the abstract class.
Example



import java.io.*;
import java.lang.*;
abstract class Ashape
{
      public final double pi=3.14;
      abstract public double area();
      static int cnt;
      public Ashape()  // contructor
      {
           cnt ++ ;
      }
      public void dispCount()
      {
         System.out.println(cnt);
      }
}
Class Acircle extends Ashape
{
     double r;
     public void setRadius(double r)
     {
         this.r = r;
     }
     public double area()
     {
            return pi * r * r;
     }


}
public class AbstractDemo
{
          public static void main(String args[])
          {
                Ashape s=new Acircle();
                ((Acircle)s).setRadius(10);
                System.out.println(s.area());
                s.dispCount();
          }
}




Final : 
final variable can only be initialized once, either via an initializer or an assignment statement. It need not be initialized at the point of declaration: this is called a 'blank final' variable. A blank final instance variable of a class must be definitely assigned at the end of every constructor of the class in which it is declared; similarly, a blank final static variable must be definitely assigned in a static initializer of the class in which it is declared: otherwise, a compile-time error occurs in both cases.


Extends:
Extends keyword is used to implement or share the method/variable of parent class within child class.

The Parent class
public class Parent {
     }
The Child class
public class Child extends Parent {
     }

Blank Final  :
The blank final, which was introduced in Java 1.1, is a final variable whose declaration lacks an initializer. A blank final can only be assigned once and must be unassigned when an assignment occurs. In order to do this, a Java compiler runs a flow analysis to ensure that, for every assignment to a blank final variable, the variable is definitely unassigned before the assignment; otherwise a compile-time error occurs.
In general, a Java compiler will ensure that the blank final is not used until it is assigned a value and that once assigned a value, the now final variable cannot be reassigned another value.

Throws Keyword in JAVA

Throws Keyword :

"throws " is a keyword defined in the java programming language. Keywords are basically reserved words which have specific meaning relevant to a compiler in java programming language likewise the throw keyword indicates the following :
-- The throws keyword in java programming language is applicable to a method to indicate that the method raises particular type of exception while being processed.-- The throws keyword in java programming language takes arguments as a  list of the objects of type java.lang.Throwables class.
-- when we use the throws with a method it is known as ducking. The  method calling a method with a throws clause is needed to be enclosed within the try catch blocks.

Example

import java.io.*;
import java.lang.*;
public class throwsWord
{
   public static void main(String args[])
   {
      int a,b;
      try
      {
            a=Integer.parseInt(args[0]);
            if(a!=0)
            {
                 b=100/a;
            }
            else
            {
                 throw new ArithmeticException("Error");
            }
            System.out.println("division:" + b);
      }
      catch(ArithmeticException e)
      {
           System.out.println("Division by 0..");
           throw e;
      }
   }
}

Multiple Catch Block in JAVA

In this Example i have used Arithmetic and NumberFormat Exception, and also Finally keyword


Arithmetic Exception:
This Exception is thrown when any Arithmetic Error occur such as dividing number by 0


NumberFormat Exception:
NumberFormatException is a subclass of the Runtime Exception class.  A Number Format Exception occurs in the java code when a programmer tries to convert a String into a number. The Number might be int,float or any java numeric values. 


Finally : 
The finally block always executes when the try block exits. This ensures that the finally block is executed even if an unexpected exception occurs. But finally is useful for more than just exception handling — it allows the programmer to avoid having cleanup code accidentally bypassed by a returncontinue, or break.




Example

import java.io.*;
import java.lang.*;
public class MultiCatch
{
          public static void main(String args[])
          {
                    int a,b;
                    try
                    {
                          a=Integer.parseInt(args[0]);
                          b=100/a;
                          System.out.println("Division:"+ b);


                    }
                   catch(ArithmeticException e)
                    { 
                       System.out.println("Division by zero.." );
                    }
                   catch(NumberFormatException e)
                    {
                      System.out.println("Not a Number.." );   
                    }
                  finally
                   {
                      System.out.println("Always executes " );
                   }
          }


}

What is JAVA

Definition:

Java is a computer programming language. It enables programmers to write computer instructions using English based commands, instead of having to write in numeric codes. It’s known as a “high-level” language because it can be read and written easily by humans. Like English, Java has a set of rules that determine how the instructions are written. These rules are known as its “syntax”. Once a program has been written, the high-level instructions are translated into numeric codes that computers can understand and execute.

Example


Addition,Subtraction and multiplication in JAVA


import java.lang.*;
public class mathematics
{
          public static void main(String args[])
          {
                   int a,b,c;
                   a=10;
                   b=5;
                   System.out.println("Add:"+(a + b));
                   System.out.println("Sub:"+(a - b));
                   System.out.println("Multiply:"+(a * b));
          }


}


o/p
15
5
50



Why public static void main(String args[])  is used.

The Main method is the method in which execution to any
java program begins. 

A main method declaration looks as follows: 

public static void main(String args[])
{

}
 

The method is public because it be accessible to the JVM to
begin execution of the program. 


It is Static because it be available for execution without an
object instance. you may know that you need an object instance
to invoke any method. So you cannot begin execution of a class
without its object if the main method was not static. 


It returns only a void because, once the main method execution
s over, the program terminates. So there can be no data that
can be returned by the Main method 


The last parameter is String args[]. This is used to signify
that the user may opt to enter parameters to the java program
at command line. We can use both String[] args or String args[].
The Java compiler would accept both forms.