CS 235 - Week 3 Lab

*   today: pulling material mostly from Chapters 4, 5
    of the course text

    *   next Monday: pull from selected parts of
        Chapters 6, 7, 8

*   HIGHLY recommend Chapter 4 of the course text
    for object-oriented intro IN GENERAL!
    (as well as in Java)

*   remember: every class in Java is either a
    subclass or a descendant of the Object class;

    ...all classes thus inherit Object's methods,
    which the class can override if desired;

    e.g., every class inherit's Object's toString
    method, BUT it might override it to provide a
    "prettier" or more appropriate version for
    itself --

    for example, I might add the following overridden
    toString method to GameDie:

    /**
     * make a prettier String depiction of a GameDie
     * 
     * @return a String depiction including number of
     *         rolls and number of sides
     */
    
    public String toString()
    {
        return "GameDie[" + numSides + " sides, rolled "
               + numRolls + " times]";
    }
* now, in DrJava Interactions window:
    > GameDie die = new GameDie(4);
    > die.toString()
    "GameDie[4 sides, rolled 0 times]"
    > 
* C++ has the concept of copy constructors used for objects that need to be able to automatically make copies of themselves (e.g., linked structures...) In Java, remember that there's an overall superclass named Object -- every Java class is either a subclass of Object or a descendant of Object * one of the methods a class inherits from Object is clone() clone() - to get a complete copy of an object (rather than a reference to that object) * FOR EXAMPLE (p. 155 of Core Java 9th ed.)
        private Date hireDate;

        ...

        public Date getHireDate()
        {
            // return a COPY of the data field hireDate

            return hireDate.clone();
        }
p. 155: "As a rule of thumb, always use clone whenever you need to return a copy of a mutable data field." * to write a subclass in Java: <visibility> <newClassname> extends <superClassName>
    public class Beverage
    {
        ...
    }
    public class Tea extends Beverage
    {
        ...
    }
    public class HotTea extends Tea
    {
        ...
    }
* Tea is a subclass of Beverage Beverage is the superclass of Tea (Beverage is a subclass of Object, Object is the superclass of Beverage, and Object is an ancestor of Tea, and Tea is a descendant of Object...) * HotTea is a subclass of Tea, Tea is the superclass of HotTea (HotTea is a descendant of Beverage and Object; Beverage and Object are ancestors of HotTea) * can a class be a (direct) subclass of more than 1 class in Java? that is, does Java support MULTIPLE INHERITANCE? NO -- that's a difference between Java and (C++ and Python) * this is not as bad as it sounds because Java's INTERFACES reduce the need (in most cases?) for multiple inheritance... more on interfaces later on; * Java VISIBILITY * private - visible to the class only (and to all class instances...?) * course style: data fields that aren't named constants we want the world to see WILL be declared as private! * methods can be public or private based on what's appropriate for the method! * public - visible to the world * protected - visible to the package and all subclasses * use with CAUTION -- since other programmers may be subclassing your class later... * visible to the package - the UNFORTUNATE default... class George ...George has default visibility; George can be accesses by any other classes in the same package (including the default package, or the current directory) * parameter passing in Java C++: pass-by-value, pass-by-reference Java: ONLY has pass-by-value ...BUT since object variables are references, you pass object arguments by copying the value of the reference -- (you can change the argument via its methods...) BUT you can't change the object the argument is referencing...) * more uses of final!! * final used for a METHOD: ...means a subclass CANNOT override that method! * final used for a CLASS: ...means one cannot create a subclass of that class; * keyword super: * instances of a subclass inherit the methods and datafields of its superclass ...BUT private datafields are still private; * usually, the public setters and getters are enough to help with this; * But what about the subclass's constructors? ...it needs help to set to inherited private data fields! As the FIRST line in a subclass's constructor, super(arg1, arg2, ...); ...this calls the superclass's constructor with these arguments
        public class LoadedDie extends GameDie
        {
            private int favoredValue;

            public LoadedDie(int desiredNumSides,
                             int desiredFavoredValue)
            {
                // calls GameDie's constructor with
		//   desiredNumSides

                super(desiredNumSides);
                favoredValue = desiredFavoredValue;
            }

            ...
* second version of super: used in front of a method name to call the superclass' version of that method what if we have an Employee class, with method getSalary, and subclass Manager would like to use superclass Employee's version of getSalary as part of its modified getSalary?
        public class Manager extends Employee
        {
            private double bonus;

            ...

            public double getSalary()
            {
                 double baseSalary = super.getSalary();
                 return baseSalary + bonus;
            }

        ...
* abstract public abstract class Blah ...this is for a class Blah that is NEVER to have instances, JUST subclasses (it is just intended to be a superclass for other classes) * it is meant to be a type only, if you will; * if a method within an abstract class is labeled as abstract: public abstract String getDescription(); ...that means subclasses MUST implement this method! (OR themselves be declared as abstract!) * types in Java... int val; // what is val's type? int String name; // what is name's type? String ...BUT: name has MORE than just String as its type -- Every Java class C has an associated type C consisting of all instances of: * class C and * all of its descendants ...so, name is of type String AND type Object; ...an instance declared to be an instance of class Tea is also of type Tea, type Beverage, and type Object