Page 320 - Computer_Science_F5
P. 320

is declared and initialised within       methods to access the value of
                   the method and destroyed when the        various kinds of methods. In the
                   method has completed.                    above example, methods are such

              (b)  Instance variables: These  are           as barking(), hungry() and sleeping(). Chapter Five: Object oriented programming with Java
                   variables within a class but is outside
          FOR ONLINE READING ONLY
                   any method. They are initialised    Class and instance in Java
                   when the class is instantiated. They  A class is a group of items of the same
                   can be accessed from inside any  kind. As shown in Table 5.1, a class is
                   method, constructor or blocks of that  a three-compartment  box that  contains
                   specific class.                     data and operations.
              (c)  Class variables: These variables have
                   the static keyword applied and are   Table 5.1: Class structure
                   declared inside a class, independent   Class Name
                   of any methods.                      Static Attributes
                   A class can have any number of       Dynamic Behaviors



              Table 5.2 shows an example of two classes “Car” and “Fruit”


              Table 5.2: Example of class
               Name (Identifier)    Variables                   Methods
                                    (Static attributes)         (Dynamic behaviors)
               Car                  Make, model, color          startEngine(), accelerate(),
                                                                brake()
               Fruit                Name, color, taste          Ripen(),rote(),isSweet


              As shown from the table, a class can be viewed in three dimensions:
              1.  Name  or identity: This is used to identify the class.
              2.  Variables (sometimes called attribute, state, or field): these represent the static
                  attributes of the class.
              3.  Methods: These contain the class’s dynamic behaviors, functions, and operations.


              An instance, is an object built from a class (blueprint) which has all the characteristics
              defined in the class, but with specific values. For example, we can define a class
              called “Car” and create three instances of the class, “Car” for “Nissan”, “Toyota”
              and “Honda”.

              Table 5.3 shows three instances of the class Car, identified as “Nissan”, “Toyota”
              and “Honda”.

                                                    311
               Student’s Book  Form Five



     Computer Science Form 5.indd   311                                                     23/07/2024   12:34
   315   316   317   318   319   320   321   322   323   324   325