Obj&Classes

advertisement
User-defined Instantiable Classes
A class is a description – a template – for objects. In the real world, we have diagrams
called blueprints that describe houses. A blueprint is not a house – it is a design for a
house. A house is considered an instance from a blueprint. A house is a tangible
object. Many houses can be built from the same blueprint. But each instance may
have a different color, or a different doorway. In Object-Oriented Programming (OOP)
we design classes and then use instances of the classes. Just like in the real world, the
class is the description, and then we make instances (or objects) from the class, like
making a house from a blueprint.
blueprint 
house objects 
House Class
white house
grey house
You have already designed a class (inherited from the Applet class), and you have
already used many classes, for example, the TextField class, the Label class, the Math
class, and the Button class.
A class definition includes properties (also called instance variables) and behaviors
(known as methods). You have already used class properties, for example, Color.red,
and class methods, for example, outputField.setText(“Hello”), and Math.random(). And
you have also made your own methods.
Instantiable classes are classes for which instances (objects) are created. Another
type of class is called a static class – you do not make objects from static classes. The
Math class is a static class.
You have already used many instantiable classes from Java’s libraries, for example:
Label outputLabel = new Label (“”);
Color darkRed = new Color (255, 20, 0);
TextField name = new TextField (20);
You can also make your own instantiable classes. The statement below shows how to
make an instance of a class that you define yourself.
House funnyHouse = new House(200, 100, 100);
The class House is usually defined in a separate file. This rest of this document
describes how to make your own classes.
An instantiable class consists of the following parts:
import statement(s)
comments
class header
private data members (variables) ... all class variables should all be private
public constructor(s)
public instance methods
private instance methods
… methods to be referred to by the applet
… methods to be accessed within the class
Class header
Syntax:
class <class name>
Class names must begin with an uppercase letter.
Here are two examples:
public class House
{}
public class CheckingAccount extends Account
{}
Constructors
Constructors initialize instances of the class to a valid state. Values are sent to the
constructor when an object (instance) is instantiated through a new statement in the
main (applet) program. Examples of instantiation were shown above.
A constructor serves the same function in a user-defined class as the init() method
serves in an applet – it initializes the variables for a specific instance of a class (an
instance of a class is called an object). The Constructor’s name is the same as the
class name. If there are no constructors included in a class, the Java compiler creates
the default constructor. This is a constructor with an empty method body. If any
constructors are included in the class, the Java compiler does not create the default
constructor.
Syntax:
public <class name> (<parameters>)
Here are two examples of class definitions with constructors:
class SampClass
{
// private data members
private int z;
// constructor
public SampClass (int z)
{
this.z = z;
//this is a constructor with 1 parameter
// z is a private variable in the class
// the instance’s variable z (this.z) is set
// to the z sent to it
}
// other public and private methods
}
public class BlueHouse
{
// private data members
private int x, y, size;
private Color houseColor;
// constructor with parameters
public BlueHouse (int x, int y, int size)
{
this.x = x;
this.y = y;
this.size = size;
houseColor = Color.blue; //the this keyword is not needed
}
//constructor without parameters
public BlueHouse ()
{
x = 100;
y = 100;
size = 50;
houseColor = Color.green;
}
// other public and private methods
}
The constructor parameter list is optional and depends on the purpose of the
constructor. The data type for each parameter in the list must be given, for example,
(int x, int y). The following parameter list is invalid: (int x,y). If there are no parameters,
the constructor name is followed by a pair of parentheses (). As you can see above,
there may be more than one constructor.
Multiple constructors (Polymorphism)
Classes may have multiple constructors. Multiple constructors allow initialization of
different data types or more than one data value at a time.
1. You can have two or more constructors with a single parameter, but the parameter
must be a different data type in each constructor. Example:
public SampClass(int z) {...}
public SampClass(double z) {...}
2. You can have two or more constructors with a different number of parameters.
Example:
public SampClass() {...}
public SampClass(int z) {...}
public SampClass(int z, int y);
Methods
Methods inside of user-defined classes follow the same rules for method construction
that you have already learned in class.
Using methods from the applet class
To use an instance method from an applet, you give the object’s name, a dot, and the
method name. For example, an object tt is created for TestClass and then the method
doit() is used:
TestClass tt = new TestClass2(); // t is an instance of TestClass
tt.doit();
Using methods from within the user-defined class
When you call methods inside of a class, you do not use dot syntax (shown above).
You just give the method name. When instance methods are called from outside of the
class, for example, from the applet class, you must give the instance name, dot, and
method name. Recall in Karel++, when a robot in the task performed an instruction
(method) you gave the robot's name. When you created the instructions in the class, no
robot name was used.
For example, let’s say that you have a method within a class definition called setX.
Here is how setX is called within the class:
setX(20);
Files
A file should contain only one class. The file must have the exact name as the class.
Programs which use instantiable classes will have at least two files -- one file has the
main class (the applet) and its definition; the other file has the instantiable class and its
definition. IMPORTANT: Both files must be within the same folder. When you compile
you get two compiled class files.
Inheritance
One class may be inherited from another. An inherited class gets all of the properties
and methods from the “parent” (super) class, but may alter these methods / properties
(for example, the body of the paint() method is altered when you make an applet. An
inherited class is called a subclass. A subclass can have its own additional methods
and properties. If you were making a class to describe a bank account, you would have
methods for depositing and withdrawing funds. From this class, you might make two
inherited subclasses, savings and checking, which would have their own additional
methods and properties. For example the checking class might have a minimum
balance property.
Download