Wednesday 8 February 2017

Packages And Interfaces


                                                       
Packages And Interfaces
                                     

Packages

Packages are the containers for classes that are used to keep the class name space compartmentalized. Packages are stored in a hierarchical manner and are explicitly imported into new class definitions.

To create a package , simply include a package command as the first statement in a java source file. It's general form is:

  package pkg;

          here pkg is the name of package.

Ex-

   //Simple package program
package MyPack;

class balance 
{
    String name;
     double bal;

   Balance ( String n , double b)
{
    name=n;
     bal=b;
 }
 void show() 
{
  if(bal<0)
     System.out.print("===>");
     System.out.println(name + " Rs" + bal);
  }
}
class AccountBalance
{
     public static void main ( String [] args )
   {
       Balance curr [] = new Balance [3];
       curr [0] = new Balance (" ABC " , 123.334);
       curr [1] = new Balance (" PQR " , 1243.4);
       curr [2] = new Balance (" XYZ " , 44567.564);

    for( int i=0 ; i<3 ; i++)
   {
         current[i].show();
    }
}

Interfaces

Using the keyword interface , you can fully abstract a class's interface from its implementation. Interface specifies , What a class must do , but not how it does it.

Interface are syntactically similar to classes , but they lack  instance variables , and their methods are declared without any body.Once defined any number of classes can implement an interface. Also , one class can implement any number of interfaces.

It's general form is :

       access_modifier  interface name 
{
        return_type   method_name1 ( );
        return_type   method_name2 ( );
         type final varname1 = value ;
          type final varname2 = value ;
          .
          .
          .
          return_type   method_nameN ( );
          type final varnameN = value ;
 }

  Here , access_specifier is either public or not used , name is the name of interface , 


Ex-
   
          class client implements callback
{
            public void callback ( int p)
    {
        System.out.println (" Callback called with " + p);
     }
}
        
                                                                       By: Knowledge Bits

No comments:

Post a Comment

Video of the Day

Contact us

Name

Email *

Message *