Java
  Home arrow Java arrow Page 5 - Getting Started with Java
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Moblin 
JMSL Numerical Library 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVA

Getting Started with Java
By: Sams Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 10
    2006-05-04

    Table of Contents:
  • Getting Started with Java
  • Introduction to Java
  • Selecting a Development Tool
  • Object-Oriented Programming
  • Objects and Classes
  • Attributes and Behavior

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Getting Started with Java - Objects and Classes


    (Page 5 of 6 )

    Object-oriented programming is modeled on the observation that in the physical world, objects are made up of many kinds of smaller objects.

    However, the capability to combine objects is only one aspect of object-oriented programming. Another important feature is the use of classes.

    A class is a template used to create an object. Every object created from the same class has similar, if not identical, features.

    Classes embody all features of a particular set of objects. When you write a program in an object-oriented language, you don't define individual objects. Instead, you define classes used to create those objects.

    For example, you could create a Modem class that describes the features of all computer telephone modems. Most modems have the following common features:

    • They connect to a computer's serial port.

    • They send and receive information.

    • They dial phone numbers.

    The Modem class serves as an abstract model for the concept of a modem. To actually have something concrete you can manipulate in a program, you must use the Modem class to create a Modem object. The process of creating an object from a class is called instantiation, which is why objects are also called instances.

    A Modem class can be used to create many different Modem objects in a program, and each of these objects could have different features, such as the following:

    • Some are internal modems, and others are external modems.

    • Some use the COM1 port and others use the COM2 port.

    • Some have error control, and others don't.

    Even with these differences, two Modem objects still have enough in common to be recognizable as related objects. Figure 1.1 shows a Modem class and several objects created from that template.


    Figure 1.1  The Modem class and several Modem objects.

    Object Reuse

    Here's another example: Using Java, you could create a class to represent all command buttons—clickable boxes that appear on windows, dialog boxes, and other parts of a program's graphical user interface.

    When the CommandButton class is developed, it could define these features:

    • The text that identifies the button's purpose

    • The size of the button

    • Aspects of its appearance, such as whether it has a 3D shadow

    The CommandButton class also could define how a button behaves, deciding the following things:

    • Whether the button needs a single click or a double-click to use

    • Whether it should ignore mouse clicks entirely

    • What it does when successfully clicked

    After you define the CommandButton class, you can create instances of that button—in other words, CommandButton objects. The objects all take on the basic features of a clickable button as defined by the class, but each one could have a different appearance and slightly different behavior depending on what you need that object to do.

    By creating a CommandButton class, you don't have to keep rewriting the code for each command button that you want to use in your programs. In addition, you can reuse the CommandButton class to create different kinds of buttons as you need them, both in this program and in others.


    Note - One of Java's standard classes, javax.swing.JButton, encompasses all the functionality of this hypothetical CommandButton example and more. You get a chance to work with it during Day 9, "Working with Swing."


    When you write a Java program, you design and construct a set of classes. When your program runs, objects are instantiated from those classes and used as needed. Your task as a Java programmer is to create the right set of classes to accomplish what your program needs to accomplish.

    Fortunately, you don't have to start from scratch. The Java language includes hundreds of classes that implement most of the basic functionality you will need. These classes are called the Java 2 class library, and they are installed along with a development tool such as SDK 1.5.

    When you're talking about using the Java language, you're actually talking about using this class library and some standard keywords and operators recognized by Java compilers.

    The class library handles numerous tasks, such as mathematical functions, text handling, graphics, sound, user interaction, and networking. Working with these classes is no different than working with classes you create.

    For complicated Java programs, you might create a whole set of new classes with defined interactions among them. These classes could be used to form your own class library for use in other programs.

    Reuse is one of the fundamental benefits of object-oriented programming.

    More Java Articles
    More By Sams Publishing


       · This article is an excerpt from the book "Sams Teach Yourself Java 2 in 21 Days, 4th...
     

    Buy this book now. This article is excerpted from chapter one of Sams Teach Yourself Java 2 in 21 Days, 4th Edition, written by Rogers Cadenhead and Laura Lemay (Sams; ISBN: 0672326280). Check it out today at your favorite bookstore. Buy this book now.

    JAVA ARTICLES

    - Deploying Multiple Java Applets as One
    - Deploying Java Applets
    - Understanding Deployment Frameworks
    - Database Programming in Java Using JDBC
    - Extension Interfaces and SAX
    - Entities, Handlers and SAX
    - Advanced SAX
    - Conversions and Java Print Streams
    - Formatters and Java Print Streams
    - Java Print Streams
    - Wildcards, Arrays, and Generics in Java
    - Wildcards and Generic Methods in Java
    - Finishing the Project: Java Web Development ...
    - Generics and Limitations in Java
    - Getting Started with Java Web Development in...






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
    Stay green...Green IT