Java
  Home arrow Java arrow Java Classes
Dev Shed Forums 
Administration  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
eWeek
 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: 3 stars3 stars3 stars3 stars3 stars / 5
    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:
      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
     
     
    Iron Speed
     
    ADVERTISEMENT

    TestComplete™ automates software testing for a fraction of what the big guys charge. Easy functional and load testing for all Windows, .NET, Java and Web apps. Download a free trial now.

    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


       · Hi,Entertaining, easy-to-read article but... I can't help feeling your wording...
       · Oh I nearly forgot, the Bad Non-Default Constructor (BNDC?) isn't that bad at all of...
       · Hey Elinor,Thanks for taking the time to comment. Good points all around,...
     

       

    JAVA ARTICLES

    - 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...
    - Java Operators
    - Primitive Data Types and Basic Language Rule...
    - Java and Object-Oriented Programming
    - Java Beginning Programming
    - Gaming Development Setup
    - Using RPC-Style Web Services with J2EE
    - Integrating XML with J2EE
    - Taming Tiger: Concurrent Collections
    - Combating the ‘Object Crisis’

    Iron Speed



    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway