Accessing a class means:
|
||
| To have access the class members must visible. | ||
| Modifiers | Modifiers control a class or member visibility
Modifiers also control access to declared class, methods, or variable. |
|
| These are the visibility modifiers (access):
public, protected, private, none package. |
||
| They control the protection of and access of an element.
There are special modifiers that control behavior: final, abstract, synchronized, native, strictfp, transient, volatile or static, etc. |
||
| Classes | Class are declaration template for a potential object.
Classes are instantiated to become an object. |
|
| Class Modifiers | public | Visible and access by any class (default or external package) in a program. |
| final | Class can’t be changed. Can not be extended. | |
| none (default, friendly) | Class are visible only to current package.
Other classes in same package access this class. |
|
| abstract | Means class can not be instantiated.
A class with abstract method must declare that it is abstract.
Class has abstract methods and must be extended to be implemented. |
|
| Exam Tip | Make sure when extending a class from an external package, the base class is public. | |
| Exam Tip | When extending an abstract class the derived class must implement the method or declare the class itself to be abstract. | |
| Constructor | Invoked when an object is instantiated (using new operator). | |
| Initialize variable within a class or assign values. | ||
| Must have the same name as the class. | ||
| No return type is specified. The return type is implicitly this. | ||
| When a object does not define a constructor the default constructor is automatically inherited from object. This constructor has no argument. That’s if the class does not define its own constructor. | ||
| Constructor can have a list of exceptions. | ||
| Constructors can be overloaded.
The methods parameter must be different. |
||
| super() | In extended classes, the super() method can be used to call the base class constructor. | |
| Exam Tip | super() must be the first statement called in the constructor of the subclass.
This applies also for this(). |
|
| Exam Tip | Only other constructor can call other constructors.
This applies also for this(). |
|
| Exam Tip | this() and super() cannot be called in the same constructor. Only one can be called. | |
| Exam Tip | Constructor cannot be final, abstract, synchronized, native or static. | |
| Exam Tip | Constructor can be public, protected or private. | |
| Exam Tip | Class cannot be private, protected or static.
If declared so, a compiler error will occur. |
|
| Exam Tip | public class Derive extends Demo {
private X, Y; public Derive(int x, int y) { X=x; Y=y; } public Derive( int x) { super(x); } }
These super constructor should be defined: Demo(int c) and Demo() called from the Derive(int x, inty) constructor. |
|
| Exam Tip | abstract class TraderNames extends EmployeeNames{
public static String[] Traders = {“Sally”, “Nicom”, “Ziggy”}; }
Although the class is abstract with no abstract method, the code will compile. We can declare a class abstract and have not abstract method. However, if instantiated a runtime error will occur. |
|
| extends | Subclassing a class to create a subclass.
The parent (base) class is the class being extended. |
|
| A subclass is more specific than the superclass.
Superclass is more general or less specific. |
||
| Object class is the highest-level class. | ||
| If no super class is specified, the object is the super class by default. It extends Object. | ||
| Exam Tip | Subclasses can only access data members that are declared public or protected. | |
| super | Access the parent class variable and methods. | |
| this | Access the current class variable and methods.
Object can pass a reference to itself. Can call it’s own constructor. |
|
| Interface | Define an interface that may contain constants and method declarations.
interface SeverCode { public static final int MAXCONNNECTION = 23; public void connect(); } |
|
| Used when a class must be treated like more than one type of object | ||
| They may have inheritance like classes.
Interface may extend other interface. |
||
| Classes can implement more than one interface. | ||
| Interface cannot be instantiated. | ||
| Interfaces cannot implement other interfaces. | ||
| Implicitly interfaces are abstract. | ||
| All methods in the interface must be implemented in the class | ||
| There are no base interface as the Object class. | ||
| Exam Tip | Implicitly interface variable are public static and final.
We cannot implement another interface. |
|
| Interface cannot be protected, private, final or static.
Interfaces can be public or package. |
||
| Members | Class can have data or method members. | |
| Reference variable (instance members) | Object scope. Are available once the class is instantiated. | |
| A reference variable is the memory location that holds the object, or it contains null. | ||
| Static members | Instance methods or variables cannot be called from a static method. | |
| Static variables can be accessed from instance methods. | ||
| Primitive variable | Hold the actual value itself. | |
| Static variable (class members) | Belong to the class. Don’t have to be instantiated. | |
| Local variable (automatic variable) | Local variable are declared inside a method. | |
| They can only be modified by the member modifiers below. | ||
| Local variables are not automatically initialized as data members. | ||
| Exam Tip | Their values must be initialized before usage.
The compiler will look for paths where the variable is not initialized. |
|
| Local variables cannot be public, private, protected, static, final, transient or volatile.
No modifiers allowed. |
||
| Exam Tip | Local variables are available in the scope they are defined.
Example: Inside a try block. Inside the for loop. |
|
| Member
Modifiers |
public member
private member
protected members (friendly access)
none (default) |
Class, methods or variables visible are accessed by any class (callers) in the immediate/external packages.
Methods or variables accessible to class only.
Methods or variables accessible to classes (callers) immediate package or derived classes {subclasses} in immediate/external packages.
Methods or variables accessible to immediate package. |
| Special Modifiers | abstract | A method or class cannot be instantiated, till subclassed modified and implemented. Subclass is a must. |
| Modifies only methods. Cannot be used on variable. | ||
| native | Indicate a method is written in a platform-dependent language, such as C. | |
| Modifies only methods
Cannot be used on variable. |
||
| final | For variable, their values cannot be changed – their constant. Value must be initialized in the same statement. | |
| For methods, method cannot be overridden. | ||
| For classes, classes cannot be extended.
They cannot be used as parent class. |
||
| The only modifier that can be used inside a local scope.
In inner class of methods, any local variable must be final. |
||
| Exam Tip | public abstract int getPrice();
The method is used as class marker This code will compile. |
|
| Exam Tip | Be careful with final members.
Making the member variable final, does not mean a local method can use the local variables name.
Ex: class { final int mname = 20; void method() { int mname = this.mname; } }
The local name is being assigned, not the member variable which is final. |
|
| static | Makes a method or variable belong to an entire class.
Not the class instance (there is no this or object).
No this is available for the class. |
|
| A static variable exist once for the class. | ||
| synchronized | A synchronized method can only be access by one thread at a time.
A Thread entering this method obtains a lock on the object, which prevents other thread from entering any synchronized code for the object. |
|
| Modifies only methods
Cannot be used on variable. |
||
| transient | Variable are not to have their data written to an ObjectStream. | |
| volatile | Compiler optimizes and takes shortcuts to keep local copies of variables when using Threads. Volatile keyword does not optimize, but uses the data member copy. Turn off data member modification.
Used in thread to allow data member to be changed instead of an optimized local variable. |
|
| Exam Tip | A class can have a:
protected static void method(); |
|
| Exam Tip | Be careful and make sure method return type comes before the method name. | |
| Exam Tip | Make sure a return type is specified in the method declaration. | |
| Exam Tip | The order of Member and Special modifier are not important.
Compiler places restriction such as a method cannot be final and abstract. |
|
| Methods | Method give class behavior or functionality. | |
| Access Modifiers | public data – Accessible to all objects.
private data – Accessible to method only.
protected – Access to these callers: super class, subclass (in/out package) and callers in the same package can access the class members.
none – Access by current package only. |
|
| It’s best to use (accessor) methods -get’s.
It’s best to use (mutator) methods -set’s. |
||
| Scope:
Automatic Variables (methods) |
||
| Object (instance variables) | ||
| static: Creates class scope | ||
| main() | Main method is used in java programs. | |
| Signature | public static void main(String arg[]) {} or,
static public void main(String arg[]) {} |
|
| arg | Arguments start at index 0. | |
| Using main() | ||
| Exam Tip | java myprogram var1 var2
var1 is argument 0. The myprogram is not var1. |
|
| Exam Tip | public void main(String arg[]); will compile, but will not start a java program. | |
| Return Type | A method must declare the data type it returns. | |
| Objects | Object type can return its type or its subclasses.
Not its base class. |
|
| void | When a method returns no value void is returned. | |
| Primitive | boolean declaration must return boolean.
char declaration must return char.
Integer return type can only return integer type (int, byte, short, char, long). The return type can be promoted to a higher bit type. See below. An integer declaration can not return a floating type.
Floating type can return integers. Double can return a float and integers. Not the other way. See below. |
|
| Return type promotion of numeric data types (top is highest order):
double float long int char short byte |
||
| Static Initializer | static { …}
Creates a special block of code when the class is loaded. Used to initialize complex data structures and variables without the constructor. This takes place before the constructor. |
|
| Exam Tip | A static method cannot access instance variable, unless initialized | |
| Exam Tip | Instance methods can access static vars. | |