Java
  Home arrow Java arrow Page 3 - 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 - Organizing Classes and Class Behavior


    (Page 3 of 7 )

    An introduction to object-oriented programming in Java isn't complete without looking at three more concepts: inheritance, interfaces, and packages.

    These three things are all mechanisms for organizing classes and class behavior. The Java class library uses these concepts, and the classes you create for your own programs also need them.

    Inheritance

    Inheritance is one of the most crucial concepts in object-oriented programming, and it has a direct effect on how you design and write your own Java classes.

    Inheritance is a mechanism that enables one class to inherit all the behavior and attributes of another class.

    Through inheritance, a class immediately has all the functionality of an existing class. Because of this, you only must define how the new class is different from an existing class.

    With inheritance, all classes—those you create and those from the Java class library and other libraries—are arranged in a strict hierarchy.

    A class that inherits from another class is called a subclass. The class that gives the inheritance is called a superclass.

    A class can have only one superclass, but each class can have an unlimited number of subclasses. Subclasses inherit all the attributes and behavior of their superclasses.

    In practical terms, this means that if the superclass has behavior and attributes that your class needs, you don't have to redefine it or copy that code to have the same behavior and attributes. Your class automatically receives these things from its superclass, the superclass gets them from its superclass, and so on, all the way up the hierarchy. Your class becomes a combination of its own features and all the features of the classes above it in the hierarchy.

    The situation is comparable to the way you inherited all kinds of things from your parents, such as your height, hair color, love of grunge music, and reluctance to ask for directions. They inherited some of these things from their parents, who inherited from theirs, and backward through time to the Garden of Eden, Big Bang, or insert personal cosmological belief here.

    Figure 1.2 shows the way a hierarchy of classes is arranged.

                                               



    Figure 1.2  A class hierarchy.

    At the top of the Java class hierarchy is the class Object—all classes inherit from this one superclass. Object is the most general class in the hierarchy, and it defines behavior inherited by all the classes in the Java class library. Each class farther down the hierarchy becomes more tailored to a specific purpose. A class hierarchy defines abstract concepts at the top of the hierarchy. Those concepts become more concrete farther down the line of subclasses.

    Often when you create a new class in Java, you will want all the functionality of an existing class with some modifications of your own creation. For example, you might want a version of a CommandButton that makes a sound when clicked.

    To receive all the CommandButton functionality without doing any work to re-create it, you can define your class as a subclass of CommandButton. Your class then would automatically inherit behavior and attributes defined in CommandButton, as well as the behavior and attributes defined in the superclasses of CommandButton. All you have to worry about are the things that make your new class different from CommandButton itself. Subclassing is the mechanism for defining new classes as the differences between those classes and their superclass.

    Subclassing is the creation of a new class that inherits from an existing class. The only task in the subclass is to indicate the differences in behavior and attributes between itself and its superclass.

    If your class defines an entirely new behavior and isn't a subclass of another class, you can inherit directly from the Object class. This allows it to fit neatly into the Java class hierarchy. In fact, if you create a class definition that doesn't indicate a superclass, Java assumes that the new class is inheriting directly from Object. The VolcanoRobot class you created earlier inherited from the Object class.

    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