Subclass has access to public members of parent class
Public methods and properties are accessible in subclass
Intermediate level of protection between public and private
Protected members accessible by
subclass members
Classes in the same package
You cannot reduce accessibility of methods in subclasses
Meaning of extends
Parent-granted privileges cannot be revoked/reduced by child
There are two situations in which this approach is not sufficient.
Class data or static property
Class method or static method
You want to have only a single piece of storage for a particular field
Regardless of how many objects of that class are created
Or even if no objects are created
Example:
Altima.designYear
Class data = static properties
If you need a method that isn’t associated with any particular object of this class.
You need a method that you can call even if no objects are created
Example:
Counting how many objects we created so far
Class methods = static methods
Static methods can access only static properties
Static methods are actually class operations
If a method uses only static fields, make it static!
Static methods are accessible via classes
double d = Double.parseDouble("12");
String s = String.valueOf(12);
public static void main(String[] args)
Static properties and methods are invoked on class name
Static properties are class data, not object data
Constructors are created for initializing object data
How to initialize class data?
Two ways:
Inline values
Static block
Once per class
Static variable declaration
Static block
section 20.6: static member