Professional Documents
Culture Documents
Topic 2
Declarations & Access Control Modifiers
Example
public abstract class Test{
final int x; Instance var
static String name; Static var
Test (){
x=99; Constructor
}
public static void main(String [ ] args){ Method var
double d1; Local var
Test t= new Test();
System.out.println(t.name);
System.out.println(Test.name);
}
public abstract void doThings(int y); Abstract method
}
Access Modifiers
Define the access control to members
of the class.
Java supports 4 distinct access levels
public
protected
private
Access Modifiers
public – can be accessed by any other
class.
protected – can be accessed by
classes in the same package or its
subclasses in different package.
default (no modifier) – can be accessed
by classes in the same package only.
private – can only be accessed from
inside the class.
Access Modifiers
Class2 Class1
OK
Public int a;
OK
protected int b;
OK
int c;
error private int d;
X
Package1
Access Modifiers
Class2 Class1
OK
public int a;
OK
protected int a;
error
X int b;
error
X private int c;
Package2 Package1
B
Member Modifiers
Member can have all 4 access levels.
If a class cannot be accessed, it’s members
cannot be accessed.
final methods cannot be overridden in a
subclass.
final instance variable cannot be reintialised
once assigned a value.
Local variable cannot have access modifiers,
the only allowed modifier is final.
Local variable must be initialized before use.
Inheritance and Member
Accessibility
Only the private elements of the Superclass
super class are not accessible
from its subclasses. All other
types of elements are accessible.
Protected members can be
directly accessed by the
subclasses and also other
classes in the same package.
Subclass
public
private
protected
Question 10 – protected
P1 P2
Protected member
can only be accessed
by its subclass in
different package
Parent Child
X=4 X=4
c.doStuff() OK
doStuff()
p.doStuff
Member non-access Modifiers
Final methods cannot be overridden in a subclass.
Abstract methods cannot be private/final as they must
be inheritable.
Both synchronized and native modifiers apply only to
methods.
Synchronized methods cannot be abstract, but can be
final.
final instance variable cannot be reinitialized once
assigned a value.
Both transient and volatile modifiers apply only to
variables.
Method cannot be overridden to become more
private (or restrictive).
Question 13 & 14
void aMethod()
Super
Sub
Static Variables and Methods
Static members are not tied to any instance
of a class. It’s tied to the class.
Static variables have default values just like
instance variables.
All static variables will be initialized first.
To use a static method, there is no need to
instantiate a class, use the class name.
A static method cannot access a non-static
(instance) method or variable.
Static methods cannot be overridden.
Declaration Rules
A source file can have more than one non-
public class, but only one public class.
A class in a different package must be
referenced using the fully qualified name if
the import statement is not included.
Use fully qualified names when you have
different classes from different packages,
with the same class name.
Overloading vs. overriding methods.
Interface Declaration
The java.lang.Runnable interface has only
one interface method, public void run();
Only public and abstract are valid.
By default, all methods are public, abstract.
By default, all variables are public, static and
final.
A class implementing an interface can itself
be abstract.
A non-abstract implementing class provides
implementation for all methods from the
interface.