Mat 2170 Week 10
Classes & Objects Spring 2014
1
Student Responsibilities
◮ Reading: Textbook, Chapter 6, Chapter 7.1 – 7.3 ◮ Lab: Inheritance ◮ Attendance
2
Java Classes
◮ Class: a collection of (usually related) data, along with the
methods or operations capable of accessing it
◮ Data members: the storage components of a class ◮ Member functions: the messages or methods of a class, used
to access and modify the data members
◮ We must instantiate (declare) an object of the class before we
are allowed to store data in it or send it a message
3
Java Objects
◮ Fundamental (primitive) Data Types
◮ Examples: int, double, boolean ◮ Objects of a fundamental type have:
- 1. a state described by a single value.
◮ Java Classes
◮ Examples: String, GRect, GPoint ◮ Objects of a class have:
- 1. a state: a collection of values or attributes
- 2. messages which the object understands and can act upon
4
Defining Our Own Classes
◮ The standard form of a class definition in Java:
public class name extends superclass { class body }
◮ The extends clause on the header line specifies the name of the
superclass.
◮ If the extends clause is missing, the new class becomes a direct
subclass of Object, which is the root of Java’s class hierarchy.
5
Class Contents
◮ The body of a class consists of a collection of Java definitions
that are generically called entries.
◮ The most common entries are:
- 1. constructors — how to create an instance (an object with
initial value/s) of the class
- 2. methods — the methods associated with the class
- 3. instance variables — any necessary local objects
- 4. named constants — any necessary constants for the class
6