Java
  Home arrow Java arrow Java Classes
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
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

Java Classes
By: James Payne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 8
    2008-01-09


    Table of Contents:
  • Java Classes
  • How to Declare a Class
  • Using Public Methods to Obtain Private Fields
  • Naming Conventions for Our Pal, the Method
  • Creating Objects with Constructors

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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


    Java Classes
    ( Page 1 of 5 )

    Well it's been a while since we discussed Java and I graced these pages with its complicated beauty (for an example of complicated beauty, take a peek at Hillary Swank, who is pretty in a well...complicated sort of way). In this new series of articles, we will be discussing Classes.

    We touched on Classes and Objects in my article on Java and Object-Oriented Programming, which you can find here: http://www.devshed.com/c/a/Java/Java-and-ObjectOriented-Programming/2/

    Objects and Classes: What Are They?

    An object is made up of states and behaviors and is usually used as a model for objects in the real world. An example of an object might be a Ford truck. Classes, meanwhile, would represent the category of Trucks. There can be Ford Trucks, Chevy Trucks, Dodge Trucks, and so forth, and while they may vary from one another in certain ways, fundamentally they are all part of the truck class, and therefore all share the same basic truck foundations (i.e.; four tires, a flatbed, etc).

    Here is an example of a class declaration:


    public class BruceLee {

    //We will give Bruce Lee three fields

    public int punch;

    public int kick;

    public int roundhouse;

    //We will provide one constructor for Bruce

    public BruceLee(int usePunch, int useKick, int useRoundhouse) {

    punch=usePunch;

    kick=useKick;

    roundhouse=useRoundhouse;

    }

    //We will give Bruce Lee three methods

    public void setPunch (int someNewValue) {

    punch = someNewValue;

    }

    public void setKick (int someNewValue) {

    kick = someNewValue;

    }

    public void setRoundhouse (int someNewValue) {

    roundhouse = someNewValue;

    }

    }

    Now if we said that the class BruceLee had pupils, or subclasses, we could declare that subclass like so:

    public class Pupil extends BruceLee {

    // We give Pupil one field

    public int eyePoke;

    //And one Constructor

    public Pupil(int usePunch, int useKick, int useRoundhouse) {

    flex(usePunch, useKick, useRoundhouse);

    eyepoke=usePunch;

    }

    //And one method

    public void finger(int someNewValue) {

    eyepoke=somenewValue;

    }

    }

    Don't worry if that doesn't makes sense right now; it doesn't even make sense to me and I wrote it. But I promise you by the end of this tutorial it will appear brilliant.

    In the above example, Pupil inherits all of the fields of BruceLee, since it is a subclass of his, and adds its own field (eyepoke) and a method to use it (finger).



     
     
    >>> More Java Articles          >>> More By James Payne
     

       

    JAVA ARTICLES

    - Exception Handling Techniques in Java
    - More About Multithreading in Java
    - The Basics of Multiple Threads in Java
    - Data Access Using Spring Framework JDBC
    - New Object Initialization in Java
    - Adding Images With iTextSharp
    - Adding Columns With iTextSharp
    - Creating Simple PDF Files With iTextSharp
    - The Spring Framework: Understanding IoC
    - Introducing the Spring Framework
    - Java Classes
    - Completing the Syntactic Comparison of Java ...
    - Syntactic Comparison of Java and C/C++
    - Java Statements
    - Conditionals, Expressions and Other Java Ope...





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