modifier |
CLASS |
METHOD |
PROPERTY (variable) |
public (A) |
Class may be accessed by anyone, anywhere member is accessible anywhere the class is |
Method may be accessed by anyone, anywhere |
Property may be accessed by anyone, anywhere |
protected (A) |
member is accessible within the defining package and within subclasses |
may be accessed by methods within the same class or (subclasses anywhere) |
may be accessed by methods within the same class or subclasses |
blank (A) ("friendly") |
may only be accessed within the package |
may be accessed by methods within the same package only |
may be accessed by methods within the same package only |
private (A) |
member is only accessible to the class that defines it |
may be accessed by methods within the same class only |
may be accessed by methods within the same class only |
static |
used to declare a toplevel class as opposed to an inner class |
cannot be instantiated, are called by classname.method,can only access static variables |
only one instance of the variable exists |
abstract |
cannot be instantiated, must be a superclass, used whenever one or more methods are abstract |
Method is defined but contains no implementation code (implementation code is included in the subclass). If a method is abstract then the entire class must be abstract. |
not applicable |
synchronized |
not applicable |
acquire a lock on the class for static methods acquires a lock on the instance for non-static classes |
not applicable |
volatile |
not applicable |
not applicable |
field should not be serialized |
native |
not applicable |
not applicable |
field maybe accessed by unsynchronized threads |
final |
Class cannot be inherited |
Method cannot be overridden |
makes the variable a constant |
(A) static abstract synchronized volatile native final
Abstract classes and interfaces allow us to implement polymorphism and multiple inheritance (Object Oriented Programming concepts) in Java. By referring to the abstract class instead of the specific subclasses we can easily call a method that will do something different for each subclass but retain the same coupling. The interface allows us to "implement" more than one superclass without creating conflicting methods or variables. The following table illustrates the differences between abstract classes and interfaces.
|
type |
Notes |
methods |
variables |
|
abstract class |
Cannot be instantiated, subclasses must provide body for all abstract methods (else
subclass must be declared abstract) |
May have an abstract method (a method that does not have a body). If an abstract method exists, class is/remains abstract |
no restrictions on variables |
|
interface |
Cannot be instantiated, all methods must be overridden by the implementing class |
All methods must be abstract |
all variables, if present, must be static and final |
|
|
|
|
|