Date: 2008/5/5 (Sections 4.1-4.7) Intro How is your book reading coming along? How many have gone to see the TAs? INHERITANCE =========== Section 4.1 ----------- Relationships: IS-A: model this type of relationship with inheritance HAS-A: model with composition (use private data fields) <> Example: Person, Student, Employee Discuss: copy-and-paste VS. inheritance * code is redundant and garbage code is propagated * maintenance and versioning * relationships are lost: all are independent In Java, if a Student IS-A Person (Student extends Person) then new fields and methods may be added to and even overridden. However, fields and methods may not be removed as doing so would violate the relationship. Terminology: a derived class (or subclass) extends a base class (or superclass) transitivity Derived classes are new classes that have compatibility with the base class it was derived from * for instance, we can write functions that take a Person, which means that all classes derived from Person would be accepted * if we write a function that works on a List object, then this function would also work on all derived List classes (like ArrayList and LinkedList) 4.1.3 polymorphism, aka dynamic dispatch or late binding (or sometimes dynamic binding) Student s = new Student("Joe",26,"1 Main St","555-5555", 3.8); Employee e = new Employee("Bob",42,"4 Main St","555-1234",100000.0); Person p = null; if( getTodaysDay().equals("Tuesday") ) p=s; else p=e; System.out.println("Person is "+p.toString()); visibility rules (4.1.5) * private, public, protected (visible to derived classes and classes in the same package) Default constructor for a derived class (if you don't put it then this is implied): public Derived() { super(); } 4.1.7 final methods and classes (static, too) * final methods cannot be overridden * final classes cannot be extended (called leaf classes) * static methods are invariant requiring no instance of the object 4.1.8 overriding a method * signatures must match (or be "compatible" Java 5+) 4.1.9 type compatibility revisited ((Employee) p[1]).raise( 0.04 ); // p[1] is a student (VM would throw a ClassCastException) Array types keep track of the object it is allowed to store (ArrayStoreException) Section 4.2 designing hierarchies --------------------------------- ex. Shape hierarchy < Circle, Rect > Square, Triangle >> ... discuss methods that might be found in each polymorphism - ask people to define it * depending on the shape * area and perimeter 4.3 Multiple inheritance ------------------------ Q: Does Java allow multiple inheritance? 4.4 the interface ----------------- * an Interface is an abstract class that contains no implementation details 4.5 fundamental inheritance --------------------------- The Object class the hierarchy of exceptions Exceptions vs Errors - draw hierarchy (page 119) << show MyException.java and TemperatureException.java >> 4.6 generics ------------ * using Object for genericity public class IntCell { public int read(){ return storedValue;} public void write( int x ){ storedValue = x; } private int storedValue; } public class MemoryCell { public int read(){ return storeValue; } public void write( Object x ) { storeValue = x; } private Objecdt storedValue; } * Note: primitive data types cannot be used (e.g., byte, short, int, long, boolean) * autoboxing/unboxing << show BoxingDemo.java >> * adapters: changing an interface //(aka. wrapper functions) public class StorageCell { public Object get() { return m.read(); } public void put( Object x ) { return m.write(x); } private MemoryCell m = new MemoryCell(); } * using interface types for genericity << show FindMaxDemo.java >> 4.7 implementing generic components using java 5 generics --------------------------------------------------------- * by making the class generic, many of the errors that were previously only reported at runtime become compile-time errors << show GenericMemoryCell.java >> << show ShapeDemo.java >> change to type bounds - (skip for now) type erasure - generic classes are converted by the compiler to non-generic classes by a process known as type erasure Generics do not make the code faster. They do make the code more type-safe at compile time * restrictions on generics (see book, page 136)