Java Quick Reference
  Language Fundamentals
  Operators and Assignments
  Flow Control and Exceptions
  Declarations and Access Control
  Garbage Collection
  Overloading and Overriding
  Threads
  The java.lang Package
  The java.util Package
  The java.awt Package
  The java.io Package
  References
  Miscellaneous Notes
  Tips & Traps
  Mock Exams

Overloading, Overriding, Runtime Types and Object Orientation - is A vs Has A

  • is a defines a direct relationship between a superclass and a subclass
  • has a identifies a relationship in which one object contains another object (defined by field variables)

Examples

  • A circle is a shape that has a center point and a radius. (JJ pg 138)
    public class Circle extends Shape { // a circle is a shape
        Point  center;                  // a circle has a point
        double radius;                  // a circle has a radius
    }
  • Define a class hierarchy for the following classes (BB pg14):
    1. An Employee class that maintains an employee number.
    2. A Full-time employee class that maintains an employee number, hours worked per week and calculates it's own pay using a salary() method.
    3. A Retired employee class that maintains an employee number, the number of years worked, and calculates it's own pay using a salary() method.
    public class Employee {     // an employee
        long   id;              // has an id, and
        String status;          // a status
    }

    abstract class EmployeeStatus extends Employee {
        abstract double salary();
    }
    
    // fulltime is a status
    class FullTime extends EmployeeStatus { 
        double hrs;        
        double salary(){
             return hrs * 60.0;
        }
    }
    
    // retired is a status
    class Retired extends EmployeeStatus {  
        int years;
        double salary() {
            return 0;
        }
    }    
  • Create classes for 2DShape, Circle, Square and Point. Points have an (x,y) location. Circles have an (x,y) location and a radius. Squares have an (x,y) location and a length. (BB pg17)
    class Point {       // a point
        int x;          // has an x-location, and
        int y;          // a y-location
    }

    class 2DShape {     // all 2DShapes
        Point p;        // have a point
    }
    
    class Circle extends 2DShape {  // a circle is a 2DShape
        double radius;              // and has a radius
    }
    
    class Square extends 2DShape {  // a circle is a 2DShape
        double length;              // and has length
    }
Encapsulation Polymorphism isA/hasA Overloading Overriding Field Variables
Initialization Top-level Classes Inner Classes Static Nested Classes Local Classes Anonymous Classes