Download Chapter 3: Object-Oriented Programming

Transcript
Java by Definition
Chapter 3: Object-Oriented Programming
Page 2 of 82
Quick View
Here is a quick overview of the topics covered in this chapter.
3.1. Classes and Objects
Classes; Objects, Instantiation, and Constructors; Destructors and Garbage Collection
3.2. Class Access and Encapsulation
Accessing Fields and Methods; Private, Public, and Protected; Static Fields and Methods
3.3. Overloading
Overloading Methods; Overloading Constructors
3.4. Inheritance
Creating Class Hierarchies; Inheritance and Constructors; Overriding; Abstract and Final
3.5. The Basic Java Object
(*) 3.6 Interfaces and Polymorphism
Multiple Inheritance and Interfaces; Polymorphism
(**) Case Study: OO Design for Invoice Program
(*) This section is optional but recommended
(**) This section is optional
3.1. Classes and Objects
Any code written in Java is entirely composed of classes and objects. We have previously written
Java programs and hence we have already created classes.1 This section defines classes formally and
shows how to use them to manufacture objects.
Classes
Classes
A class is the fundamental structure in Java. It is composed of two sections, fields to contain data
and methods to manipulate data or perform an action. Every class represents a new reference
type that can be used by other classes. Classes are defined using the syntax:
[public] class ClassName [extends ClassName] [implements InterfaceList]
{ /* list of fields */
/* list of methods */;
}
1
String and StringBuffer in section 1.4, the wrapper classes in section 2.5, the Console class in section 2.4, and
decimal formatting in section 1.5 are examples where we have used existing non-executable classes.
Bert G. Wachsmuth
DRAFT April 2009