WindowEvent defines behavior for (works with) the Close option on the frame menu.JFrame defines the top-level window including the window title and frame menu.ExampleProgram defines the program data and methods to work on that data.Each class has its own function and purpose as summarized below, and where appropriate, the classes are defined to work with objects of another class. The program code to place the text in a label to display it in a user interface uses a number of cooperating classes. This section changes the example program to display the concatenated character string in a JLabel component in a user interface to further illustrate the concepts of well-defined class boundaries and object cooperation. In the previous section, you saw how the System, String, and StringBuffer objects cooperated to print a concatenated character string to the command line. Well-Defined Boundaries and CooperationĬlass definitions must allow objects to cooperate during execution. The System class is never instantiated by the ExampleProgram class because it contains only static variables and methods, and therefore, cannot be instantiated by a program, but it is instantiated behind the scenes by the Java virtual machine 1 (VM). In the last example, four different String objects are created for the concatenation operation, text object, text2 object, and a String object created behind the scenes from the " that uses classes and objects" character string passed to the ncat method.Īlso, because String objects cannot be edited, the method converts the String objects to StringBuffer (editable) string objects to do the concatenation.īesides the String object, there is an instance of the ExampleProgram.java class in memory as well. There can be any number of objects of a given class in memory at any one time. I'm a simple Program that uses classes and objectsĪn instance is an executable copy of a class. This way, you can call a method such as the ncat method that adds text to the original string. If you want access to the String methods, you can rewrite the example program to create a String object as follows. During execution, the Java platform creates a String object from the character string passed to the call, but your program cannot call any of the String class methods because it did not instantiate the String object. In the example, you do not see an explicit use of the String class, but in the Java language, a character string can be used anywhere a method expects to receive a String object. In contrast, the class defines character strings. The class used in the example defines such things as standard input, output, and error streams, and access to system properties. ("I'm a simple Program") Ĭlasses in the Java platform API libraries define a set of objects that share a common structure and behavior. To help you gain a deeper understanding of object-oriented programming and its benefits, this lesson presents a very brief overview of object-oriented concepts and terminology as they relate to some of the example code presented in this tutorial. But mostly, you have been doing object-oriented programming without really thinking about it.Īnd that is one of the great things about the Java programming language. You already know a little about object-oriented programming because after working the example programs in Java Programming Language Basics, Part 1 and Part 2, you are somewhat familiar with the object-oriented concepts of class, object, instance, and inheritance plus the access levels public and private. And, if the Java programming language is your first experience with an object-oriented language, you are probably wondering what all the talk is about. You have probably heard a lot of talk about object-oriented programming.
0 Comments
Leave a Reply. |