Page 357 - Computer_Science_F5
P. 357

ClassName() {
       Computer Science      Exercise 5.2            (b) Importance of using constructors
                                                        // constructor body
             1.  Create a class named MyFamily
                                                        }
                 utilising keywords “public” and
                 “class”.
          FOR ONLINE READING ONLY
             2.  Use the class MyFamily, create the
                 object called “OurGarden” using         (i)  Guaranteed initialisation:
                                                              Constructors guarantee that
                 the keyword “New”.                           objects are created with their
             3.  Write codes that will use the object         properties properly initialised,
                 “OurGarden” to access and print              preventing potential errors or
                 the value of any variable declared           unexpected behavior caused by
                 with integer data type in the class          uninitialised properties.
                 MyFamily.
                                                        (ii)  Flexibility: By creating
                                                              constructors with different
           Constructors and destructors in                    arguments,  you  have  the
           Java                                               flexibility to customize the initial

           In Java, constructors and destructors              state of your objects according
           play crucial roles in OOP. Constructors            to specific requirements or
           are  special  methods  used  to  initialise        preferences.
           objects and are called  automatically
           upon object  instantiation. Java does     (c) Creating constructors
           not  have  destructors  like  C++,  relying
           instead on garbage collection to manage   When  you create  a constructor  in
           memory by cleaning up unused objects.     Java, adhere  to  the  following  steps as
                                                     demonstrated in Program Example 5.18.
           Constructors in Java                      Steps to develop constructor:
           Constructors are special  methods in a
                                                      st
           class that are automatically called when   1  Step: Declare a method with the same
           objects are created from that class. They   name as the class.
           initialise objects when they are created.   2  Step: Do not specify both return type
                                                      nd
           They have the same name as the class
           and does not have a return type. They     and void for the constructor.
           ensure objects are in a valid state from   3  Step: Provide parameters to the
                                                      rd
           the beginning.                            constructor to initialise the object with

                                                     specific values and perform any required
           (a) Syntax for a constructor              initialisation  or setup for the  object
           Any constructor have a name and           within a constructor.
           follows a syntax as shown below:

                                                 348
                                                                for Advanced Secondary Schools



     Computer Science Form 5.indd   348                                                     23/07/2024   12:34
   352   353   354   355   356   357   358   359   360   361   362