Page 319 - Computer_Science_F5
P. 319

Objects in Java
                works on Windows, Mac, Linux,
       Computer Science  (b)  Java programs are object-oriented:  items all around us, including humans,
                Raspberry Pi, etc.
                                                     In the actual world, there are a various
                they are constructed around objects  dogs, cars, and other  objects.  Each  of
                that have functions (methods) and  these items has a behavior and a state.
          FOR ONLINE READING ONLY
                data (attributes). This encourages  A dog has the  following  state:  name,
                maintainability and reusability of  breed,  color, and  behavior:  running,
                the code.                            barking, and tail-wagging. The software
           (c)  Robust and secure: Java is a popular   object  and a real-world item  share
                choice for enterprise applications   many similarities in their features.
                since it comes with built-in         Additionally, software  objects  possess
                memory management and security       a state and a behavior. Fields are used
                capabilities.                        to  store  a  software  object’s state,  and
           (d)  Large community: Java has a vast     methods are used to display its behavior.
                and active developer community,      Thus, in software development, methods
                that provides extensive resources and   work with an object’s internal  state
                support for learning and development.  and facilitate  communication  between
           (e)  Java is an object-oriented language   objects.
                that reduces development costs by    Classes in Java
                providing programs with a clear
                structure and enabling code reuse.   A class is a blueprint from which individual
           (f)  Programmers can easily transition    objects are created. The following is an
                                                     example of a class.
                from C++ and C# to Java because
                of its closeness to these languages.       public class Dog {

                                                             String breed;
           Objects and classes in Java
                                                                   int age;
           Object                                            String color;
           State and behavior are attributes of objects.           void barking(){
           A good example would be a dog’s color,
           breed, name and behaviors, such as eating,              }
           barking, and tail-wagging. Therefore, an        }
           object is an instance of a class.
                                                     A class can contain any of the following

           Class                                     variables:
           Behavior  or state  of the  object  can  be   (a)  Local variables: These are variables
           described by a class, which is a kind of      which are defined inside methods,
           template or blueprint.                        constructors or blocks. The variable

                                                 310
                                                                for Advanced Secondary Schools



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