Java
  Home arrow Java arrow Page 4 - Introducing Classes and More in 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  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
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

Introducing Classes and More in Java
By: Sams Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 2
    2006-05-11

    Table of Contents:
  • Introducing Classes and More in Java
  • Running the Program
  • Organizing Classes and Class Behavior
  • Creating a Class Hierarchy
  • Inheritance in Action
  • Summary
  • Questions and Answers

  • 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


    Introducing Classes and More in Java - Creating a Class Hierarchy


    (Page 4 of 7 )

    If you're creating a large set of classes, it makes sense for your classes to inherit from the existing class hierarchy and to make up a hierarchy themselves. Organizing your classes this way takes significant planning, but the advantages include the following:

    • Functionality common to multiple classes can be put into a superclass, which enables it to be used repeatedly in all classes below it in the hierarchy.

    • Changes to a superclass automatically are reflected in all its subclasses, their subclasses, and so on. There is no need to change or recompile any of the lower classes; they receive the new information through inheritance.

    For example, imagine that you have created a Java class to implement all the features of a volcanic exploratory robot. (This shouldn't take much imagination.)

    The VolcanoRobot class is completed and works successfully, and everything is copacetic. Now you want to create a Java class called MarsRobot.

    These two kinds of robots have similar features; both are research robots that work in hostile environments and conduct research. Your first impulse might be to open up the VolcanoRobot.java source file and copy a lot of it into a new source file called MarsRobot.java.

    A better plan is to figure out the common functionality of MarsRobot and VolcanoRobot and organize it into a more general class hierarchy. This might be a lot of work just for the classes VolcanoRobot and MarsRobot, but what if you also want to add MoonRobot, UnderseaRobot, and DesertRobot? Factoring common behavior into one or more reusable superclasses significantly reduces the overall amount of work that must be done.

    To design a class hierarchy that might serve this purpose, start at the top with the class Object, the pinnacle of all Java classes. The most general class to which these robots belong might be called Robot. A robot, generally, could be defined as a self-controlled exploration device. In the Robot class, you define only the behavior that qualifies something to be a device, self-controlled, and designed for exploration.

    There could be two classes below Robot: WalkingRobot and DrivingRobot. The obvious thing that differentiates these classes is that one travels by foot and the other by wheel. The behavior of walking robots might include bending over to pick up something, ducking, running, and the like. Driving robots would behave differently. Figure 1.3 shows what you have so far.


    Figure 1.3  The basic Robot hierarchy.

    Now, the hierarchy can become even more specific. With WalkingRobot, you might have several classes: ScienceRobot, GuardRobot, SearchRobot, and so on. As an alternative, you could factor out still more functionality and have intermediate classes for TwoLegged and FourLegged robots, with different behaviors for each (see Figure 1.4).

    Finally, the hierarchy is done, and you have a place for VolcanoRobot. It can be a subclass of ScienceRobot, which is a subclass of WalkingRobot, which is a subclass of Robot, which is a subclass of Object.

    Where do qualities such as status, temperature, or speed come in? They come in at the place they fit into the class hierarchy most naturally. Because all robots have a need to keep track of the temperature of their environment, it makes sense to define temperature as an instance variable in Robot. All subclasses would have that instance variable as well. Remember that you need to define a behavior or attribute only once in the hierarchy, and it automatically is inherited by each subclass.

    Figure 1.4  Two-legged and four-legged walking robots.


    Note - Designing an effective class hierarchy involves a lot of planning and revision. As you attempt to put attributes and behavior into a hierarchy, you're likely to find reasons to move some classes to different spots in the hierarchy. The goal is to reduce the number of repetitive features that are needed.


    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). 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-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek