Packages and Interfaces
Sometimes what we find that two classes with same name can collide wvwntually which results in error and this is not the good habbit of programming in the java.Packages are one type of container which stores different classes regardless of the collision of the classes with same name stored elsewhere.
Packages statements defines a name Now we will peep into two Important topic which plays a great role in shaping java into its full beauty.
spaces in which classes are stored.
Finding Packages and CLASSPATH
Sometimes youmay come across some of the important questions that where to look for the packages to store the classes.the answer for this question is in three parts.
1)BY defalt the java run time system uses the current working directory as its starting point.Thus if your package is in the subdirectory of the current directory it will be found.
2)you can specify a directory path or paths by setting the CLASSPATH environmental variables.
A short package example
package Mypack;//creation of the package which will contain the class
class Balance
{
string name;
int bal;
Balance(string m, int n)
{
name=m;
bal=n;
}
void show()
{
System.out.println(name+"&"+bal);
}
}
class AccountBalance
{
public static void main(String args[])
{
Balance current[]=new Balance[3];
current[0]=new Balance("anand kumar jha",1000000);
current[1]=new Balance("munna kumar jha",100000);
current[2]=new Balance("Rakesh kumar jha",10000);
for(i=0;i<3;i++)
{
current[i].show();
}
}
Call this file AccountBalence .java and put it in a directory called Mypack.Next compile the file.Make sure that the resulting .claas file is also in the Mypack directory.Then try executing the AccountBalance class
Access protection
Classes and packages both are used for encapsulating and containing the name and scope of the variables and methods.as we have understood from previous example that packages are the container of different classes and other packages,and classes act as the container for the codes and methods so we can conclude that the classes are the smallest unit of abstraction.because of this java addresses four categories of visibility for class members:
*subclasses in the same packages
*non-subclasses in the same packages
*subclasses in the different packages
*classes that are neither in the same package nor subclasses
An access example
package p1;
public class protection
{
int n=1;
public int m=2;
protected m_pro=3;
private m_pri=4;
}
public protection()
{
System.out.println("base constructor");
System.out.println(n);
System.out.println(m);
System.out.println(m_pro);
System.out.println(n_pri);
}
}
package p1;
class derived extends protection
{
derived()
{
System.out.println(n);
System.out.println(m);
System.out.println(m_pro);
//System.out.println(m_pri);
}
}
package p1;
class samepackage
{
samepackage()
{
System.out.println(n);
System.out.println(m);
System.out.println(m_pro);
//System.out.println(m_pri);
}
}
package p2;
class protection2 extends p1.proctection
{
protection2()
{
//System.out.println(n);
System.out.println(m);
System.out.println(m_pro);
//System.out.println(m_pri);
}
}
package p2;
class otherpackage
{
otherpackage()
{
p1.protection p=new p1.protection();
//System.out.println(p.n);
System.out.println(p.m);
//System.out.println(p.m_pro);
//System.out.println(p.m_pri);
}
}
package p1;
class packagedemo
{
public static void main(String args[])
{
protection ob1=new protection();
derived ob2=new derived();
samepackage=new samepackage();
}
}
package p2;
class demo
{
public static void main(String args[]);
{
protection2 ob4=new protection2();
otherpackage ob=new otherpackage();
}
}
Importing Packages
Java includes the import statements to bring certain classes or entire packages,into visibility.The import is a convenience to the programmer and it is not necessary for the programmer to use it in the program. In java source file import statement immeadiatly follows after the packages .This is the general form of the import packages.
As for the example
import pkg1[.pkg2].(classname)
here pkg1 is the name of the outer most packages and the pkg2 is the second most packages which is inside the pkg1 and which is having the great use.there is no practicle limit of the depth of the packages hirarchy .finally you specify the explicit classname or a star which is then java compiler then import the entire package.
The code fragment shows both forms in use:
Import java.util.Date;
An example program which shows the use of the import packages are as follows.
package Mypack;
public class Balance
{
string name;
int bal;
public Balance(string m,int n)
{
name=m;
bal=n;
}
void show()
{
System.out.println("the name is"+name+"and the balance is"+bal);
}
}
import Mypack.*;
class AccountBalance1
{
public static void main(String args[])
{
Balance ob=new Balance("anand kumar jha",123456);
ob.show();
}
}
Interface
Interface is the main topic which has a great use in java programming language.A classes can be fully abstracted using this concept. It means that you may specify what a class must do regardless of how actually it does it. Interface is just similar to the classes. the only difference of it from the classes is that it does not have any instance variable and the methods can be defined without any body of it.
An interface is defined much like a class.
access interface_name
{
return_type method_name1(parameter_list);
return_type method_name2(parameter_list);
return_type method_name3(parameters);
}
The simple program which shows the implementation of the interface
interface callback
{
void call(int param);
}
class Client implements Callback
{
public void call(int p)
{
System.out.println("the value of the p is "+P);
}
}
class testIface
{
public static void main(String args[])
{
Callback 0=new Client();
o.call(42);
}
}
It is always possible for the class which implements interfaces to add some extra methods of their own . It is important for the class to be declared in public which implements the interface. Notice that the object o is declared to be of the interface type Callback.yet it was assigned an instance of Client. The object 0 although access the call() method but it does not access any other methods.
Sometimes what we find that two classes with same name can collide wvwntually which results in error and this is not the good habbit of programming in the java.Packages are one type of container which stores different classes regardless of the collision of the classes with same name stored elsewhere.
Packages statements defines a name Now we will peep into two Important topic which plays a great role in shaping java into its full beauty.
spaces in which classes are stored.
Finding Packages and CLASSPATH
Sometimes youmay come across some of the important questions that where to look for the packages to store the classes.the answer for this question is in three parts.
1)BY defalt the java run time system uses the current working directory as its starting point.Thus if your package is in the subdirectory of the current directory it will be found.
2)you can specify a directory path or paths by setting the CLASSPATH environmental variables.
A short package example
package Mypack;//creation of the package which will contain the class
class Balance
{
string name;
int bal;
Balance(string m, int n)
{
name=m;
bal=n;
}
void show()
{
System.out.println(name+"&"+bal);
}
}
class AccountBalance
{
public static void main(String args[])
{
Balance current[]=new Balance[3];
current[0]=new Balance("anand kumar jha",1000000);
current[1]=new Balance("munna kumar jha",100000);
current[2]=new Balance("Rakesh kumar jha",10000);
for(i=0;i<3;i++)
{
current[i].show();
}
}
Call this file AccountBalence .java and put it in a directory called Mypack.Next compile the file.Make sure that the resulting .claas file is also in the Mypack directory.Then try executing the AccountBalance class
Access protection
Classes and packages both are used for encapsulating and containing the name and scope of the variables and methods.as we have understood from previous example that packages are the container of different classes and other packages,and classes act as the container for the codes and methods so we can conclude that the classes are the smallest unit of abstraction.because of this java addresses four categories of visibility for class members:
*subclasses in the same packages
*non-subclasses in the same packages
*subclasses in the different packages
*classes that are neither in the same package nor subclasses
An access example
package p1;
public class protection
{
int n=1;
public int m=2;
protected m_pro=3;
private m_pri=4;
}
public protection()
{
System.out.println("base constructor");
System.out.println(n);
System.out.println(m);
System.out.println(m_pro);
System.out.println(n_pri);
}
}
package p1;
class derived extends protection
{
derived()
{
System.out.println(n);
System.out.println(m);
System.out.println(m_pro);
//System.out.println(m_pri);
}
}
package p1;
class samepackage
{
samepackage()
{
System.out.println(n);
System.out.println(m);
System.out.println(m_pro);
//System.out.println(m_pri);
}
}
package p2;
class protection2 extends p1.proctection
{
protection2()
{
//System.out.println(n);
System.out.println(m);
System.out.println(m_pro);
//System.out.println(m_pri);
}
}
package p2;
class otherpackage
{
otherpackage()
{
p1.protection p=new p1.protection();
//System.out.println(p.n);
System.out.println(p.m);
//System.out.println(p.m_pro);
//System.out.println(p.m_pri);
}
}
package p1;
class packagedemo
{
public static void main(String args[])
{
protection ob1=new protection();
derived ob2=new derived();
samepackage=new samepackage();
}
}
package p2;
class demo
{
public static void main(String args[]);
{
protection2 ob4=new protection2();
otherpackage ob=new otherpackage();
}
}
Importing Packages
Java includes the import statements to bring certain classes or entire packages,into visibility.The import is a convenience to the programmer and it is not necessary for the programmer to use it in the program. In java source file import statement immeadiatly follows after the packages .This is the general form of the import packages.
As for the example
import pkg1[.pkg2].(classname)
here pkg1 is the name of the outer most packages and the pkg2 is the second most packages which is inside the pkg1 and which is having the great use.there is no practicle limit of the depth of the packages hirarchy .finally you specify the explicit classname or a star which is then java compiler then import the entire package.
The code fragment shows both forms in use:
Import java.util.Date;
An example program which shows the use of the import packages are as follows.
package Mypack;
public class Balance
{
string name;
int bal;
public Balance(string m,int n)
{
name=m;
bal=n;
}
void show()
{
System.out.println("the name is"+name+"and the balance is"+bal);
}
}
import Mypack.*;
class AccountBalance1
{
public static void main(String args[])
{
Balance ob=new Balance("anand kumar jha",123456);
ob.show();
}
}
Interface
Interface is the main topic which has a great use in java programming language.A classes can be fully abstracted using this concept. It means that you may specify what a class must do regardless of how actually it does it. Interface is just similar to the classes. the only difference of it from the classes is that it does not have any instance variable and the methods can be defined without any body of it.
An interface is defined much like a class.
access interface_name
{
return_type method_name1(parameter_list);
return_type method_name2(parameter_list);
return_type method_name3(parameters);
}
The simple program which shows the implementation of the interface
interface callback
{
void call(int param);
}
class Client implements Callback
{
public void call(int p)
{
System.out.println("the value of the p is "+P);
}
}
class testIface
{
public static void main(String args[])
{
Callback 0=new Client();
o.call(42);
}
}
It is always possible for the class which implements interfaces to add some extra methods of their own . It is important for the class to be declared in public which implements the interface. Notice that the object o is declared to be of the interface type Callback.yet it was assigned an instance of Client. The object 0 although access the call() method but it does not access any other methods.
No comments:
Post a Comment