|

Computer Science (CSCI)
Department
College of Engineering, Computer Science, & Technology (ECST)
|
|
JETT Workshops
Object-Oriented Programming
Fall 2004
|
 |
|
Classes and Objects
are fundamental concepts of
Object-Oriented Programming (OOP)
(Example code and figures from Computing Concepts with Java Essentials, 3rd
edition, by Cay Horstmann.
Visit www.wiley.com/college/horstmann
for information on the current edition of this excellent text.)
- from the AP
CS Java Subset
- Students are expected to construct objects with the new operator,
to supply construction parameters, and to invoke accessor and modifier
methods.
- For the A exam, students are expected to modify existing classes
(by adding or modifying methods and instance variables).
- For the AB exam, students are expected to design their own classes.
- All instance variables are private.
- Methods, constructors, and constants (static final variables) are either
public or private.
- Students need to be able to implement constructors that initialize
all instance variables.
- Class
- provides the definitions needed for a given type of object
- kind of like a template for object creation and manipulation
- can also think of it as a factory for producing instances of a certain
type of object - it has constructors
- it also provides operations that work on a certain type of object - it
has methods
- it also specifies what kind of information should be stored for each
instance of one of these objects - it has instance variables
- Object
- an instance of some class
- an entity you can create and manipulate in your program
- a collection of data items that store the state of the object and methods
to manipulate them
- Using a Class versus Implementing a Class
- using a class
- there are many classes already defined in the Java library
- we can use these to create objects in our programs
- a class will provide a public interface
- constructors allow the user of a class to construct
(create) objects
- methods allow the user of a class to manipulate objects
- implementing a class
- we can design and implement our own classes
- we will provide constructors and a public interface
- a class will have a hidden implementation
- methods and data that makes the public interface work
- these are private - hidden from a user of the class
- Object Creation
new Rectangle(5, 10, 20, 30)
|
- new - Java keyword - create a new object - allocates memory
- Rectangle - the type of the object - given by the name
of the class
- Rectangle(5, 10, 20, 30) is a call to a constructor for
a Rectangle object
- 5, 10, 20, 30 - the construction parameters - vary these for
different objects
- Application Programmer's Interface (API)
- Rectangle is a class provided as part of the Java
API
- it is part of the Abstract Windowing Toolkit (AWT)
- a package is a collection of classes - Rectangle is in the java.awt
package
- Implementing a Class
- a class consists of three main components
- constructors
- instance variables (fields)
- methods
- Method Definition
- access specifier (such as public)
- return type (such as double or void)
- method name (such as deposit)
- list of parameters (type & name, such as double amount)
- method body in { }
public class BankAccount
{ ...
public void deposit(double amount)
{
double newBalance = balance + amount;
balance = newBalance;
}
...
}
|
- Instance Variables
- access specifier (such as private)
- type of variable (such as double)
- name of variable (such as balance)
public class BankAccount
{ ...
private double balance;
}
|
- Accessing Instance Variables
- the methods of the BankAccount class can access the private
instance variable
- other methods cannot
public class BankAccountTest { public static void main(String[] args) {
BankAccount myAccount = new BankAccount(); . . . System.out.println(myAccount.balance); // ERROR!! }
|
- Constructors
- a constructor initializes the instance variables
- constructor name = class name - there is no return type
public class BankAccount { public BankAccount(double initialBalance)
{
balance = initialBalance;
} . . . }
|
- invoked in new expression
- Testing a Class
- Test class: a class with a main method that contains statements
to test another class.
- Typically carries out the following steps:
- Construct one or more objects of the class that is being tested.
- Invoke one or more methods.
- Print out one or more results
Encapsulation = Hiding data and providing
access & behaviors through methods
|
Exercise - modify the BankAccount class
- pick a programming environment
- set up the BankAccount project
- play with the program
- create some more BankAccount objects in BankAccountTest
- call methods to deposit and withdraw
- get the balance for each and print it
- modify the BankAccount class
- add an instance variable to store the account owners name
- add an appropriate accessor method
- supply an appropriate constructor
- modify the test class to test this new functionality
- solution - no peeking until yours works!
|