Docsity

Chapter Six: Part II - Objects and Classes - Extending | MAT 2170, Study notes of Computer Science

Partial preview of the text

Download Chapter Six: Part II - Objects and Classes - Extending | MAT 2170 and more Study notes Computer Science in PDF only on Docsity!

Mat 2170

Chapter Six ó Part II

Objects & Classes ñ Extending

Fall 2007

The javadoc Documentation System

I (^) One of the important ways in which Java works together with the WorldñWideñWeb is in the design of its

documentation system , javadoc.

I The javadoc program reads Java source les and

generates documentation for each class in the le.

I (^) Complete documentation for the ACM Java Libraries is located at:

http://jtf.acm.org/javadoc/student/

Constructor and Method Summaries

Constructor and Method Details

Writing javadoc Comments

I The javadoc system is designed to create the

documentary web pages automatically from the Java source code.

I (^) To make this work with your own programs, you need to add specially formated comments to your code.

I A javadoc comment begins with the characters /** and

extends up to the closing */ just as a regular comment

does.

I (^) Please read the book for more information.

Java Classes

I (^) Class : a collection of (usually related) data, along with the methods or operations capable of accessing it

I (^) Data members : the storage components of a class

I (^) Member functions : the messages or methods of a class, used to access and modify the data members

I (^) Must declare (instantiate) an object of the class before allowed to store data in it or send it a message

Java Objects

I (^) Fundamental Data Types I (^) Examples: int , double , boolean I (^) Objects of a fundamental type have:

  1. a state described by a single value.

I (^) Java Classes I (^) Examples: String , GRect , GPoint I (^) Objects of a class have:

  1. a state: a collection of values or attributes
  2. messages which the object understands and can act upon

Thinking about clocks...

A Wall Clock... An Alarm Clock...

A Grandfather Clock... A Wristwatch...

Thinking about clocks...

E A Wall clock is a kind of clock ⇒ Clock that hangs on a wall

E An Alarm clock is a kind of clock ⇒ Clock with an alarm

E A Grandfather clock is a kind of clock ⇒ Clock with a pendulum and chimes

E A Wristwatch is a kind of clock ⇒ Clock that straps to your arm

E Wall clocks, Alarm clocks, Grandfather clocks, and Wristwatches are specialized clocks

Extending Classes

Wouldn't it be nice to be able to create specialized program objects without starting from scratch?

For example:

I (^) Blinking rectangles

I (^) Moving circles

I (^) Arbitrary precision numbers

Inheritance is the objectñoriented programming mechanism for specialization.

Inheritance

I (^) Inheritance is the ability to dene new classes of objects using existing classes as a basis.

I (^) The new class inherits the attributes and behaviors of the parent classes.

I (^) The new class ISñA specialized version of the parent class.

Inheritance

I (^) A natural way to reuse code I (^) Programming by extension rather than re-invention

I (^) Object-oriented paradigm is wellñsuited for this style of programming

I (^) Terminology I (^) Parent , base, or superclass

I (^) Derived or subclass

Class Messages

I (^) Constructors : Create an object, provide initial values

int size = Rgen.nextInteger(6, 8);

I (^) Mutators : Change the state or attributes

MyCircle.setColor(Color.RED);

I (^) Inspectors : Determine something about the state

getHeight()

I (^) Facilitators : Perform a task

MyCircle.move();