Java CheatSheet
Java CheatSheet
by l.cuvillon
Access Modifier
Has access? Same Class Same Package Subclass another package Universe
private yes
(package- yes yes
private)
protected yes yes yes
public yes yes yes yes
● Class visibility: private (useless) or protected are both forbidden.
● An inner class can/should be private or protected.
C++: a class has no access modifier.
● Attribute and Method: default modifier is (package-private).
C++: default modifier is private.
● If constructor is private: direct object creation unavailable; available through a public static
method (see singleton class). If protected: object creation is only available inside the current
package, or outside the package through the definition of a subclass (super()).
Demo: _modifierAccess
Naming Convention
var variable, reference
method() methods
ObjectClass Object Class
●Name of a class starts with an upper case.
●The filename of a source file is the name
of the public class inside, if any. Many
class can be defined in one file, but only
one can be public.
Java essentials
by l.cuvillon
trainer . c a l l ( pluto );
t r a i n e r . c a l l ( grosMinet ) ;
Downcast to subtype:
if ( a n i m a l 1 i n s t a n c e o f Dog )
{
Dog dogy = ( Dog ) a n i m a l 1 ;
dogy . b a r k ( ) ; //now , v a l i d call
}
// Note : a n i m a l 1 . b a r k ( ) i n v a l i d ,
// c l a s s A n i m a l h a s no b a r k ( )