how to specify access specifiers: public: private: protect
Access control:
Encapsulation links data with the code that manipulates it. However,encapsulation provides another important attribute: access control.Java’s access specifiers are public, private, and protected. Java also defines a default access level.protected applies only when inheritance is involved. When a member of a class is modified by the public specifier, then that member can be accessed by any other code.
When a member of a class is specified asprivate, then that member can only be accessed by other members of its class.
When a member of a class is specified asprivate, then that member can only be accessed by other members of its class.
Acess specifiers
public:
A class, method, constructor, interface etc declared public can be accessed from any other class. Therefore fields, methods, blocks declared inside a public class can be accessed from any class belonging to the Java
Universe.
Methods, Variables and Constructors that are declared private can only be accessed within the declared class itself. Private access modifier is the most restrictive access level. Class and interfaces cannot be private.Variables that are declared private can be accessed outside the class if public getter methods are present in the class.
protect:
Variables, methods and constructors which are declared protected in a superclass can be accessed only by the subclasses in other package or any class within the package of the protected members' class.The protected access modifier cannot be applied to class and interfaces.
Default access modifier means we do not explicitly declare an access modifier for a class, field, method, etc. A variable or method declared without any access control modifier is available to any other class in the same package. The fields in an interface are implicitly public static final and the methods in an interface are by default public.
/* This program demonstrates the difference between public and private.*/
class Test
{
int a; // default access
public int b; // public access
private int c; // private access
// methods to access c
void setc(int i) // set c's value
{
c = i;
}
int getc() // get c's value
{
return c;
}
}
class AccessTest
{
public static void main(String args[])
{
Test ob = new Test();
// These are OK, a and b may be accessed directly
ob.a = 10;
ob.b = 20;
// This is not OK and will cause an error
// ob.c = 100; // Error!
// You must access c through its methods
ob.setc(100); // OK
System.out.println("a, b, and c: " + ob.a + " " +
ob.b + " " + ob.getc());
}
}
You lost me, buddy. I mean, I suppose I get what youre saying. I have an understanding of what you’re saying, but you just appear to have ignored that there are some other individuals within the world who see this issue for what it really is and may not agree with you. You may perhaps be turning away a lot of persons who may have been followers of your website. javascript blogging platform
ReplyDeleteI just added this weblog to my rss reader, great stuff. Cannot get enough! python string comparison
ReplyDeleteKanhasoft is leading Django Application Development Company in India and USA. We have a team of 45+ experienced Django developers to outsource our services. You can visit our site for more information.
ReplyDeleteWell done – It really takes a lot of time to have blog posts. While I don’t agree on your blog I will concede that your are entitled to have your own perspective. The site ended up having some cool perspectives. Cool work job– JSON Formatter Online
ReplyDeleteI found that site very usefull and this survey is very cirious, I ' ve never seen a blog that demand a survey for this actions, very curious... Melbourne Access Control Systems
ReplyDeleteYou have shared a nice article here about thecctv and access control solution .cctv and access control solution company. Your article is very informative and nicely describes the causes and natural remedies of Snoring. I am thankful to you for sharing this article here.
ReplyDeleteThis comment has been removed by the author.
ReplyDelete