Java Reference
In-Depth Information
￿ o1.compareTo(o2)
Returns a positive integer if o1 is bigger, 0 if they are equal,
and a negative number if o1 is smaller.
￿ o1.class
Returns the runtime type of o1 .
Returns true if the runtime type of o1 is the C class or one of
its direct or indirect subclasses.
￿
o1 instanceof C
The o1 object becomes a copy of the o2 object.
￿
o1 = o2.clone();
The C class contains a compareTo
method that can be used to compare objects of type C .
{
...
}⇒
￿
class C implements Comparable<C>
￿
Collections.sort(a);
Sorts an ArrayList of Comparable objects. The
compareTo method is used to compare the objects.
Sorts an array of Comparable objects. The compareTo method
is used to compare the objects.
￿
Arrays.sort(a);
￿ public int m() {
...
}⇒
The m method is accessible everywhere.
￿ int m() {
...
}⇒
The m method is accessible only within the package.
￿ protected int m() {
The m method is accessible within the package and in
subclasses (direct or indirect).
...
}⇒
￿ private int a;
The variable a is only accessible within the class. As a general
rule, define all non constant variables as private .
￿ private int m() {
...
}⇒
The m method is accessible within the class.
The C class is accessible from everywhere. Every Java file
must have exactly one public class that has exactly the same name as the name of
the file.
{
...
}⇒
￿
public class C
{
...
}⇒
The C class is only accessible within the package.
￿
class C
￿
final int m()
{
...
}⇒
The m method cannot be overridden.
￿
abstract void m();
Defines the m method as abstract. The method must be
overridden in the subclasses.
8.16 Important Points
1. Class composition and class inheritance are similar. However, there are few differences.
(a) They use different syntax.
(b) Every object in a subclass contains a reference to exactly one object of the
superclass that is not null . Conversely, an object can contain inside it 0 or more
non-null references to objects.
(c) Class composition does not support dynamic binding.
 
Search WWH ::




Custom Search