Open All | Close All
  • Brief review of two older problems
    • Determining when two Circles overlap: discussion
      • The reasoning
        • You are in a helicopter of radius r1
        • Your friend is in a helicopter of radius r2
        • What's the closest you can get so your rotor blades don't hit each other?
        • The answer to the question above determines whether your circles overlap or not.
      • Reducing one case to another
        • If the static method is provided
          • class Circle {
              // other class or instance members, constructors come here  
              static boolean overlap(Circle a, Circle b) {
                // the code for this method is provided but secret 
              } 
              boolean overlaps(Circle other) {
                return Circle.overlap(this, other);
              } 
            }
        • If the instance method is provided
          • class Circle {
              // other class or instance members, constructors come here  
              static boolean overlap(Circle a, Circle b) {
                return a.overlaps(b); 
              } 
              boolean overlaps(Circle other) {
                // the code for this method is provided but secret
              } 
            }
    • Determining if two Rectangles overlap: discussion
      • The reasoning
        • When do two rectangles overlap?
          • If two rectangles overlap there is at least one point P in both.
          • If there is such a point it will be within the width and height of both.
          • As a result the projection on the axes of the two rectangles are non-empty.
          • Two rectangles overlap when both of these projections intersect.
          • In other words they overlap when they overlap both on the x axis and the y axis.
          • Here's a picture see if it's relevant or not.
        • When do two rectangles not overlap?
          • If one rectangle is completely to the north of the other.
          • If one rectangle is completely to the west of the other.
          • If one rectangle is completely to the south of the other.
          • If one rectangle is completely to the west of the other.
          • Two rectangles do not overlap when at least one of these conditions holds.
          • Note that more than one condition (but at most two) can be true at the same time.
          • Two rectangles don't overlap when all four conditions above evaluate to false.
      • Reducing one case to another
        • If the static method is provided
          • Isn't this exactly the same as the corresponding case for Circles?
        • If the instance method is provided
          • Isn't this exactly the same as the corresponding case for Circles?
  • The Class Extension Mechanism
    • Definition
      • Every class has a blueprint.
      • Every blueprint models something
      • Each model is the set of instance members in the blueprint
      • Models can be built in stages
      • The resulting model is the set union of features
      • Examples: one, two (but see below too).
    • Consequences
      • Inheritance
        • Horse = { talk() }
        • Unicorn = Horse U { sing() } = { talk(), sing() }
        • Any Unicorn object has a talk() instance method by inheritance from Horse
      • Polymorphism
        • What it means: being of more than one type
          • Every Unicorn is a Horse
          • Every variable is like a remote controller, with buttons
          • The number and types of buttons precisely match the blueprint
          • A Horse remote controller works for a Unicorn object
          • When we think of a Unicorn object as the Horse it is we ignore the horn (the extra features)
        • Common mistake #1
          • All Unicorns are Horses but Horses can't sing.
          • Using a Horse remote for a Unicorn object is fine, but the remote makes sing() inaccessible
        • Common mistake #2
          • Not all Horses are Unicorns (reformulated below)
          • Using a Unicorn remote controller for a Horse object is not allowed
      • Dynamic Method Lookup
        • Given Horse = { talk() } we understand Unicorn = Horse U { sing() }
        • What is the meaning of Unicorn = { sing(), talk() } in this context?
        • The answer is that the second talk() overrides the first.
        • From the outside of the object (via remote controller) only the second is available.
        • From the inside they're both available as this.talk() and super.talk()
        • Here's a very basic example.
      • Constructor Chaining
        • The Rules
          • When you define a class, Java guarantees that the class's constructor method is called whenever an instance of that class is created. It also guarantees that the constructor is called when an instance of any subclass is created. In order to guarantee this second point, Java must ensure that every constructor method calls its superclass constructor method. If the first statement in a constructor is not an explicit call to a constructor of the superclass with the super keyword, then Java implicitly inserts the call super() -- that is, it calls the superclass constructor with no arguments. If the superclass does not have a constructor that takes no arguments, this causes a compilation error.

            There is one exception to the rule that Java invokes super() implicitly if you do not do so explicitly. If the first line of a constructor, C1, uses the this() syntax to invoke another constructor, C2, of the class, Java relies on C2 to invoke the superclass constructor, and does not insert a call to super() into C1. Of course, if C2 itself uses this() to invoke a third constructor, C2 does not call super() either, but somewhere along the chain, a constructor either explicitly or implicitly invokes the superclass constructor, which is what is required.

        • Example #1
          • bash-3.2$ cat Horse.java
            class Horse {
              String name;
              Horse(String name) {
                this.name = name;
              }
              void talk() {
                System.out.println(this + "says: Howdy.");
              }
            }
            bash-3.2$ javac Horse.java
            bash-3.2$ cat Unicorn.java
            class Unicorn extends Horse {
              void sing() {
                System.out.println("That's me in the (uni)corner..."); // R.E.M.
              }
            }
            bash-3.2$ javac Unicorn.java
            Unicorn.java:1: cannot find symbol
            symbol  : constructor Horse()
            location: class Horse
            class Unicorn extends Horse {
            ^
            1 error
            bash-3.2$
        • Example #2
          • bash-3.2$ cat Horse.java
            class Horse {
              String name;
              Horse(String name) {
                this.name = name;
              }
              void talk() {
                System.out.println(this + "says: Howdy.");
              }
            }
            bash-3.2$ javac Horse.java
            bash-3.2$ cat Unicorn.java
            class Unicorn extends Horse {
              Unicorn(String name) {
                this.name = name;
              }
              void sing() {
                System.out.println("That's me in the (uni)corner..."); // R.E.M.
              }
            }
            bash-3.2$ javac Unicorn.java
            Unicorn.java:2: cannot find symbol
            symbol  : constructor Horse()
            location: class Horse
              Unicorn(String name) {
                                   ^
            1 error
            bash-3.2$
        • Example #3
          • bash-3.2$ cat Horse.java
            class Horse {
              String name;
              Horse(String name) {
                this.name = name;
              }
              void talk() {
                System.out.println(this + "says: Howdy.");
              }
            }
            bash-3.2$ javac Horse.java
            bash-3.2$ cat Unicorn.java
            class Unicorn extends Horse {
              Unicorn(String name) {
                super(name);
              }
              void sing() {
                System.out.println("That's me in the (uni)corner..."); // R.E.M.
              }
            }
            bash-3.2$ javac Unicorn.java
            bash-3.2$
            
  • Abstract Classes
    • An abstract class is a class that models a type that can't be instantiated.
      abstract class Horse {
        String name; 
        Horse(String name) {
          this.name = name; 
        } 
        void talk() {
          System.out.println("Howdy, I'm " + name); 
        } 
      }
      
      class Unicorn extends Horse {
        Unicorn(String name) {
          super(name); 
        } 
        void talk() {
          super.talk(); 
          System.out.println("I also speak a bit of French."); 
        }
      }
      
      class Nineteen {
        public static void main(String[] args) {
          Horse a = new Unicorn("Oliver"); // totally fine 
          a.talk(); 
          Horse b = new Horse("Stanley"); // does not work
        } 
      }
      We'll have many more examples in the notes below and tomorrow.
    • Better example
  • Interfaces
    • An interface is an element of pure design.
      interface Horse {
        void talk(); 
      }
      
      class Unicorn implements Horse {
        String name; 
        Unicorn(String name) {
          this.name = name; // there can't be constructor chaining in this case
        } 
        public void talk() {
          System.out.println("Bonjour, je suis " + name); 
        }
      }
      
      class Eighteen {
        public static void main(String[] args) {
          Horse a = new Unicorn("Francois"); 
          a.talk(); // an interface is even closer to a remote controller metaphor
        } 
      }
    • Better example.
  • Examples
    • Extending a JFrame
      • Identify inheritance, polymorphism, dynamic method lookup in the code below:
        import javax.swing.*;
        import java.awt.*;
        
        class One extends JFrame {
          One() {
            this.setSize(300, 500);
            this.setVisible(true);
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          }
          public static void main(String[] args) {
            JFrame a = new JFrame();
            a.setSize(300, 500);
            a.setVisible(true);
            a.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            One b = new One();
          }
          public void paint(Graphics g) {
            super.paint(g);
            System.out.println("Ouch!");
          }
        }
    • The Comparable interface
      • Generic, customizable interface shown below in an example:
        class Student implements Comparable<Student> {
          String name;
          int age;
          public String toString() {
            return this.name + "(" + this.age + ")";
          }
          Student(String name, int age) {
            this.name = name;
            this.age = age;
          }
          public int compareTo(Student other) {
            if (this.age < other.age) return -1;
            else if (this.age > other.age) return 1;
            else return this.name.compareTo(other.name);
          }
          public static void main(String[] args) {
            Student a, b, c;
            a = new Student("Lauren", 12);
            b = new Student("Larry", 12);
            c = new Student("Landon", 13);
            System.out.println( a + " ahead of " + b + (a.compareTo(b) > 0 ? " no" : ( a.compareTo(b) < 0 ? " yes" : " maybe")));
            System.out.println( a + " ahead of " + c + (a.compareTo(c) > 0 ? " no" : ( a.compareTo(c) < 0 ? " yes" : " maybe")));
            System.out.println( c + " ahead of " + b + (c.compareTo(b) > 0 ? " no" : ( c.compareTo(b) < 0 ? " yes" : " maybe")));
          }
        }
    • Sorting arrays and ArrayLists of objects.
      • Two ways of storing and sorting objects in Java.
        import java.util.*;
        
        class Example {
          public static void main(String[] args) {
            Student[] a = new Student[5];
            a[0] = new Student("Lauren", 12);
            a[1] = new Student("Laura", 12);
            a[2] = new Student("Larry", 12);
            a[3] = new Student("Leslie", 11);
            a[4] = new Student("Lance", 13);
        
            System.out.println(Arrays.toString(a));
            Arrays.sort(a);
            System.out.println(Arrays.toString(a));
        
            ArrayList b = new ArrayList();
            b.add(new Student("Arthur", 86));
            b.add(new Student("Aaron", 86));
            b.add(new Student("Betty", 8));
            b.add(new Student("Samuel", 12));
            b.add(new Student("Samantha", 12));
            b.add(new Student("Sam", 12));
        
            System.out.println(b);
            Collections.sort(b);
            System.out.println(b);
        
          }
        }
  • In-class Notes

Updated by © Adrian German for C212/A592 -- 6W2 Summer 2017