===== TODAY WE WILL ===== * announcements! * MORE on Java objects & inheritance for C++ programmers * [today's lab exercise will be answering clicker questions!] * prep for next class * reading for next week: Chapter 6 - Section 6.1 and 6.2 * Homework 2 is due by 11:59 pm tonight * watch for class emails as parts of Homework 3 become available * in Java, EVERY class that is not specifically a subclass of another class IS a subclass of the class Object * every Java variable that is an object is actually a reference (a Java variable that is a primitive type is not, it is just a primitive type value) * == tends to compare and return true if two objects are the SAME object in memory ...override the the Object equals method if you'd like to have a way to test if two objects are equivalent (have the same data field values, for example) ===== * how DO you make a subclass (other than a subclass of Object in Java? public class DesiredNewSubClass extends SuperClass { ... } and now DesiredNewSubClass is a subclass of SuperClass public class SimpleFrame extends JFrame { ... } and now SimpleFrame is a subclass of JFrame public class Tea extends Beverage { ... } etc.! * NOTE that Java does NOT support multiple inheritance (every class except Object has exactly one parent, BUT that parent has a parent, and so on) can have ONE parent, MANY ancestors; * Java interfaces actually REDUCE the need, in a practical sense, for multiple inheritance; * C++ has pass-by-value (default) and pass-by-reference if you speciy for passing parameters Java just has pass-by-value (but when you pass an object as a parameter, then, you are copying its, well, address) ^ you can change the state of what is at that address, but you can't make the object argument reference a DIFFERENT object in memory; * IF a Java class defines NO constructors, you get a default no-argument constructor. IF you explicitly define even one constructor, you don't get a default no-argument constructor. * We've seen final with a data field -- and a variable -- that's Java's way of making a named constant. * BUT a method can be declared as final, also (!!) If a METHOD is declared as final, NO subclass can override that method! * AND a CLASS can be declared as final, and if it is, you cannot make a subclass of that class. * You might recall that in C++ there is special syntax for a subclass' constructor to call its superclass' constructor to properly initialize inherited private data fields; * IN a constructor of that subclass, you use super like a function call, in the FIRST LINE of the subclass' constructor: super(arg1, arg2, ...); means: call the superclass' version of the constructor with those arguments, public class LoadedDie extends GameDie { private int factor; public LoadedDie(int desiredNumSides, int desiredFactor) { super(desiredNumSides); // calls superclass GameDie's constructor // with this argument this.factor = desiredFactor; } * ANOTHER similar use of super: used in front of a method call super.desiredMethod(arg, arg, ...) ...it means to call the superclass'/parent's version of that method * you can make a class abstract: public abstract class DesiredClass ...DesiredClass is NEVER to have instances, it is just intended to be a superclass for subclasses to be created from; and for each method within an abstract class that is labeled as abstract: public abstract RetType desiredMethod(...); ...then subclasses must implement those methods or themselves be abstract (AND declared accordingly) * QUICK note on Java types: Every Java class C has an associated type C consisting of all instances of class C and all of its subclasses and descendants So, every non-Object Java class has Object as an ancestor, so every Java class instance is also of type Object GameDie object? Is of type GamedDie and type Object. LoadedDie object? Is of type LoadedDie, GameDie, and Object