answersLogoWhite

0


Best Answer

Protected members of a class are only accessible to its subclasses and to friends of the class or its subclasses. However, if a subclass changes the access to private, only the subclass and its friends have access but this does not affect the access rights of its ancestors or their friends.

User Avatar

Wiki User

9y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Can you access a protected member from a Sub class outside the package of its Base class?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What do you mean by public private protected and friendly?

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. Public2. Protected3. Default and4. PrivateJava does not have a friendly access modifier.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).Private Members:Members marked private can't be accessed by code in any class other than the class in which the private member was declaredProtected 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


What is a default value?

A default value is the one that has been considered the most appropriate on severalsystems, methods, programs. Obviously it is the parameter which works better, otherwise it wouldn't be set as a standard value. To set something "by default" is the same as to choose a value, a configuration more convenient for that purpose.


Only public member functions can access public member data TrueFalse?

False. Public member data is accessible to all functions, whether they be public, protected or private members of the same class, or they are outside of the class completely.


What kinds of access can a class member have?

Public, protected and private access members.


Explain the difference between public and protected access specifier?

A private variable is only accessible in the class where it was declaredA protected variable is accessible to sub classes and classes in the same package as where they were declared

Related questions

What is protected in Java?

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 accessSystem.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 classcertification.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.


What do you mean by public private protected and friendly?

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. Public2. Protected3. Default and4. PrivateJava does not have a friendly access modifier.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).Private Members:Members marked private can't be accessed by code in any class other than the class in which the private member was declaredProtected 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


What is a default value?

A default value is the one that has been considered the most appropriate on severalsystems, methods, programs. Obviously it is the parameter which works better, otherwise it wouldn't be set as a standard value. To set something "by default" is the same as to choose a value, a configuration more convenient for that purpose.


Only public member functions can access public member data TrueFalse?

False. Public member data is accessible to all functions, whether they be public, protected or private members of the same class, or they are outside of the class completely.


What kinds of access can a class member have?

Public, protected and private access members.


Explain the difference between public and protected access specifier?

A private variable is only accessible in the class where it was declaredA protected variable is accessible to sub classes and classes in the same package as where they were declared


Difference between public nd default access specifier?

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. Public2. Protected3. Default and4. PrivatePrivate 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 accessSystem.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 classcertification.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.


Are the private access specifiers in Java and C plus plus the same?

No.In Java, the private access modifier restricts member access to the class in which the member is declared. But in C++, private members are also accessible to friends of the class in which they are declared. The rough equivalent in Java would be package private access.Not that Java doesn't have access specifiers, it has access modifiers. When no modifier is specified, default access is implied, which is package private for classes and public for interfaces.


Which access limitation is found in a class member declared protected internal?

"Internal" is not a C++ keyword, so it is meaningless in this context. "Protected" means that the class member is visible to (has scope from) only the class and classes derived from the class.


Which access label allows the variables to be accessible in both the current assembly and by any classes derived from the base class?

Protected access. This is the same as private access except that derivatives can also gain access. However, in order to preserve encapsulation of the base class, only its private member methods should be granted protected access (and only where that access is necessary). Private data members should never be elevated to protected access.


What is friendly in c plus plus?

The keyword "friend" allows a function or variable to have access to a protected member inside a class.


Is the default access specifier same as protected?

A private member of a class can only be accessed by methods of that class. A protected member of a class can only be accessed by methods of that class and by methods of a derived class of that class.