Pages

Tuesday, 6 March 2012

Try and Catch statements in Java

       Using try and catch ,the default exception handler provide the Java Runtime System is used for debugging.You will usually want to handle an exception yourself.To guard against and handle a runtime error simply encloses the code that you want to monitor inside the try block.Immediately following the try block inside a catch clause that specify the exception type that you want to wish to catch.The following program include try block and catch clause which process Arithmetic Exception.
       class A
       {
                    int d,a;
                    try
                    {
                             d=0;
                             a=42;
                             a=a/d;
                             system.out.println("This will not be permitted");
                    }
                    catch(Exception e)
                    {
                             System.out.println("division by 0is not possible !");
                    }
       }     
OUTPUT
        division by 0 is not possible.

Introduction to Packages in Java

      Packages are containers for classes.We can define  classes inside a package that are not accessible by code outside that package.A package can be defined by including a package command as the first statement in the Java source file.Any classes declared within that file will belong to the specified package.The package statement defines a namespace in which classes are stored.If we omit the package statement,the class names are put into the default package,which has no name.
     General Form:
               package package_name ;
      Example:   package MyPackage ;
      which creates a package called MyPackage.Java uses the file system directories to store packages ,that is the class file for any classes that declared to be a part of MyPackage and must be stored in a directory called MyPackage.The directory name must match the package name exactly.More than one file can include the same package statement.We can create a hierarchy of packages.To do so simply separate each package name from the one above it by use of a period.The general form of a multilevel package statement is ;
                                        package pkg1.[pkg2.[pkg3]] ;
         A package hierarchy must be referred in the file system of the Java Development System,
                                        package java.awt.image ;
        needs to be stored in the java\awt\image
Example program:
        
      package MyPackage;
      class Balance 
      {
                  String name;
                  double bal;
                  Balance (String n,double b)
                  {
                               name = n;
                               bal = b;
                               show( );
                  }
                  void show( )
                  {
                               if(bal > 0)
                               {
                                              System.out.println("Name = "+name+"  balance = "+bal);
                               }
                               else
                               { 
                                              System.out.println("Name = "+name+"  balance = ----");
                               }
                  }
      }
      class AccountBalance
      {
                  public static void main(String args[ ])
                  {
                              balance current[ ] = new Balance[3];
                              current[0] = new Balance("John",123.25);
                              current[1] = new Balance("Jack",155.99);
                              current[2] = new Balance("Tom",160.00);
                  }
      }
OUTPUT
     Name = John Balance = 123.25
     Name = Jack Balance = 155.99
     Name = Tom Balance = 160.00

Dynamic Method Dispatch in Java

    Method overriding forms the basis for one of Javas most powerful concept called Dynamic Method Dispatch  .Which is the mechanism by which a call to an overridden method is resoled at runtime rather than at compile time.Dynamic Method Dispatch shows how Java implements runtime polymorphism.A superclass reference to a subclass object.Java uses this concept of act to resolve the calls to overridden methods at runtime.When an overridden method is called through a superclass reference , Java determines which version of that method to execute based upon the type of the object being referred to at the time call occurs.Thus this determination is made at runtime, that is it is the type of the object being referred to that determines which version of an overridden method will be execute.
   class A
   {
         void CallMe( )
         {
                 System.out.println( "Inside A's CallMe method !" );
         }
   }
   class B extends A
   {
         void CallMe( )
         {
                 System.out.println( "Inside B's CallMe method !" );
         }
   } 
   class C extends B
   {
         void CallMe( )
         {
                 System.out.println( "Inside C's CallMe method !" );
         }
   } 
   class MethodDispatch
   {
          public static void main(String args[ ])
          {
                    A a = new A( );//object of A
                    B a = new A( );//object of B
                    C a = new A( );//object of C
                    A  r;
                    r.CallMe( );
                    r = b;//referred to B's object
                    r.CallMe( );
                    r = c;//referred to C's object
                    r.CallMe( ); 
          }
   }
 OUTPUT
   Inside A's CallMe method
   Inside B's CallMe method
   Inside C's CallMe method 

Monday, 5 March 2012

Method Overriding in Java

           When a method in a sub class has the same name and type as a method in its super class,then the method in the subclass is said to be override in the super class.When an overridden method is called from within a subclass,then it will always refer to the version of that methoddefined by the subclass.The version of the method defined by the super class will be hidden.
     class A
     {
               void show( )
               {
                       System.out.println( "\nsuper class !");
               }
     }
     class B extends A
     {
               void show( )
               {
                       System.out.println( "base class!" );
               }
     }
     class Overriding( )
     {
               public static void main(String args[ ] )
               {
                      B obj = new B( );
                      obj.show( );
                      super.show( );
               }
     }
OUTPUT
  base class!
  super class
     here the versions of show( ) inside B overrides version declared in A.To access the super class version of overridden function it can do so by using super keyword.In the programe it is illustrated as
   super.show( );
and it will call the super class function.

Method Overloading

           In Java it is possible to define two or more methods within the same class that share the same name as long as their parameters declarations are different.Then the methods are said to be overloaded and the process is referred to be Method Overloading.
           Method Overloading is one of the ways that Java implement polymorphism.When as overloaded method is invoked, java uses the type and number of arguments as its guide to determine which version of the overloaded method to actually call.This overloaded methods must differ in the type and the number of their parameters and it may have different return types.

class OverloadDemo
{
             void test( )
             {
                       System.out.println( "No parameters!" );
             }
             void test(int a , int b)
             {
                       int c;
                       c=a+b;
                       System.out.println( " a+b = " + c);
             }
             double test(double a)
             {
                       return a*a;
             }
  }
  class Overload
  {
               public static void main( String args[ ] )
               {
                         double result;
                         OverloadDemo obj = new OverloadDemo( );
                         obj.test( );
                         obj.test( 10,20);
                         result = obj.test(10);
                         System.out.println( "result="+result);
               }
  }
OUTPUT
      No parameters!
      a+b=30
      result=100