OOP_2

Cards (50)

  • Objects
    • Software components that are created and destroyed dynamically during program execution
    • Instances of their class which defines their type
    • Have a set of variables (fields) representing their state
    • Behaviour determined by a set of instance methods which can be procedures that change their state or functions that return information about their state
    • Interact with each other by sending messages (instance method invocation)
  • Class
    • A compilation unit defining the state and behaviour of its objects
    • Contains declarations of fields (or instance variables), constructors, instance methods
    • Allows control of visibility of class members (private or public)
    • Can contain static variables, static methods, static or non-static local classes, static or non-static initialization blocks
  • Primitives of the Class Duration
    • Constructors with various arguments like number of seconds, minutes and seconds, hours, minutes, and seconds
    • Methods to add or subtract durations, multiply a duration (by a scalar), compare two durations (equality, inequalities), express a duration in days
  • Constructors for class Duration
    • Various arguments for different time units
  • Methods for class Duration
    • Adding or subtracting durations
    • Multiplying a duration
    • Comparing two durations
    • Expressing a duration in days, hours, minutes, and seconds
    • Converting a duration to days, hours, minutes
    • Getting a daily time offset from another moment
    • Representing a duration as a string
  • Primitives of the Class DailyTime
    • Two distinct constructors for setting hours, minutes, and seconds
    • Functions for testing equality, preceding or following another moment, and comparison
    • Addition or subtraction of a duration
    • Determining time elapsed between two instants
    • Representing a daily time as a string
  • The access to a field of an object is written object.fieldName
  • Creation of an object is expressed by an expression of the form new ClassName(actualArguments)
  • Access to a field of an object
    Written as object.fieldName
  • Creation of an object
    Expressed by an expression of the form new ClassName(actualArguments) where the actualArguments must match the formalArguments of an existing creator of the class
  • In Java, in most cases, destruction of objects is done implicitly
  • Object Organisation in Memory
  • Illustration of Aliases
    • Canvas canvas = new Canvas(300, 300);
    • Point point1 = new Point(100, 100);
    • Point point2 = new Point(100, 200);
    • Point point3 = point1;
    • Point point4 = point2.clone();
    • Circle circle1 = new Circle(90, point1, Color.RED);
    • Circle circle2 = new Circle(90, point2, Color.BLUE);
    • Circle circle3 = new Circle(60, point3, Color.YELLOW);
    • Circle circle4 = new Circle(60, point4, Color.GREEN);
    • point3.translate(100, 0);
    • point4.translate(100, 0);
    • circle1.draw(canvas);
    • circle2.draw(canvas);
    • circle3.draw(canvas);
    • circle4.draw(canvas);
  • Object types in Java
    • Represented as references to objects, which has several important consequences
    • The comparison operator == only compares the references
    • The assignment only makes a copy of the reference
    • Two variables can refer to the same object (alias)
  • Reminder: in Java, all variables (local, static, or instance), as well as all formal parameters (of methods and constructors), are typed
  • Sending a message to an object (or invoking an instance method)

    Written as object.methodName(arguments);
  • Java distinguishes
    • Primitive types (boolean, byte, char, double, float, int, long, short)
    • Object types
  • The garbage collector in Java is responsible for detecting objects that are no longer accessible and freeing up memory space occupied by these objects
  • The keyword null is used to represent a reference that doesn’t point to any object
  • To avoid NullPointerException, it is recommended to protect the access to a field or the invocation of a method by a conditional statement
  • According to Java documentation, when redefining equals(), hashCode() should also return the same values
  • To compare the equality of two objects based on field values, you must implement a function equals(other)
  • The == operator in Java compares two objects for identity and is not based on their field values
  • Using "this" as an argument of a method
    It can be used to return a new object
  • According to the Java documentation, when you redefine equals(), you have to make sure that hashCode() also returns the same values; otherwise, you have to redefine it too
  • Static Methods
    Methods not associated with an object, declared with the keyword static
  • The java.lang.Math class only defines static methods
  • Using "this" in a constructor
    Invoking another constructor of the same class
  • Using "this" to remove ambiguities
    Distinguishing a field from an argument
  • Static Variables (or Class Variables)
    Fields have values specific to each object, while static variables are shared between all objects of a class
  • Factory Methods
    Invoking a static method that returns a new object
  • Static Methods
    • myMethod
  • The keyword "this" designates the current object in a method
  • Factory Methods
    • inSeconds
    • inMinutes
    • inHours
  • Static Initialization Blocks can be defined with the form static {...} and are executed only once when the class is loaded into memory
  • Execution Order of Initialization Blocks
    Static initialization block, Instance initialization block, Constructor
  • Static Initialization Blocks are executed before the creation of each object
  • To create an application, the main difficulty is to design the types of objects (i.e. the classes)
  • Initialization block
    1. Static initialization: System.out.println("Static initialization."); staticVar = 5;
    2. Instance initialization: System.out.println("Instance initialization."); instanceVar = 7;
  • Constructor
    System.out.println("Constructor.");