Understanding access modifiers
what is 'protected' access modifier?
protected access modifier is used to make fields and methods accessible only within the same package --> incase of Inheritance, protected can also be used by subclasses (child classes outside of the package / inherited)**
What are access modifiers in Java?
- Access modifiers are used to change level of access in classes. ------------------------------- There are 4 access modifiers -> public, protected, default and private ------------------------------- - A class in Java can only be public or default public class -> can be accessed anywhere in the project default class -> can be accessed by classes in the same package protected class -> can be accessed by classes within the same package (& in case of inheritance, protected can also be accessed outside of the package.) private class -> cannot by any classes within the same package, private can only be accessed within its own class. ------------------------------ NOTE: Access modifiers cannot be applied to local variable
What is the difference between default and public classes?
- default classes will only work in the package it was created in. (will not compile outside the package) - public classes can be used anywhere, inside or outside of its own package
Similarities between default and public classes?
-default and public classes can be used in the same package with no issues. - when made in the same package there is no need to import
What cannot be declared as private ?
Classes and Interfaces cannot be declared as private
TRUE OR FALSE: 'private' access modifier is used to make fields and methods only accessible within the same package.
FALSE!!!!! --> 'private' access modifier is used to make fields and methods unreachable, they can only be accessed within the class itself.
what is 'default' access modifier?
default access modifiers are used to make fields and methods only accessible within the same package.
what is 'private' access modifier?
'private' access modifier is used to make fields and methods unreachable, they can only be accessed within the class itself.
what is 'public' access modifier?
'public' access modifier is used to make fields and methods accessible anywhere in the project
Can Classes in Java be declared protected?
Classes CANNOT be declared protected. This access modifier is generally used in a parent child relationship.