The protected and default access levels:
Qualified name access to members designated protected or of default access level (because they have no access modifier) is available only to classes in the same package. In this respect, there is no distinction between these two access levels. For example, if we have:
package AppletUser;
public class UserV3
{
// protected instance variable
protected int numOfAccesses;
// default access level method
void setNumOfAccesses(int n)
{
numOfAccesses = n;
}
}
and we were to test the class as follows: import AppletUser.*; // this class does not compile!
public class TestUserV3
{
public static void main (String []args)
{
// this method has a UserV3 reference
UserV3 myUser;
// initialize the reference
myUser = new UserV3();
// qualified access to instance variable numOfAccesses
// is not allowed!
myUser.numOfAccesses = 1;
// qualified access to setNumOfAccesses method
// is not allowed!
myUser.setNumOfAccesses(1);
}
}
we would find that the class TestUserV3 does not compile, because the condition that it should be in the same package as UserV3 is not satisfied and therefore it cannot access either the protected or the default access level members of UserV3. Instead of importing the classes in the AppletUser package, the TestUserV3 class would have to be preceded by the words package AppletUser;
Inheritance of protected and default members
Where the protected and default access levels differ is in terms of inheritance of members by subclasses. Any subclass inherits any protected and public members of its superclass. Any subclass in the same package as its superclass inherits not only the protected and public members of its superclass, but also any default access level members. Default access level is sometimes known as 'package access' for this reason. Thus the default access type is more restrictive in terms of inheritance of members than the protected type. These members are inherited only by subclasses in the same package as the superclass. For example, the class UserV3 above has the method setNumOfAccesses defined as default access level. Therefore, any class extending UserV3 and intending to use setNumOfAccesses must be preceded by the statement:
package AppletUser;
instead of importing from the AppletUser package. If setNumOfAccesses had been defined with protected access level, then it would have been suf?cient for any subclass definition to start with an import statement.
Java Assignment Help - Java Homework Help
Struggling with java programming language? Are you not finding solution for your The protected and default access levels homework and assignments? Live The protected and default access levels experts are working for students by solving their doubts & questions during their course studies and training program. We at Expertsmind.com offer The protected and default access levels homework help, java assignment help and The protected and default access levels projects help anytime from anywhere for 24x7 hours. Computer science programming assignments help making life easy for students.
Why Expertsmind for assignment help
- Higher degree holder and experienced experts network
- Punctuality and responsibility of work
- Quality solution with 100% plagiarism free answers
- Time on Delivery
- Privacy of information and details
- Excellence in solving java programming language queries in excels and word format.
- Best tutoring assistance 24x7 hours