You are on page 1of 18

SIT (EI)

SCJP Certification Course


(5 March – 16 March)

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

 default (no modifier)

 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

Can be accessed if Class2 is a subclass of Class1


Abstract Class
 Abstract class is general to its subclasses,
certain implementation of the methods has to
be determined at the specific subclass level.
 Such methods are called abstract methods.
 An abstract class can never be
instantiated.
 A single abstract method in a class means the
whole class must be abstract.
 A class may be declared abstract even if it
has no abstract methods.
 The first concrete class to extend an abstract
class must implement all abstract methods.
Question 3
1. abstract class A {
2. abstract short m1() ; // abstract method
3. short m2() { return (short)20; }
4. }
5. abstract class B extends A {
6. short m1() { return (short)40; }
7. }
Class Modifiers
 Classes can have only public and default
access.
 There can only be one public class per
source file.
 The name of the file must match the name of
the public class.
 Classes can be modified with final, abstract
or strictfp (no need to know this).
 A final class cannot be subclassed.
 A class cannot be final and abstract at the
same time.
Question 4, 6
final
int X;
void Y( )

A final class cannot


be subclassed.
X=0 (ok to change this)
Y( )

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

A method may not be


overriden to be more
void aMethod() private (i.e. narrower/
/less privilege.

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.

You might also like