answersLogoWhite

0

What is protected in Java?

User Avatar

Anonymous

14y ago
Updated: 8/16/2019

Protected and Default Access Levels:

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.

User Avatar

Wiki User

14y ago

What else can I help you with?

Related Questions

Why is security required in java?

so it can be protected


Two types of access modifier in java?

public, <blank/none specified>, protected, private


In java by default all class variables are public protected or private?

The default (no qualifiers) is different from any of those three.


What is an access specifier do?

There is no such thing as an access specifier in Java. There are access modifiers. They specify the access level of the item they modify: public, private, protected.


What is acess spacifier in java?

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.


Explain different access modifier in java?

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.


Discuss the different level of access protection in java?

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.


Write a Program to demonstrate accessibility of different access specifiers in java?

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.


How java programming is safer for the web?

Java applications run in a "sandbox", which limits access to the underlying hardware (such as files, printers, and so on), so that malicious programs are limited in the amount of harm they can do. All modern web-based languages (Silverlight, Java, and Flash) are all protected in a limited execution environment in order to limit potential side effects and intentional sabotage.


Is it possible to declare class as private?

Yes, but not in a normal way.Sample code:namespace WikiAnswers {public class MainClass {protected class HereItIs {}}}The inner class MainClass.HereItIs is the answer to this question.If you change the accessibility of MainClass from public to protected, the compilation will fail.My interpretation of the keyword protected is: "accessible to the class itself and its derived classes", and may only decorate/mark it to class elements (data members, methods, properties, etc.)Noramlly, we declare a class as a namespace element, e.g. MainClass above, I almost answered 'NO' to this question when I read the question the first time. Tricky.The above example may be applicable to Java:1. replace namespace to package2. the rest of the codes should be the same


What Indonesian island is Jakarta on?

java


Does Visual Java plus plus and Java Builder is different from the Java language?

Yes!Visual Java plus plus and Java Builder is different from the Java language?