Lecture Notes - A First Look at Classes - Chapter 7
Simple Classes
- class - a datatype to define an object.
- object - a variable storing an instance of a class. An object is a special kind of variable
that is like a package containing many things inside. It has multiple values inside it (called member
variables, instance variables, properties or fields), and it also can have functions as part of it. These
functions are called member functions or methods.
- Encapsulation - combining into a single unit both data and the functions that operate on
the class data. Such a unit is called an object.
- Instance Variable - like a global variable for all methods of a class. It is defined inside
the class definition, but outside all the method definitions. Such a variable is also called a member
variable. It is a core part of each object of the class (each instance of the class) and has the
same lifetime as the object it's a part of. Also called a field or property (of an object)
- Instantiation - the process of creating a new object (a new instance of a class)
- Constructor - a special method (member function) which is used to create and initialize a
new object. The constructor has the same name as the class name.
- new - a keyword to generate a new object (used in conjunction with a constructor).
Indicates that we want to allocate memory for a new object to be created, even if we've used this
variable before to refer to a different object.
- Default Constructor - a constructor with no arguments. If you don't define any constructors,
a default constructor is automatically generated for you, and
it does nothing special. However, if you define any constructors yourself, no default constructor is
generated automatically. So if you define only one constructor, and it has parameters, you will not
be able to instantiate objects of that class without specifying arguments to match that constructor.
- These principles can be seen in SimpleCourseTest.java
- In-class exercise: write a Time class to store hour and minute, and output them.
- SimpleTime.java and
SimpleTimeTest.java Solution to above in-class exercise.
Good Class Design
- Now imagine we're writing this class to sell as a software library for others to use. These other
programmers might use our class incorrectly, causing problems. To make this class robust, we want to
force programmers who use the class to use methods we write to set and get the member variables. This
way we can prevent incorrect values from being assigned to the member variables, and we can have greater
control over how data is returned from the class. To have this kind of control over how a class is
used, we need to declare some members private.
- private - a class member that is only accessable from the class' member functions.
- public - a class member that is accessable both inside and outside the class definition.
- By default, all members of a class are essentially public (more details below). But we can make them
private if we wish.
- It's good programming style to make all member variables private, and use public member functions to
get and set them. That way the author of the class has maximum control over how it's used.
- Accessor - a method which returns the value of a member variable. The name of an accessor
method usually starts with "get".
- Mutator - a method which sets the value of a member variable. The name of a mutator method
usually starts with "set". Can be used to ensure the data is stored in the proper format.
- Information Hiding - A class' methods typically provide the only way to access
its data. If you want access to an object's member data, you call a method on that object. The data is
hidden from non-member functions. Information Hiding also involves hiding how exactly
the class's member functions work.
- Interface - The way a class is instantiated and used. The interface consists of the set of
public class members and what they're used for. It's the information one needs to utilize a class.
- Implementation - The way a class works "under the hood". The implementation is the private
code (hidden information) which makes the interface work. It includes the source code for the methods
- Constructor syntax is a bit different than other member functions. The constructor has no return
type (not even "void") and its name is the same as the class name.
- this - Every class has a hidden private instance variable called "this" which stores a
reference to this instance of the class. So to explicitly refer to the current object in a method, use
"this".
- Example of good class design: CourseTest.java - a file which
defines two classes - the Course class and the CourseTest class with a main method to test it.
- In-class exercise: modify the Time class from the previous exercise so that is uses good class
design
(make the member variables private, write public accessor and mutator methods as needed, verify data
format such as minutes between 0 and 59, etc.)
Your Time class should work with TimeTest.java
- Time.java - Solution to above in-class exercise
Static Class Members
- Sometimes you want to have variables or functions which relate to all members of a class, instead
of each individual object separately.
- static - a keyword indicating that a member variable or member function relates to
the entire class. A static member is not specific to any object.
- static member variable - a member variable which exists in just one memory
location for the class in general and for all objects of that class. A static member variable is a
single variable which is shared by all objects of that class. It can be referenced in two ways: either
as a member of the class as a whole, e.g.
ClassName.variableName or as a member of a
particular object of that class, e.g. objectName.variableName. Either way, the same variable
is being referenced.
- static method - a member function which has access only to static
members of the class. It can access any static member variables or functions, but can do nothing which
is specific to a particular object. Its implicit parameter is the class as a whole, not a particular
object in that class. Like static member variables, these may be called in two ways: either
as a member of the class as a whole, e.g.
Classname.functionName(argumentList) or as a
member of a particular object of that class, e.g. objectname.functionName(argumentList).
Either way, the same method is being referenced.
- The keyword "this" cannot be used in a static method, because such a method does not have an object
as an implicit parameter.
- Bubble.java - a simple demonstration of static vs. instance variables
- In-class exercise: Add a static Time object to your Time class, to represent the current time of
day.
Add static methods to the class to access and manipulate the current time. Use
this version of TimeTest.java
- Here is the solution to that in-class exercise
Arrays of Objects
- Arrays of objects in Java are really arrays of reference variables that can refer to objects.
- The reference variables in an array are null by default.
- Individual constructors must be called for individual array elements.
- CarArray.java - Declares and manipulates an array of Car objects.
- ArrayLists work especially well with objects. Here is an
example:
an ArrayList of Course objects.
- Autoboxing: Wrapper classes are automatically used when primitive variables are added to ArrayLists.
- In-class exercise: change CarArray.java above to use an ArrayList instead of an array.
Return to the main CS 111A web page