CIS 368/568 Object Oriented Design and Programming MIDTERM REVIEW HIGHLIGHTS (all page references are to Java in a Nutshell, 5th edition) 1. Brief History of Java; James Gosling from Sun, 1995, Java 1.0, great for the Internet for providing dynamic content to the Web; Java 1.1 in 1997 introduced new event model and inner classes and doubled speed of the of the Java 1.0 interpreter, many more new classes; Java 1.2 in Dec. 1998 introduced Swing with more GUI components; Java 1.3 released May 8, 2000 (referred to as Java 2, ver 1.3) had great performance gains, better web development for enterprise applications, new methods for classes involving RMI (remote method invocation), multimedia enhancements (e.g. new audio file formats), improved caching control for applets, Swing enhancements (e.g. improved performance of tables with large number of columns), security enhancements. Java 1.4, in spring of 2002 had 2991 classes and interfaces and 135 packages, containing many network API enhancements, new Swing GUI components; also added the assert statement to the language. Java 1.5 (called Java 5.0), 2004-05: 3562 classes and interfaces and 166 packages (some additions to the API make for easier I/O with Scanner class, printf method in PrintStream; String.format method; many language changes: include generic types, enumerated types, annotations, varargs methods, for/in statement). 2. Sun's description of Java: SODIRSAPHMD (know these; know what they mean). 3. The JVM; Java is a byte-compiled interpreted language; Java performance issues (because it is interpreted, and because it is an OO language). 4. Designing a Java program; software development method (SDM) – specification, analysis, design, implementation, testing, debugging, deployment, maintenance, and retirement. The analysis phase focuses on analyzing the requirements document – goal here is to decide “what to do”. In the design phase, it should be specified “how to” construct the system. Class Diagrams and Message Passing Diagrams. OO Concepts – abstraction, encapsulation, inheritance, polymorphism. 5. Class, field, method naming conventions. 6.. Some “unique” Java operators: instanceof, non-short circuit logical and/or (&, |), unsigned right shift (>>>) (pg. 29). 7. The new operator (creating an object – bottom of pg. 106, top of pg. 107; “this” reference). 8. Order of evaluation of operands in Java (e.g. pg. 32). 9. Primitive types and reference types (reference types include class, array, and interface types; (in 5.0, enumerated types and annotation types are reference types, also)). (pg. 82-83) 10. Class java.lang.Object (very important – it is at the top of the class hierarchy chain; every instance of any class “isa” Object) 11. Eight primitive types, sizes and default values (pg. 22); promotion order; assignment: type1 = type2; casting needed if type2 higher than type1(pg. 28 table 2-3). 12. java.lang.String class (note can instantiate without using new); '+' operator; methods: equals, charAt, compareTo, concat, copyValueOf, getBytes, getChars, indexOf, lastIndexOf, length, replace, substring, valueOf (heavily overloaded, static method) (pgs. 493-495). 13. Passing arguments to a method: 2 rules: i) primitive types are passed by value; ii) references are passed by value (pg. 86). 14. The signature of a method (name, number of parameters, type of parameters, order of parameters). 15. Method overloading. 16. Arrays; length attribute of an array; size of an array can be declared at runtime; arrays of references. The for/in statement (pgs. 50-51). 17. Array assignment; copying arrays using System.arraycopy method (pg. 79); cloning of arrays (pgs. 84-85). 18. Multidimensional arrays; "non-rectangular" arrays (pg. 81). 19. java.lang. StringBuffer class: constructors, methods: length, capacity, setLength, ensureCapacity, charAt, setCharAt, getChars, reverse, append, insert, delete; in 5.0 StringBuilder class has same methods (but they are not synchronized) (pg. 495-499). 20. Wrapper classes; Java has one for each of the 8 primitive types; the wrapper classes are in the java.lang package; one arg. constructor using primitive type, one arg. constructor using string type, the toString method, the method "primitive_type"Value (e.g. x.doubleValue() , where x is of type Double), the parse static methods (parseByte, parseShort, parseInt, parseLong, parseFloat, parseDouble); note Byte, Double, Float, Integer, Long, Short have java.lang.Number (an abstract class) as their direct superclass (which has java.lang.Object as its direct superclass); wrapper classes Boolean and Character have Object as their direct superclass. Boxing and Unboxing conversions (pgs. 88-89). 21. java.util.Vector implements a growable array of Objects; in Vector, the no-arg. constructor gives capacity of 10 and doubles capacity when more space is needed; the 2 arg. constructor (both int. args) allow you to specify initial capacity and capacity increment; methods: addElement and add (same functionality; add is in the List interface that is implemented by Vector), setElementAt, insertElementAt, removeElementAt; method elements returns an Enumeration; method iterator returns an Iterator (the iterator method is inherited from AbstractList, an abstract class that implements the List interface; method listIterator returns a ListIterator; the listIterator method is inherited from AbstractList; ListIterator is an interface that extends Iterator; Vector is similar to the ArrayList class, except that methods of Vector are synchronized, which makes them thread safe but increases the overhead of calling them. As of Java 5.0, Vector is a generic class: (usage e.g. Vector<Animal> anVec = new Vector<Animal>();) 22. Class, method, and field declarations (chapter 3); private, default, protected, and public access modifiers used on members of a class--what do they mean? abstract class (e.g. java.lang.Number) , final class (e.g. java.lang.System, java.lang.Math) ; final field, final method; static field (a "class" field), static method (a "class" method); note static methods are not passed a "this" reference, therefore in a static method we may not refer to any instance fields or invoke any instance methods; recall System.out.println() example (pg. 106)-dissect each component. 23. Object creation using new operator and constructors; constructors perform initialization of data members; default values for data members (zero, false, null); a default, no-argument constructor is supplied by Java if your class does not define any constructors; the "this" reference implicitly passed to a constructor; may define many constructors for a single class (pgs. 106-107). 24. Static initializers (pg. 110) invoked automatically by system when class is loaded; used to initialize "class" fields; static {.....}; note no args., no return value, no name, simply use the keyword static followed by the block that initializes the static fields. 25. Object destruction; done automatically by garbage collector (gc); gc is run as a low priority thread in the background; gc cleans up objects which are no longer referenced; a programmer can facilitate garbage collection by assigning null to a reference, and by making explicit call (System.gc()); finalize method guaranteed to be called just before gc collects the object; finalize method: no args, no overloading, a void return type. (pgs. 111-113) 26. Inheritance and Polymorphism (114-123); see handout examples, and quiz examples); class Object at top of class hierarchy; “isa” relationship; a class may "extend" only one class (i.e. it may have only one direct superclass; it may have several indirect superclasses up the chain to Object); a class may "implement" more than one interface; while a class inherits all fields and methods from its superclass, private members are not directly accessible to the subclass; OO concepts and terminology, package and import statement; composition (using an object as an instance field of another class). Casting of references is needed when a “narrowing conversion” takes place (“downcasting”) – e.g. If o is declared to be of type Object, and s is declared to be of type String, then in the assignment, s = (String) o, casting was necessary to avoid compile-time error; (note: if at runtime, the runtime type of o is not String (i.e. if o is not pointing to a String object), then a runtime error (“ClassCastException”) will occur). By runtime type is meant the type of constructor that was used in creation of the object. For example: Object obj = new StringBuffer(); //no cast necessary – widening conversion; runtime type of obj is StringBuffer 27. Shadowed fields (fields having same name) of superclass may be accessed by using super. fieldname in the subclass, or by using a cast; note that super.super.fieldname is not valid Java syntax, however, casting may be used to reference fields in any of a class's indirect superclasses (pg. 119). 28. Method overriding (pgs. 120-123; method in subclass has same signature as method in superclass and same return type (in 5.0, the return type can be a subclass of the overridden method’s return type): method in superclass that is overridden may be accessed in subclass using super.method() notation; dynamic method lookup is used by Java to implement polymorphism (see handouts and quiz 2); note dynamic method lookup is not done on final methods (also note that private and static methods are implicitly final); note that fields are bound at compile time (based on field's data type), overridden methods are bound at runtime (based on the runtime type of object that is being referenced); instanceof operator; getClass (method of Object) returns the runtime class of an object (return type is of type Class); getName() method from Class returns class name; (see handouts); note that static methods can be shadowed but not overridden. 29. Three uses of super (super.fieldName, super.methodName(), super(arg. list)); if use super(arg.list), it must appear as first statement in constructor, and is a call to the constructor of the super class; recall that the Java compiler will insert super() as the first line in a constructor, unless there is already a super or this call as the first line. This results in constructor chaining up to class Object. 30. Abstract classes--cannot be instantiated; abstract methods have no implementation; rules for abstract classes (note abstract class need not have any abstract methods; if it has one or more abstract methods, the class must be declared abstract); note that static, final, or private methods can't be abstract (why?); a final class cannot have any abstract methods (why?) (pgs. 128-130). 31. Interfaces (pgs. 135-140; similar to classes in way they are defined, but different since can't be instantiated (note an interface does not have a constructor)); a marker interface has no methods; all methods of an interface are implicitly public and abstract; the only types of fields an interface may have are final, static fields; when a (non-abstract) class implements an interface, it is like signing a contract (that says the class will give implementations for all the methods in the interface; if not, the class would be an abstract class); note an abstract class that implements an interface may give implementations for some of the interface's methods, but not all (non-abstract classes that extend that abstract class must give implementations for all the remaining methods); an interface may be declared to extend one or more other interfaces; java.util.ListIterator interface (hasNext, next, and previous methods) and java.util.Enumeration interface (hasMoreElements, nextElement methods); java.lang.Cloneable interface (a “marker” interface); java.lang.Comparable interface; java.util.Comparator interface. Interfaces vs. Abstract Classes design issues – pgs. 138-139. 32. Exception Handling (see handouts); try/catch/finally statement; class java.lang.Throwable, Exception, Error, RuntimeException classes; throw statement; checked and unchecked exceptions; printStackTrace, toString, getMessage methods of class Throwable, defining your own Exception classes, rethrowing exceptions (see handouts) (pgs. 58-60; 68)