--------------------------------------------------------- A Crash Course on Object-oriented Programming in Java --------------------------------------------------------- Overview of the topics covered: 1. Notion of class and objects (instances of a class) 2. Members of a class (fields, methods, inner classes, interfaces) 3. Construction of an object 4. Methods (static vs non-static, varargs) 5. Self parameter (this) 6. Method overloading 7. Extending a class 8. Notion of overriding 9. Type conversion 10. Access control between base and derived class 11. Controlling derivation of a class 12. Abstract class 13. Single vs. Multiple Inheritance Ch 2-3 from [Arnold05] -------------------------------------------------------- CLASS -------------------------------------------------------- Class - separate the notion of "what" from "how" - describe the contract between designer and client - define the type that all objects of the class are supposed to honor - class [extends ] (implements )* '{' member* '}' - members: fields, methods, nested classes & nested interfaces (C#: properties, indexer, event declarations) Class Modifier - Give certain properties to classes - Access modifiers: public, private (default within the declaring package) - Overriding modifiers: final (may not be subclasses) - Instantiation modifiers: abstract (incomplete, may not be instantiated). - strict floating point (strictfp) Q: Can a class be both final and abstract? Explain. --------------------------------------------------------- FIELDS - use to store state associated with the class or an instance of the class - ['='expression] - field modifiers - annotations - access modifiers: public, private, protected - scope modifiers: static (one per class vs. one per instance) - transient and volatile - If a field is not initialized, it has a default value. (Differentiate between accessing static vs non-static fields) Final field - value cannot be changed once it has been assigned Q: How is a final field different from constants? Explain. ---------------------------------------------------------- ACCESS CONTROL - motivation: "the more you know, the more you have to explain" - understanding, debugging, and maintaining programs - default, protected, private, public ---------------------------------------------------------- CONSTRUCTION - language construct "new" - allocate space for classes's fields Q: How many times is space allocated for static fields? - may run VM's garbage collection - may throw an OutofMemoryError - difference between C++: no explicit delete operation - brief overview of garbage collection Q: How does it help with programming? - [modifiers] '{' ... '}' - can invoke constructors of the same class using this() Example: Point class public class Point{ int x, y; public Point(int x, int y){ this.x = x; this.y = y; } public Point(){ this(0,0); } } this() must be the first executable statement. Why? - if there are no constructors, a default no-argument constructor is automatically generated that assigns default value to all fields. Exercise: What is a copy constructor? Exercise: What is an initialization block? Exercise: What is the order in which initialization blocks are executed? Exercise: What is a static initialization block? How often is it executed in the life-time of the Java application? Can a static initialization block refer to a non-static member? --------------------------------------------------------- METHODS - two parts: method header and body - [modifiers] '(' ')' '{' ...'}'