Content deleted Content added
→Interfaces and classes: rework, expand, rename to "Interfaces" |
→Interfaces: expand, rename to "Objects" |
||
Line 292:
*<code>LABEL2: { statements; }</code>
==
===Data members===
*<code>final</code> - value cannot be changed. Must be initialized.
*<code>static</code> - belongs to the class, rather than to an instance of the class.
*<code>sychronized</code> - only one object or class can access the data at a time (causes the object to be locked).
*<code>transient</code> - not a part of the persistent state of an object. Should not be saved and later restored.
*<code>volatile</code> - informs the compiler that it may be accessed by separate threads asynchronously.
====Constants====
*<code>final</code> - use to declare a data member as a constant value.
====Global constants====
class MyClass {
public static final int x = 10;
}
====Static Initializers====
static int i;
static { i = 20; } // a block of code
*Use after the declaration of the static data.
===Methods===
*<code>abstract</code> - the method is undefined in the class, and must be defined by any subclass that will be instantiated.
*<code>final</code> - the method cannot be redefined in a subclass (non-dynamic). The compiler may expand the method (similar to an inline function) if the method is small enough.
*<code>native</code> - the method links to native machine-dependent code. Declared without a body. Cannot be abstract.
*<code>static</code> - belongs to the class, rather than to an instance of the class.
*<code>sychronized</code> - only one object or class can access the method at a time (causes the class to be locked).
*Can't override an abstract method with another abstract method.
*A private method can't be abstract.
*All methods are implicitly virtual, and can be redefined in a child class, unless they are declared as <code>final</code>.
====Constructors====
*When possible, the object should be a valid, meaningful object once it is constructed, as opposed to relying on an Init method.
*Copy constructor: accepts an object of it's own type as a parameter and copies the data members.
*The implicit default constructor is only available if no explicit constructors are defined.
*Constructors can be overloaded.
*The first statement in a subclass constructor may invoke a superclass constructor: super(...);
*If there is no explicit call to the superclass constructor, the default superclass constructor will be called.
*The first statement in a subclass constructor may invoke another subclass constructor: this(...);
====Finalize method====
*Performs cleanup when the object goes out of scope; useful for closing resources.
*The finalize method is called before the garbage collector frees the object; the object is not immediately freed afterwards.
*There is no guarantee when the finalize method will be called, or the order in which the finalize method will be called for multiple objects.
*If the interpreter exits without performing garbage collection, the OS may free the objects, in which case the finalize method doesn't get called.
*The finalize method should always be protected.
protected void finalize() { ... }
===Classes===
====Class access====
*<code>public</code> - accessible outside the package in which it's defined.
*default - accessible only within the package in which it's defined.
====Member access====
*<code>public</code> - accessible by any class.
*<code>protected</code> - accessible within the class and by any derived class.
*<code>private</code> - accessible only within the class.
*default - accessible by any class within the package.
*Can't override a public method to protected or private (can't make it more private than it already is).
*Can override a protected method to public.
*Can't override a private or final method (causes a private method to be redefined in the subclass).
====Inheritance====
class ChildClass extends ParentClass { ... } // ChildClass inherits from ParentClass
*The default parent of a class is class Object.
*A class can only extend a single parent class (no multiple inheritance).
====Abstract classes====
abstract class ClassA {
abstract ... functName(...);
...
}
*A class is abstract if it contains one or more abstract methods.
*Abstract classes cannot be instantiated.
*Derived classes can be instantiated if they define a method for any abstract methods inherited.
====Scope====
*<code>this</code> - reference to the current subclass (assumed by default) (i.e. this.someMethod()).
*<code>super</code> - reference to the parent class (i.e. super.someMethod()).
===Interfaces===
An interface is like a class with no implementation details. Its only purpose is to define how a set of classes will be used. Classes that implement a common interface can be used somewhat interchangeably. Interfaces also help to enforce the concept of abstraction--hiding the details of how a class is implemented.
Line 301 ⟶ 389:
Java interfaces behave much like the concept of the [[Objective-C]] protocol.
====Implementing====
A class can implement one or more interfaces, in addition to extending another class.
Line 354 ⟶ 442:
Because any objects in the above array are guaranteed to have the <code>delete()</code> method, the <code>deleteAll()</code> method needn't differentiate between the <code>Fred</code> objects or any other <code>Deleteable</code> objects.
====Extending====
An interface can extend another interface.
Line 378 ⟶ 466:
}
</pre>
===Misc===
*<code>Clone()</code> method allocates and copies a new object. Returns the object as class <code>Object</code>.
==Input/Output==
|