Java
  Home arrow Java arrow Page 5 - 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 - Inheritance in Action


    (Page 5 of 7 )

    Inheritance in Java works much more simply than it does in the real world. There are no executors of a will, judges, or courts of any kind required in Java.

    When you create a new object, Java keeps track of each variable defined for that object and each variable defined for each superclass of the object. In this way, all the classes combine to form a template for the current object, and each object fills in the information appropriate to its situation.

    Methods operate similarly: A new object has access to all method names of its class and superclass. This is determined dynamically when a method is used in a running program. If you call a method of a particular object, the Java interpreter first checks the object's class for that method. If the method isn't found, the interpreter looks for it in the superclass of that class, and so on, until the method definition is found. This is illustrated in Figure 1.5.

     

    Figure 1.5  How methods are located in a class hierarchy.

    Things get complicated when a subclass defines a method that has the same name, return type, and arguments that a method defined in a superclass has. In this case, the method definition found first (starting at the bottom of the hierarchy and working upward) is the one that is used. Because of this, you can create a method in a subclass that prevents a method in a superclass from being used. To do this, you give the method with the same name, return type, and arguments as the method in the superclass. This procedure is called overriding (see Figure 1.6).

    Figure 1.6  Overriding methods.

    Single and Multiple Inheritance

    Java's form of inheritance is called single inheritance because each Java class can have only one superclass (although any given superclass can have multiple subclasses).

    In other object-oriented programming languages, such as C++, classes can have more than one superclass, and they inherit combined variables and methods from all those superclasses. This is called multiple inheritance, and it provides the means to create classes that encompass just about any imaginable behavior. However, it significantly complicates class definitions and the code needed to produce them. Java makes inheritance simpler by allowing only single inheritance.

    Interfaces

    Single inheritance makes the relationship between classes and the functionality those classes implement easier to understand and to design. However, it also can be restrictive, especially when you have similar behavior that needs to be duplicated across different branches of a class hierarchy. Java solves the problem of shared behavior by using interfaces.

    An interface is a collection of methods that indicate a class has some behavior in addition to what it inherits from its superclasses. The methods included in an interface do not define this behavior; that task is left for the classes that implement the interface.

    For example, the Comparable interface contains a method that compares two objects of the same class to see which one should appear first in a sorted list. Any class that implements this interface can determine the sorting order for objects of that class. This behavior would not be available to the class without the interface.

    You learn about interfaces during Day 6, "Packages, Interfaces, and Other Class Features."

    Packages

    Packages in Java are a way of grouping related classes and interfaces. Packages enable groups of classes to be available only if they are needed, and they eliminate potential conflicts among class names in different groups of classes.

    For now, you only need to know a few things:

    • The class libraries in Java are contained in a package called java. The classes in the java package are guaranteed to be available in any Java 2 implementation and are the only classes guaranteed to be available across different implementations. The java package contains smaller packages that define specific subsets of the Java language's functionality, such as standard features, file handling, graphical user interface support, and many other things. Classes in other packages such as sun often are available only in specific implementations.

    • By default, your Java classes have access to only the classes in java.lang (basic language features). To use classes from any other package, you have to refer to them explicitly by package name or import them in your source file.

    • To refer to a class within a package, you must normally use the full package name. For example, because the Color class is contained in the java.awt package, you refer to it in your programs with the notation java.awt.Color.

    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 3 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek