answersLogoWhite

0


Best Answer

A private variable is only accessible in the class where it was declared

A protected variable is accessible to sub classes and classes in the same package as where they were declared

User Avatar

Wiki User

13y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

15y ago

Public access modifier

Fields, methods and constructors declared public (least restrictive) within a public class are visible to any class in the Java program, whether these classes are in the same package or in another package.

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.

Protected access modifier

The protected fields or methods cannot be used for classes and Interfaces. It also cannot be used for fields and methods within an interface. Fields, methods and constructors declared protected in a superclass can be accessed only by subclasses in other packages. Classes in the same package can also access protected fields, methods and constructors as well, even if they are not a subclass of the protected member's class.

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.

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

There are four different visibility (access) modifiers in Java:

# public - Any class can access this field.

# protected - Any class in the same package as this one, as well as any subclass of this class, can access this field.

# (no modifier) - Any class in the same package as this one can access this field.

# private - Only the class to which this field belongs can access it. So specifically for protected fields, it means that you add the ability for all subclasses of this class to directly access the field. (See the simple chart in the related link.)

This answer is:
User Avatar

User Avatar

Wiki User

9y ago

Private members are only accessible to the class itself and to friends of the class. Protected members are the same as private members but are also accessible to derivatives of the class. Public members are fully accessible to any code.

Inheritance can also be public, protected or private. When you derive a new class from an existing class you automatically inherit all the public and protected members of the base class. With public inheritance, subsequent derivatives also inherit these members. With protected inheritance, the public members become protected members of the derivative but can still be inherited by subsequent derivatives. But with private inheritance, the public and protected members become private to the derivative and therefore cannot be inherited by subsequent derivatives. Note that you can only maintain or lower the access rights of a base class member; you cannot increase its access.

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

When data or methods are declared "protected," they can be directly accessed by other objects in the same package (like "public" things), but they cannot be accessed directly by objects in other packages (like "private" things).

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

An Access Modifier is a key word in java that determines what level of access or visibility a particular java variable/method or class has. There are 4 basic access modifiers in java. They are:

1. Public

2. Protected

3. Default and

4. Private

Private is the most restrictive access modifier whereas public is the least restrictive. Default is the access protection you get when you do not specifically mention an access modifier to be used for a java object.

Public Members:

If a variable or method is declared public, it means that it can be accessed by anyone or any other class (irrespective of which package they are in). Of course, it is mandatory that the class inside which the public method is placed is visible in the first place for the method or variable to be visible. You can check out the code example in the previous paragraph for an example. The method from Dad class is visible and available for usage inside the son class.

For a subclass, if a member of its superclass is declared public, the subclass inherits that member regardless of whether both classes are in the same package.

Ex:

package pack1;

public class Parent {

public String getName() {

return "Parent";

}

}

Package pack2;

Import pack1.Parent;

Public class Child extends Parent{

Public String getParentsName() {

return getName();

}

}

If you see the example above, the child class is able to access the parent class's method getName() even without instantiating an object of the parent because it inherits the Parent class and all its method as part of the inheritance (extends) feature.

Protected and Default Members:

The protected and default access control levels are almost identical, but with one critical difference. A default member may be accessed only if the class accessing the member belongs to the same package, whereas a protected member can be accessed (through inheritance) by a subclass even if the subclass is in a different package.

Take a look at the following two classes:

package certification;

public class ClassOne {

void testIt() { // No modifier means method has default access

System.out.println("ClassOne");

}

}

In another source code file you have the following:

package otherCertification;

import certification.ClassOne;

class ClassTwo {

static public void main(String[] args) {

ClassOne o = new ClassOne();

o.testIt();

}

}

As you can see, the testIt() method in the first file has default (think: package-level) access. Notice also that class OtherClass is in a different package from the AccessClass. When you compile the ClassTwo.java file you will get an error like below:

No method matching testIt() found in class

