Java Is Object-Oriented from the Ground Up

We first got a look at object-oriented programming when working with JavaScript, but that was only a quick glance. Object-oriented programming is integral to every aspect of Java. For example, take a look at the application we just saw:

 public class ch10_01  {     public static void main(String[] args)     {         System.out.println("Welcome to Java");     } } 

Note the very first line, public class ch10_01 , which defines a class named ch10_01 . Our whole program is based on that class because, unlike in JavaScript, every line of code you write in Java has to be contained in a class (or an interface, which is a more generalized form of classes that we'll see in the next chapter). When Java runs this application, it creates an object of this class and gives that object control. So while you can optionally use objects in JavaScript, there's no avoiding them in Java.

I'll take a closer look at the idea behind classes now because we'll have to understand more about them than we did when discussing JavaScript. Object-oriented programming is really just another technique to let you implement that famous programming dictum: "Divide and conquer."

Here's the idea: You encapsulate data and functions into objects, which makes objects into self-contained units. The data inside an object can be purely internal to the object, in which case it's called private data, or it can be accessible externally, in which case it's called public data.

The functions built into an object can also be either purely private or public. In fact, ideally , the object should interact with the rest of the program only through a well-defined interface, as created by its public functions. As we saw in Chapter 6, "Understanding JavaScript," functions that are part of classes or objects are called methods .

Object-oriented programming was first developed to let programmers handle larger programs by breaking them into functional units that can be easily conceptualized. As you know, you can already break your code into functions. Object-oriented programming goes a step farther than that, letting you create objects that can contain not just one function, but many, as well as internal data items. When you encapsulate part of your code into an object, it lets you think of that part of the program in an easily conceptualized way, and that's the motivation behind object-oriented programming.

For example, consider a carbut consider it not as a sleek new automobile, but as an assemblage of pipes, wires, valves , switches, gasoline, and all the various parts that make it work. Now imagine that you are responsible for handling everything that the car usually does itself, such as pumping the fuel, igniting the fuel, transmitting power to the wheels, regulating electrical power, and more. A device requiring such attention would be impossible to drive. Now imagine all those functions back where they should be, internal to the car and interacting with each other automatically as needed when you step on the gas. You think of the result simply as a caran easily imagined (and used) single concept. All you have to do is turn it on and step on the gas.

That's the idea behind encapsulation: You can turn a complex system that requires a lot of attention into an object that handles the details internally when you pass control to it. If the first dictum of object-oriented programming is "Divide and conquer,"the second is surely "Out of sight, out of mind."

In Java, object-oriented programming revolves around a few key concepts: classes, data members , inheritance, methods, and objects. This list summarizes these terms:

  • Class A class can be thought of as a template from which you create objects. The definition of the class includes the formal specifications for the class and any data and methods in it.

  • Data members The data members of a class are the variables that are part of an object. You store the data that the object uses in its data members.

  • Inheritance This is the process of deriving one class, called the derived class, from another, the base class, and being able to make use of the base class's methods in the derived class.

  • Method A method is a function built into an object. Methods can be part of classes ( class methods ) or objects ( object methods ), as we'll see in this chapter.

  • Object An object is an instance of a classwhat you create with classes. You can think of a class as the type of an object. When you've created an object, you can customize it by storing data in it (which you can't do with a class).

All these constructs are important to object-oriented programming, and we'll get more details on each of them in this chapter as we see how to create our own classes.



Real World XML
Real World XML (2nd Edition)
ISBN: 0735712867
EAN: 2147483647
Year: 2005
Pages: 440
Authors: Steve Holzner

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net