Writing complex computer programs

  • The material in the preceding chapters will allow you to solve many programming problems using selections (if, if-else), loops for, while), methods, and arrays.

  • However, these programming features are not sufficient for developing large-scale complex computer programs

  • Before about 1980, we use the modular programming technique to help build large-scale complex computer programs

  • Today's programming languages use the object concept to build large-scale complex computer programs

  • The style of programming using objects is called:

      • Object Oriented Programming (OOP)

Object Oriented Programming (OOP)

  • Object-oriented programming (OOP) involves programming using objects.

  • An object represents an entity in the real world that can be distinctly identified.

  • Examples of objects:

      • a student (each student can be uniquely identified)
      • a circle (each circle can be uniquely identified)
      • ...

  • An object has:

      • A unique identity (you can tell different object apart)
      • A state            (discussed next)
      • A behavior     (discussed next)

The state of objects

  • The state of an object (also known as its properties or attributes) is represented by data fields with their current values.

    Examples:

      • A circle object has a data field radius, which is the property that characterizes a circle.

      • A rectangle object has the data fields width and height, which are the properties that characterize a rectangle.

  • A Java class defines the properties (and behavior) of objects:

      • The instance variables (that we will study later) inside a class represents the state of an object

The behavior of objects

  • The behavior of an object (also known as its actions ) is defined by methods.

  • To invoke a method on an object is to "tell the object" to perform an action.

    Examples:

      • You can define methods named getArea() and getPerimeter() for circle objects.

      • Then:

          • invoking getArea() will instruct a circle to return its area
          • invoking getPerimeter() will instruct a circle to return its perimeter

  • A Java class defines the behavior (and properties) of objects:

      • The instance methods (that we will study later) inside a class define the behavior of an object

Classes

  • A Java class defines the (1) properties and (2) behaviors for objects:

      • The instance variables in a (Java) class store the current values of the properties of an object       (e.g.: radius of a circle object)

      • The instance methods in a (Java) class defines the behavior/actions of the object                               (e.g.: getArea() of a circle object)

  • A Java class is used in a Java program as a template (= description) to construct the object's data fields and methods:

      • When you create objects of a class, Java will use the class definition to allocate the instance variables for that object.

      • When you invoke some method on an object, Java will run the code in the method definition on the instance variables of the object

Objects and its class

  • An object is an instance created using a class definition.

      • (Remember that the class definition describes the properties (= instance variables) and behavior (= instance methods) of the object)

  • You can create as many instances (= objects) of a class as you need.

      • Each object will have its own properties (= instance variables)

      • But: all object will share the same actions (= instance methods)


  • Analogy to explain:     class and object:

      • Class = recipe to make apple pie

      • Object = some apple pie (e.g.: John's apply pie)