CSC1351 9/6/2011 Where are we?
Polymorphism // Use dynamic lookup to allow different data types to be manipulated // with a uniform interface. public class FlameThrower implements Weapon { public void inflict(Target t) { t.fireDamage(6); } public class Sword implements Weapon { public void inflict(Target t) { t.cuttingDamage(3); }... Weapon w = new Sword(); w.inflict(target); // Dynamic lookup of Sword's inflict method w = new FlameThrower(); w.inflict(target); // Method comes from object type, not variable type
Vocabulary super class sub class accessor mutator polymorphism callback observer pattern side effect constant object
Types Eight primitive types int - signed, 4 bytes double - 8 bytes float - 4 bytes short - signed, 2 bytes long - signed 8 bytes char - unsigned, 2 bytes byte - signed, 1 byte boolean - two states
Primitive vs. Object Primitives passed by value automatic variables are allocated off the stack wrappers for all primitives are in java.lang wrappers provide parse methods Objects passed by reference automatic variables are allocated with new and come from the heap
Keywords - Know what they mean interface class abstract public private package import static final
Types Objects All classes extend class Object Objects all have public String toString() String/StringBuffer public char charAt(int pos) int length() String substring(int start,int end) int indexOf(char)
Types Arrays allocated with new final int field named length Enums A special type of object to represent a finite set of values
Conventions All class names are capitalized First letters of words are capitalized, e.g. GiantRat, BankAccount, etc. All method names start with lower case constants (final static) are in all caps and use the underscore to separate words, e.g. MIN_VALUE
Inheritance You can only extend one class Abstract classes have some undefined methods Interfaces have only undefined methods, or constants
Inner Classes Static Inner Classes – declared inside another class – does not have access to fields of enclosing class Non-static Inner Classes – declared inside another class – has access to fields of enclosing class Anonymous Inner Classes – a non-static inner class without a name