Home arrow Java arrow Page 5 - Getting Started with Java
JAVA

Getting Started with Java


This article introduces you to the Java programming language. The first of two parts, it 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).

Author Info:
By: Sams Publishing
Rating: 5 stars5 stars5 stars5 stars5 stars / 12
May 04, 2006
TABLE OF CONTENTS:
  1. · Getting Started with Java
  2. · Introduction to Java
  3. · Selecting a Development Tool
  4. · Object-Oriented Programming
  5. · Objects and Classes
  6. · Attributes and Behavior

print this article
SEARCH DEVARTICLES

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.


blog comments powered by Disqus
JAVA ARTICLES

- Java Too Insecure, Says Microsoft Researcher
- Google Beats Oracle in Java Ruling
- 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 ...

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials