DECLARATIONS AND ACCESS CONTROL
ACCESS MODIFIERS
Modifier | Used with | Description |
public | Classes Interfaces Constructors Inner Classes Methods Field variables |
A Class or Interface may be accessed from outside its package. Constructors, Inner Classes, Methods and Field variables may be accessed from wherever their class is accessed. |
protected | Constructors Inner Classes Methods Field variables |
May be accessed by other classes in the same package or from any subclasses of the class in which they are declared. |
private | Constructors Inner Classes Methods Field variables |
May be accessed only from within the class in which they are declared. |
no modifier | Classes Interfaces Constructors Inner Classes Methods Field variables |
May only be accessed from within the package in which they are declared. |
SPECIAL MODIFIERS
Modifier | Used with | Description |
abstract | Classes Interfaces Methods |
Declares a Class or Method that is incomplete. All Interfaces are implicitly abstract so the modifier is redundant. A Class which has an abstract Method must be declared abstract. |
final | Classes Field variables Methods Method parameters Local variables |
Indicates a definition is complete and cannot be changed. Classes may not be extended. Field variables may not be modified once a value is assigned. Methods cannot be overridden. Required for Method parameters and Local variables if they are to be used by an Inner Class. Note: A Class may not be both final and abstract. |
native | Methods | Indicates a platform-specific method written in another language. Note: a method cannot be both native and abstract |
static | Initializers Methods Variables |
Indicates an initializer, method or variable belongs to a class vs an instance (object) of the class. Static initializers are processed once, when the class is loaded. Static methods are used to access static variables. They may not be used to access non-static variables unless they specify an instance of the class. |
synchronized | Methods | Indicates a method acquires a lock on an object before it executes. Used to control access to objects shared by multiple threads. |
transient | Variables | Indicates the variable is not part of the permanent state of an object and may not be serialized (written to a stream). |
volatile | Variables | Indicates a variable may be changed by more than one thread. Each thread has it’s own copy of a working variable. Volatile ensures the variable is compared to the master copy each time it is accessed. |
this and super
this
- this is an Object-Oriented Programming (OOP) operator
- it is used to refer to the current instance of an object
- it can be used in the body of a class constructor to refer to the object being created
- it can be used in the body of an instance method or initializer to refer to the object whose method is being executed
- it cannot be used in a static method or initializer
- when used in a constructor it must be the first statment in the constructors body
super
- super is an Object-Oriented Programming (OOP) operator
- used to call a constructor declared in a classes superclass
- if used in a constructor, must be the first statment in constructor body
- You cannot use this() and super() in the same constructor( Important )
Shadowing
- Because of the way identifiers are looked up; shadowing declarations can occur
- For example, a field declaration can be shadowed by a local variable declaration
Hiding
- Shadowing is not the same as hiding
- hiding applies to members that would normally be inherited but are not because of a declaration of the same identifier in a subclass
- a method can hide a method in the superclass by overriding it
- a method cannot override a static method in the superclass; however, it can hide it by using the same declaration
- static methods are hidden vs overridden as the JLS states they “cannot be overridden” so the compiler never compares subclass method declarations to static superclass method declarations.
- a static method in a subclass cannot hide an instance method in the superclass
class SuperA {
void method1() { }
}
class SubA extends SuperA() {
static void method1() { // compile-error }
}
- a hidden method can be accessed by using super() , casting to the superclass or using the methods fully qualified name
ACCESS CONTROL
- declared protected , accessible from other code within the same package or from subclasses in other packages if the outside code is involved in the implementation of the class. For example, the following produces a compile-error
package point;
class Point {
protected int x, y;
}
package threepoint;
import point.Point;
class ThreePoints extends Point {
protected int z;
public void delta(Point p) {
p.x += this.x; // compile-error: cannot access p.x
p.y += this.y; // compile-error: cannot access p.y
}
}
Even though ThreePoints is a subclass of Point, it cannot access the protected fields in Point. The subclass must be involved in the implementation of Point. The fact that the code is within the body of a subclass is irrelevant. To the compiler, Point is a type reference and p.x and p.y are declared protected in the type Point. If the parameter is changed to ThreePoints p the code will compile as the type ThreePoints inherits the protected fields x and y from Point.
Page Visitors: 2999

Tomcy John

Latest posts by Tomcy John (see all)
- A Guide to Continuous Improvement for Architects - February 2, 2023
- Cloud-first Architecture Strategy - January 26, 2023
- Architecture Strategy and how to create One - January 24, 2023