OOP Tutorials
01/08/2009 17:40
Tutorials & Code Camps
Lesson 8: Object-Oriented Programming
|
|
|
|
|
|
|
|
||||||||||
You have probably heard a lot of talk about object-oriented programming. 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 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 And that is one of the great things about the Java programming language. It is inherently object oriented. 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.
Object-Oriented Programming DefinedObject-oriented programming is a method of programming based on a hierarchy of classes, and well-defined and cooperating objects. ClassesA class is a structure that defines the data and the methods to work on that data. When you write programs in the Java language, all program data is wrapped in a class, whether it is a class you write or a class you use from the Java platform API libraries. The
Classes in the Java platform API libraries define a set of objects that share a common structure and behavior. The In the example, you do not see an explicit use of the If you want access to the
I'm a simple Program I'm a simple Program that uses classes and objects ObjectsAn instance is an executable copy of a class. Another name for instance is object. There can be any number of objects of a given class in memory at any one time. In the last example, four different Also, because Besides the The Well-Defined Boundaries and CooperationClass definitions must allow objects to cooperate during execution. In the previous section, you saw how the This section changes the example program to display the concatenated character string in a The program code to place the text in a label to display it in a user interface uses a number of cooperating classes. 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.
While each class has its own specific purpose, they all work together to create the simple user interface you see here.
InheritanceOne object-oriented concept that helps objects work together is inheritance. Inheritance defines relationships among classes in an object-oriented language. In the Java programming language, all classes descend from The following diagram shows the class hierarchy as it descends from As you move down the hierarchy, each class adds its own set of class-specific fields and methods to what it inherits from its superclass or superclasses. The PolymorphismAnother way objects work together is to define methods that take other objects as parameters. You get even more cooperation and efficiency when the objects are united by a common superclass. All classes in the Java programming language have an inheritance relationship. For example, if you define a method that takes a You saw an example of polymorphism in Part 2, Lesson 5: Collections where a collection object can contain any type of object as long as it descends from
Data Access LevelsAnother way classes work together is through access level controls. Classes, and their fields and methods have access levels to specify how they can be used by other objects during execution, While cooperation among objects is desirable, there are times when you will want to explicitly control access, and specifying access levels is the way to gain that control. When you do not specify an access level, the default access level is in effect. ClassesBy default, a class can be used only by instances of other classes in the same package. A class can be declared Here is an applet class declared to have a public class DbaAppl extends Applet implements ActionListener {
Without the class DbaAppl extends Applet implements ActionListener {
Also, in Part 2, Lesson 6: Internationalization the server classes are made public so client classes can access them. Fields and MethodsFields and methods can be declared
private Connection c;
Your Own ClassesWhen you use the Java API library classes, they have already been designed with the above concepts in mind. They all descend from For example, you will not find a But what about when you write your own classes? How can you be sure your classes have well-defined boundaries, cooperate, and make use of inheritance? One way is to look at the functions you need a program to perform and separate them into distinct modules where each functional module is defined by its own class or group of classes. Well-Defined and Cooperating ClassesLooking at the RMIClient2 class from the Part 2, Lesson 5: Collections lesson, you can see it performs the following functions: Get data, display data, store customer IDs, print customer IDs, and reset the display. Getting data, displaying the data, and resetting the display are closely related and easily form a functional module. But in a larger program with more data processing, the storing and printing of customer IDs could be expanded to store and print a wider range of data. In such a case, it would make sense to have a separate class for storing data, and another class for printing it in various forms. You could, for example, have a class that defines how to store customer IDs, and tracks the number of apples, peaches, and pears sold during the year. You could also have another class that defines report printing. It could access the stored data to print reports on apples, peaches, and pears sold by the month, per customer, or throughout a given season. Making application code modular by separating out functional units makes it easier to update and maintain the source code. When you change a class, as long as you did not change any part of its public interface, you only have to recompile that one class. InheritanceDeciding what classes your program needs means separating functions into modules, but making your code more efficient and easier to maintain means looking for common functions where you can use inheritance. If you need to write a class that has functionality similar to a class in the Java API libraries, it makes sense to extend that API library class and use its methods rather than write everything from scratch. The RMIClient2 class from the Part 2, Lesson 5: Collections lesson extends Likewise, if you want to add customized behavior to an existing class, you can extend that class and add the functionality you want. For example, you might want to create your own Access LevelsYou should always keep access levels in mind when you declare classes, fields, and methods. Consider which objects really need access to the data, and use packages and access levels to protect your application data from all other objects executing in the system. Most object-oriented applications do not allow other objects to access their fields directly by declaring them private. Then they make their methods protected, public, or package as needed and allow other objects to manipulate their private data by calling the methods only. This way, you can update your class by changing a field definition and the corresponding method implementation, but other objects that access that data do not need to be changed because their interface to the data (the method signature) has not changed. Program ImprovementsProgram improvements for this lesson include setting the correct access levels and organizing a program into functional units. Setting Access LevelsIt is always best to restrict access as much as possible. Going back to Part 2, Lesson 7: Packages and JAR Files, the server classes had to be made At that time, no access level was specified for the other classes and fields so they are all A good exercise would be to go back to the client classes and give the classes, fields, and methods an access level so they are not accessed inappropriately by other objects. Here are possible solutions for the RMIClient1.java and RMIClient2.java client programs. Can you explain why the |
|||||||||||||||||
———
Back