Home arrow Java & J2EE arrow Java Classes

Java Classes

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.

TABLE OF CONTENTS:
  1. Java Classes
  2. How to Declare a Class
  3. Using Public Methods to Obtain Private Fields
  4. Naming Conventions for Our Pal, the Method
  5. Creating Objects with Constructors
By: James Payne
Rating: starstarstarstarstar / 20
January 09, 2008

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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 & J2EE Articles          >>> More By James Payne
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

JAVA & J2EE ARTICLES

- More Java Bugs Lead to More Attacks
- Oracle's Java One Brings News, Surprises
- Oracle Patches Java Runtime Environment
- Apple Syncs Java Update with Oracle
- Spring 3.1 Java Development Framework Compat...
- Jelastic Java PaaS Availability and Pricing ...
- NetBeans 7.1 Released, Supports JavaFX 2
- SolarWinds Releases Newest Version of Java M...
- Free Monitoring Tool for Java Apps on Heroku
- Heroku Adds JCloud Platform Support, Java 7 ...
- Java SE 8 Speculation in Full Swing
- Java SE 7 Now Available
- New JVM Language and Java Reporting Tool
- Java 7 Release Update and New Eclipse Toolkit
- The Best Java Netbeans IDE Plugins

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: