Home

This Chapter
-Introduction
-Java, the Language and the Technology
-An Overview of Object-Oriented Programming
-New Features in Java 5
-About This Book
-Downloading and Installing Java 5
-Downloading Program Examples and Answers

Table of Contents
-Introduction
-Chapter 1: Your First Taste of Java
-Chapter 2: Language Fundamentals
-Chapter 3: Statements
-Chapter 4: Objects and Classes
-Chapter 5: Core Classes
-Chapter 6: Inheritance
-Chapter 7: Error Handling
-Chapter 8: Numbers and Dates
-Chapter 9: Interfaces and Abstract Classes
-Chapter 10: Enums
-Chapter 11: The Collections Framework
-Chapter 12: Generics
-Chapter 13: Input Output
-Chapter 14: Nested and Inner Classes
-Chapter 15: Swing Basics
-Chapter 16: Swinging Higher
-Chapter 17: Polymorphism
-Chapter 18: Annotations
-Chapter 19: Internationalization
-Chapter 20: Applets
-Chapter 21: Java Networking
-Chapter 22: Java Database Connectivity
-Chapter 23: Java Threads
-Chapter 24: Security
-Chapter 25: Java Web Applications
-Chapter 26: JavaServer Pages
-Chapter 27: Javadoc
-Chapter 28: Application Deployment
-Appendix A: javac
-Appendix B: java
-Appendix C: jar
-Appendix D: NetBeans
-Appendix E: Eclipse

Previous
Next

 

An Overview of Object-Oriented Programming

Object-oriented programming (OOP) works by modeling applications on real-world objects. Three principles of OOP are encapsulation, inheritance, and polymorphism. They are given thorough coverage in this book.

The benefits of OOP are real. These are the reason why most modern programming languages, including Java, are object-oriented (OO). I can even cite two well-known examples of language transformation to support OOP: The C language evolved into C++ and Visual Basic was upgraded into Visual Basic.NET.

This section explains the benefits of OOP and provides an assessment of how easy or hard it is to learn OOP.

The Benefits of OOP

The benefits of OOP include easy code maintenance, code reuse, and extendibility. These benefits are presented in more detail below.

  1. Ease of maintenance. Modern software applications tend to be very large. Once upon a time, a “large” system comprised a few thousand lines of code. Now, even those consisting of one million lines are not considered that large. When a system gets larger, it starts giving its developers problems. Bjarne Stroustrup, the father of C++, once said something like this. A small program can be written in anything, anyhow. If you don’t quit easily, you'll make it work, at the end. But a large program is a different story. If you don't use techniques of “good programming,” new errors will emerge as fast as you fix the old ones.

    The reason for this is there is interdependency among different parts of a large program. When you change something in some part of the program, you may not realize how the change might affect other parts. OOP makes it easy to make applications modular, and modularity makes maintenance less of a headache. Modularity is inherent in OOP because a class, which is a template for objects, is a module by itself. A good design should allow a class to contain similar functionality and related data. An important and related term that is used often in OOP is coupling, which means the degree of interaction between two modules. Loosely coupling among parts make code reuse—another benefit of OOP—easier to achieve.

  2. Reusability. Reusability means that code that has previously been written can be reused by the code writer and others who need the same functionality provided by the original code. It is not surprising, then, that an OOP language often comes with a set of ready-to-use libraries. In the case of Java, the language is accompanied by hundreds of class libraries or Application Programming Interfaces (APIs) that have been carefully designed and tested. It is also easy to write and distribute your own library. Support for reusability in a programming platform is very attractive, because it shortens the time taken to develop an application.

    One of the main challenges to class reusability is creating good documentation for the class library. How fast can a programmer find a class that provides the functionality he/she is looking for? Is it faster to find such a class or write a new one from scratch? Fortunately, Java core and extended APIs come with extensive documentation.

    Reusability does not only apply to the coding phase through the reuse of classes and other types; when designing an application in an OO system, solutions to OO design problems can also be reused. These solutions are called design patterns. To make it easier to refer to each solution, ach pattern is given a name. The early catalog of reusable design pattern can be found in the classic book Design Patterns: Elements of Reusable Object-Oriented Software, by Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides.

  3. Extendibility. Every application is unique. It has its own requirements and specifications. In terms of reusability, sometimes you cannot find an existing class that provides the exact functionality that your application requires. However, you will probably find one or two that provide part of the functionality. Extendibility means that you can still use those classes by extending them so that they suit your need. You still save time, because you don’t have to write code from scratch.

    In OOP, extendibility is achieved through inheritance. You can extend an existing class, add some methods or data to it, or change the behavior of methods you don't like. If you know the basic functionality that will be used in many cases, but you don’t want your class to provide very specific functions, you can provide a generic class that can be extended later to provide functionality specific to an application.

Is OOP Hard?

Java programmers need to master OOP. As it happens, it does make a difference if you have had programmed using a procedural language, such as C or Pascal. In the light of this, there is bad news and good news.

First the bad news.

Researchers have been debating the best way to teach OOP at school; some argue that it is best to teach procedural programming before OOP is introduced. In many curricula, we see that a OOP course can be taken when a student is nearing the final year of his/her university term.

More recent studies, however, argue that someone with procedural programming skill thinks in a paradigm very different from how OO programmers view and try to solve problems. When this person needs to learn OOP, the greatest struggle he/she faces is having to go through a paradigm shift. It is said that it takes six to 18 months to switch your mindset from procedural to object-oriented paradigms. Another study shows that students who have not learned procedural programming do not find OOP that difficult.

Now the good news.

Java qualifies as one of the easiest OOP languages to learn. For example, you do not need to worry about pointers, don’t have to spend precious time solving memory leaks caused by forgetting to destroy unused objects, etc. On top of that, Java comes with very comprehensive class libraries with relatively very few bugs in their early versions. Once you know the nuts and bolts of OOP, programming with Java is really easy.

Previous
Next