certification.ClassOne.o.testIt();

From the preceding results, you can see that AccessClass can't use the OtherClass method testIt() because testIt() has default access, and AccessClass is not in the same package as OtherClass. So AccessClass can't see it, the compiler complains.

Default and protected behavior differs only when we talk about subclasses. If the protected keyword is used to define a member, any subclass of the class declaring the member can access it through inheritance. It doesn't matter if the superclass and subclass are in different packages, the protected superclass member is still visible to the subclass. This is in contrast to the default behavior, which doesn't allow a subclass to access a superclass member unless the subclass is in the same package as the superclass. (See the example above)

Whereas default access doesn't extend any special consideration to subclasses, the protected modifier respects the parent-child relationship, even when the child class moves away (and joins a new package). So, when you think of default access, think of package restrictions. No exceptions at all. But when you think protected, think package + kids. A class with a protected member is marking that member as having package-level access for all classes, but with a special exception for subclasses outside the package.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Explain the difference between public and protected access specifier?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Explain the significance of public and protected and private access specifiers in inheritance?

These are all access modifiers in Java. a. Public - these are accessible anywhere. This is the least restrictive access specifier. b. Private - these are accessible only inside the declaring class. This is the most restrictive access specifier. c. Protected - these are in between public and private. These are accessible to all classes that inherit this class d. Package - this is the default access specifier. These are accessible to all classes that are present in the same package as the contained class.


Difference between private and protected specifier in c plus plus language?

In C++, the private specifier means that the item can only be accessed by methods of the class, not including methods of derived classes. Protected, on the other hand, means the item can be accessed by methods of the class, and methods of derived classes. Public, to complete the explanation, means that the item can be acessed by any method, this class, another class, or otherwise.


What is the difference between a system and a subsystem?

explain the difference between systems and sub systems


Difference between single-point cutting tool use in lathe and shaping machine?

explain the difference between single point & multi point cutting tool


What is the major difference between noncombustible construction and fire resistive construction?

Basically non combustible and fire resistive are the same. Difference is non combustible has steel frame members that are unprotected or limited protected. Fire resistive has protected steel frame members with a fire rated material such as concrete.

Related questions

Explain the significance of public and protected and private access specifiers in inheritance?

These are all access modifiers in Java. a. Public - these are accessible anywhere. This is the least restrictive access specifier. b. Private - these are accessible only inside the declaring class. This is the most restrictive access specifier. c. Protected - these are in between public and private. These are accessible to all classes that inherit this class d. Package - this is the default access specifier. These are accessible to all classes that are present in the same package as the contained class.


What is the difference between access specifier and access modifier?

An access modifier is another name for an access specifier, which in object-orientated software is a keyword applied to a variable which indicates which other parts of the programme are permitted to access it.


How do you explain the difference between the vassals and serfs?

Explain the difference between the vassals and the serfs


Explain the difference between young and mature mountains?

Explain the difference between young and mature mountains?


Difference between private and protected specifier in c plus plus language?

In C++, the private specifier means that the item can only be accessed by methods of the class, not including methods of derived classes. Protected, on the other hand, means the item can be accessed by methods of the class, and methods of derived classes. Public, to complete the explanation, means that the item can be acessed by any method, this class, another class, or otherwise.


What is difference between explain and describe?

Describe is what it is and explain is why it is as it is


What is the difference between describe and explain?

Describe is what it is and explain is why it is as it is


Explain the difference between a cash and credit transaction for purchass and sales?

explain the difference between cash and credit transaction


Explain the difference between share of customer and customer equity.?

Explain the difference between share of customer and customer equity


What is the difference between a system and a subsystem?

explain the difference between systems and sub systems


Explain the difference betwwen the two types of feeding?

explain the difference between the two types of feeding?


Explain the difference between the solubility behaviors of glycerol and triglycerides?

Explain the difference between the solubility behaviors of glycerol,fatty acid and triglycerides