Components of Java Technology


  • Language elements
    • Class-based OOPL with inheritance
    • A class is a template that defines how an object will look and behave once instantiated

  • Sample Java Code

    public class Point {
          private float x_;
          private float y_;
    
          public Point (float xval,
                   float yval)
              { x_=xval; y_=yval; }
    
          public float distanceFrom
                      (Point refpt)
                      {
          ...
       }
    }
    

Detailed description: 

"So let's look at the code that's at the bottom of the screen.  It looks similar to C++ if you're familiar with that. 
First, we start with public class Point.  We're defining a class named Point and associated with that class are two pieces of information.  Immediately following the class statement is private float x_ and private float y_.  In this course, we use the convention of putting an underscore after the instance variables.  It helps differentiate them from the methods that we'll talk about later. 
       Now for those with a C++ background, you notice there is a slight difference in the syntax here.  First, the word private does not have a colon after it, because it's not an access specifier that represents a group of things.  Each variable and method must  have the word private, public, protected, or nothing; we'll talk about these access specifiers in more detail as we go through the language. 
       The other thing that is different from C++ is the fact that the class has the word public in front of it.  This says that we're exporting this class to be used outside a  package, and we'll deal with that more further on. 
       Now that we've covered the instance variables, let's look at the methods themselves.  Our first method is called a constructor.  The constructor we see is a public constructor.  It has the same name as the class name and no return type.  It takes two parameters, two floating point values, xval and yval, and we see within the body of the method that we're setting the value x_ equal to xval and the value of y_ equal to yval.  So we're just initializing the object so it has a fixed value when we get started.  When we create a new point, we'll know that it has an x and y value. 
       And finally, we have another method that we've not filled it in for sake of brevity.  It is our distanceFrom, and we indicate that our distanceFrom takes a point as an argument and returns a floating point value.  We assume this routine is going to produce a distance between the two points through some equation. 
        If you look at down at the bottom, you'll also notice one other thing.  The last  thing in the file or in this class is not a semicolon, for you C++ developers.  This is only a slight difference.  No semicolon at the end of classes. 
       One more thing you might notice is that there are no header files in Java; there are only source files.  So if you look at this piece of code you'll realize that the body of the methods are in line with the other information that defines the method. 
       That's a brief overview of a piece of code.  You don't need to memorize it now.  We'll go through it again as we proceed with the rest of the course."