| Forums.Sureshkumar.net : A Perfect Place to Share Knowledge Blogs Games Magazines |
|
|
#2 (permalink) |
|
Moderator
Join Date: Jul 2006
Posts: 2,184
Thanks: 5 Thanked 371 Times in 255 Posts Thanks: 5
Thanked 371 Times in 255 Posts
Rep Power: 58
|
Re: about access specifier
One of the techniques in object-oriented programming is encapsulation. It concerns the hiding of data in a class and making this class available only through methods. In this way the chance of making accidental mistakes in changing values is minimized. Java allows you to control access to classes, methods, and fields via so-called access specifiers.
Java offers four access specifiers, listed below in decreasing accessibility: * public * protected * default (no specifier) * private We look at these access specifiers in more detail. public public classes, methods, and fields can be accessed from everywhere. The only constraint is that a file with Java source code can only contain one public class whose name must also match with the filename. If it exists, this public class represents the application or the applet, in which case the public keyword is necessary to enable your Web browser or appletviewer to show the applet. You use public classes, methods, or fields only if you explicitly want to offer access to these entities and if this access cannot do any harm. An example of a square determined by the position of its upper-left corner and its size: public class Square { // public class public x, y, size; // public instance variables } protected protected methods and fields can only be accessed within the same class to which the methods and fields belong, within its subclasses, and within classes of the same package, but not from anywhere else. You use the protected access level when it is appropriate for a class's subclasses to have access to the method or field, but not for unrelated classes. default (no specifier) If you do not set access to specific level, then such a class, method, or field will be accessible from inside the same package to which the class, method, or field belongs, but not from outside this package. This access-level is convenient if you are creating packages. For example, a geometry package that contains Square and Tiling classes, may be easier and cleaner to implement if the coordinates of the upper-left corner of a Square are directly available to the Tiling class but not outside the geometry package. private private methods and fields can only be accessed within the same class to which the methods and fields belong. private methods and fields are not visible within subclasses and are not inherited by subclasses. So, the private access specifier is opposite to the public access specifier. It is mostly used for encapsulation: data are hidden within the class and accessor methods are provided. An example, in which the position of the upper-left corner of a square can be set or obtained by accessor methods, but individual coordinates are not accessible to the user. public class Square { // public class private double x, y // private (encapsulated) instance variables public setCorner(int x, int y) { // setting values of private fields this.x = x; this.y = y; } public getCorner() { // setting values of private fields return Point(x, y); } } The following table summarizes the access level permitted by each specifier. ![]() Note the difference between the default access which is in fact more restricted than the protected access. Without access specifier (the default choice), methods and variables are accessible only within the class that defines them and within classes that are part of the same package. They are not visible to subclasses unless these are in the same package. protected methods and variables are visible to subclasses regardless of which package they are in.
__________________
Kiran Last edited by kiran2710; 21-02-08 at 05:43 PM. |
|
|
|
|
|
#3 (permalink) |
|
Junior Member
Join Date: Feb 2008
Posts: 25
Thanks: 3 Thanked 2 Times in 2 Posts Thanks: 3
Thanked 2 Times in 2 Posts
Rep Power: 1
|
Re: about access specifier
hai kiran,
i have understood what you said.So the default access specifier of a class is default.see the below prg...... class Demo { int a=5; void print() { System.out.println(" a is: "+a); } } class Main { public static void main(String args[]) { Demo d=new Demo(); d.print(); } } Here the "Main"class is able to acess the variable "a" and the function "print()" which are the default members of Demo class which explains to be as the classes "Demo" and "Main" belongs to the same package. Does the classes "Demo" and "Main" really belongs to the same package.If so,which package it is? and how? |
|
|
|
|
|
#4 (permalink) |
|
Moderator
Join Date: Jul 2006
Posts: 2,184
Thanks: 5 Thanked 371 Times in 255 Posts Thanks: 5
Thanked 371 Times in 255 Posts
Rep Power: 58
|
Re: about access specifier
Classes Main and Demo will be in the default package.
Here the default package in the sense, its the folder where the java file will reside. E.g. Here I placed the file in c:\demopack folder and compiled. As a result the classes will be stored in the demopack. The scope\visibility of this classes will be in demopack. class Demo { int a=5; void print() { System.out.println(" a is: "+a); } } class Main { public static void main(String args[]) { Demo d=new Demo(); d.print(); } }
__________________
Kiran |
|
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Mega Free Download S/W Links A-Z | yathish | OTHERS | 513 | 30-09-08 02:49 PM |
| Sql - 1)What is difference between Oracle and MS Access? 2) What are disadvantages in Oracle and MS Access? 2) What are feratures&advantages in Oracle and MS Access? | radhika_btech | Oracle Interview / Technical Questions | 0 | 23-01-07 09:33 PM |
| Remote PC access | yathish | Amazing TIPS & TRICKS | 1 | 03-01-07 11:20 AM |
| Calls to India and other International destinations just got even cheaper!!! | AjayKumar.Kataram | Amazing TIPS & TRICKS | 2 | 09-12-06 03:40 PM |
| JAVA collection | naga | JAVA Technologies | 0 | 21-02-06 06:01 AM |