Mat 2170 Classes Week 10 Contents Creating Inheritance Classes - - PowerPoint PPT Presentation

mat 2170
SMART_READER_LITE
LIVE PREVIEW

Mat 2170 Classes Week 10 Contents Creating Inheritance Classes - - PowerPoint PPT Presentation

Mat 2170 Week 10 Classes & Objects Week 10 Mat 2170 Classes Week 10 Contents Creating Inheritance Classes & Objects Methods Lab 10 The GSquare Definition Spring 2014 The GSmartSquare Class Definition Notes Access Review


slide-1
SLIDE 1

Mat 2170 Week 10 Classes & Objects Week 10 Classes Contents Creating Inheritance Methods Lab 10 The GSquare Definition The GSmartSquare Class Definition Notes Access Review

Mat 2170 Week 10

Classes & Objects Spring 2014

slide-2
SLIDE 2

Mat 2170 Week 10 Classes & Objects Week 10 Classes Contents Creating Inheritance Methods Lab 10 The GSquare Definition The GSmartSquare Class Definition Notes Access Review

Student Responsibilities

Reading: Textbook, Chapter 6, Chapter 7.1 – 7.3 Lab: Inheritance Attendance

slide-3
SLIDE 3

Mat 2170 Week 10 Classes & Objects Week 10 Classes Contents Creating Inheritance Methods Lab 10 The GSquare Definition The GSmartSquare Class Definition Notes Access Review

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

slide-4
SLIDE 4

Mat 2170 Week 10 Classes & Objects Week 10 Classes Contents Creating Inheritance Methods Lab 10 The GSquare Definition The GSmartSquare Class Definition Notes Access Review

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
slide-5
SLIDE 5

Mat 2170 Week 10 Classes & Objects Week 10 Classes Contents Creating Inheritance Methods Lab 10 The GSquare Definition The GSmartSquare Class Definition Notes Access Review

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.

slide-6
SLIDE 6

Mat 2170 Week 10 Classes & Objects Week 10 Classes Contents Creating Inheritance Methods Lab 10 The GSquare Definition The GSmartSquare Class Definition Notes Access Review

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
slide-7
SLIDE 7

Mat 2170 Week 10 Classes & Objects Week 10 Classes Contents Creating Inheritance Methods Lab 10 The GSquare Definition The GSmartSquare Class Definition Notes Access Review

Creating Classes

We can design exactly the structure needed to store related data We can designate exactly how data may be accessed or modified We learned how to use classes, now we’ll learn how to modify them, and create our own by:

adding messages to existing classes adding attributes / data to existing classes

  • verriding messages in existing classes

building a class from “scratch”

slide-8
SLIDE 8

Mat 2170 Week 10 Classes & Objects Week 10 Classes Contents Creating Inheritance Methods Lab 10 The GSquare Definition The GSmartSquare Class Definition Notes Access Review

Thinking about clocks. . .

A Wall Clock. . . An Alarm Clock. . . A Grandfather Clock. . . A Wristwatch. . .

slide-9
SLIDE 9

Mat 2170 Week 10 Classes & Objects Week 10 Classes Contents Creating Inheritance Methods Lab 10 The GSquare Definition The GSmartSquare Class Definition Notes Access Review

Thinking about clocks. . .

A Wall clock IS-A kind of clock ⇒ a clock that hangs on a wall An Alarm clock IS-A kind of clock ⇒ a clock with an alarm A Grandfather clock IS-A kind of clock ⇒ a clock with a pendulum and chimes A Wristwatch IS-A kind of clock ⇒ a clock that straps to your arm Wall clocks, Alarm clocks, Grandfather clocks, and Wristwatches are specialized clocks

slide-10
SLIDE 10

Mat 2170 Week 10 Classes & Objects Week 10 Classes Contents Creating Inheritance Methods Lab 10 The GSquare Definition The GSmartSquare Class Definition Notes Access Review

Inheritance Hierarchy

IS−A relationships

Alarm Clock Wall Clock Wristwatch Grandfather Clock

Clock

slide-11
SLIDE 11

Mat 2170 Week 10 Classes & Objects Week 10 Classes Contents Creating Inheritance Methods Lab 10 The GSquare Definition The GSmartSquare Class Definition Notes Access Review

Extending Classes

Wouldn’t it be nice to be able to create specialized Java

  • bjects without starting from scratch? Of course it would!

For example: Blinking rectangles Moving circles Arbitrary precision numbers

Inheritance

is the object–oriented programming mechanism for

specialization

.

slide-12
SLIDE 12

Mat 2170 Week 10 Classes & Objects Week 10 Classes Contents Creating Inheritance Methods Lab 10 The GSquare Definition The GSmartSquare Class Definition Notes Access Review

Inheritance

Inheritance is the ability to define a new class by using an existing class as a basis. The new class inherits the attributes and behaviors of the parent class. The new class IS–A specialized version of the parent class.

slide-13
SLIDE 13

Mat 2170 Week 10 Classes & Objects Week 10 Classes Contents Creating Inheritance Methods Lab 10 The GSquare Definition The GSmartSquare Class Definition Notes Access Review

Derived Classes

A derived class IS–A new version of its parent class. It has the same members its parent class has. It can add members — both methods and data It can re–define members of the parent class, over-riding the parent’s definition.

For example, re-defining what it means to move() by giving a new implementation for the function.

slide-14
SLIDE 14

Mat 2170 Week 10 Classes & Objects Week 10 Classes Contents Creating Inheritance Methods Lab 10 The GSquare Definition The GSmartSquare Class Definition Notes Access Review

Inheritance

A natural way to reuse code

Programming by extension rather than re-invention — building on what we already have, rather than starting from scratch. Object-oriented paradigm is well–suited for this style of programming

Terminology

Parent, base, or superclass Derived or subclass

slide-15
SLIDE 15

Mat 2170 Week 10 Classes & Objects Week 10 Classes Contents Creating Inheritance Methods Lab 10 The GSquare Definition The GSmartSquare Class Definition Notes Access Review

Class Message Examples

Constructors: Create an object — initialize data members GRect rectA = new GRect(0.0, 0.0, 20.0, 30.0); GRect rectB = new GRect(x, y, width, height); Mutators or Setters: Change the state or attributes MyCircle.setColor(Color.RED); Inspectors or Getters: Determine something about the state double bh = Box.getHeight() Facilitators: Perform a task MyCircle.move();

slide-16
SLIDE 16

Mat 2170 Week 10 Classes & Objects Week 10 Classes Contents Creating Inheritance Methods Lab 10 The GSquare Definition The GSmartSquare Class Definition Notes Access Review

Derived Class Definition

General form: public class DerivedClassName extends ParentClass { // Members go here } Example from GSquare Class: public class GSquare extends GRect { // Rest of the definition goes here }

slide-17
SLIDE 17

Mat 2170 Week 10 Classes & Objects Week 10 Classes Contents Creating Inheritance Methods Lab 10 The GSquare Definition The GSmartSquare Class Definition Notes Access Review

A Square and More. . .

A GSquare IS-A GRect which Is restricted to equal width and height Can be created with a given color See Lab Writeup

slide-18
SLIDE 18

Mat 2170 Week 10 Classes & Objects Week 10 Classes Contents Creating Inheritance Methods Lab 10 The GSquare Definition The GSmartSquare Class Definition Notes Access Review

The GSmartSquare Class

A GSmartSquare IS-A GSquare which knows: How to find its window Whether its left or right edge has gone out of the window Whether its top or bottom edge has gone out of the window Whether its fits in its window See Lab Writeup You will be completing this class in lab

slide-19
SLIDE 19

Mat 2170 Week 10 Classes & Objects Week 10 Classes Contents Creating Inheritance Methods Lab 10 The GSquare Definition The GSmartSquare Class Definition Notes Access Review

A Square and Still More. . .

A GMovingSquare IS-A GSmartSquare which: Keeps track of a displacement (x- and y-coordinate) Knows how to move by this displacement We can derive this class from GSmartSquare by adding: displacement: a new attribute to keep track of ∆x and ∆y for the object move: a message to make a move based on this displacement a constructor that initializes the displacement as well as the other usual attributes of a GSquare object See Lab Writeup You will be completing this class in lab

slide-20
SLIDE 20

Mat 2170 Week 10 Classes & Objects Week 10 Classes Contents Creating Inheritance Methods Lab 10 The GSquare Definition The GSmartSquare Class Definition Notes Access Review

Technical Notes I

When we need to refer to a parent class constructor or another of its methods from within a derived class method, we preface the call with the keyword: super.

Example (GSquare): super(x, y, size, size);

When we need to distinguish a derived class method from a parent class method or a parameter, we preface the call with the keyword: this.

Example (GSquare): this(x, y, size, Color.BLACK);

slide-21
SLIDE 21

Mat 2170 Week 10 Classes & Objects Week 10 Classes Contents Creating Inheritance Methods Lab 10 The GSquare Definition The GSmartSquare Class Definition Notes Access Review

Technical Notes II

When no receiver is specified the message is sent to the current object. In other words, message() is the same as this.message() within the class.

Example (GSquare): setColor(color); Is equivalent to: this.setColor(color);

We can also use the keyword this to call a constructor from another constructor within the same class.

slide-22
SLIDE 22

Mat 2170 Week 10 Classes & Objects Week 10 Classes Contents Creating Inheritance Methods Lab 10 The GSquare Definition The GSmartSquare Class Definition Notes Access Review

Notes on Lab 10

The graphics window is able to clear objects from itself with the remove(obj) message. It can erase everything with the removeAll() message. You must be clear in your mind between methods in the circle classes and those in the project (program) class, and how they differ. This lab is worth 50 points, and is not due for two weeks (rather than the usual one) from Thursday. Take this as a hint to begin working on it early. There will be another new lab next week.

slide-23
SLIDE 23

Mat 2170 Week 10 Classes & Objects Week 10 Classes Contents Creating Inheritance Methods Lab 10 The GSquare Definition The GSmartSquare Class Definition Notes Access Review

Provided for you: Moving the particles

// Move all particles in the window // For every object in our graphics window... for (int j = 0; j < getElementCount(); j++) { // Get the jth object GObject gObject = getElement(j); // Make sure it is a Particle if (gObject instanceof GParticle) { // Since we have a particle, // cast it as such and move it. ((GParticle) gObject).move(); } }

slide-24
SLIDE 24

Mat 2170 Week 10 Classes & Objects Week 10 Classes Contents Creating Inheritance Methods Lab 10 The GSquare Definition The GSmartSquare Class Definition Notes Access Review

Controlling Access to Entries

Each entry in a Java class is marked with a keyword to control which classes have access to that entry. The types of access are termed public, private, and protected. The text uses only public and private. All entries are marked as private unless there is a compelling reason to export them.

slide-25
SLIDE 25

Mat 2170 Week 10 Classes & Objects Week 10 Classes Contents Creating Inheritance Methods Lab 10 The GSquare Definition The GSmartSquare Class Definition Notes Access Review

Designer versus Client View of Class Members

Clients cannot access these!

&

Clients can access these

Public members

Protected members

Private members

slide-26
SLIDE 26

Mat 2170 Week 10 Classes & Objects Week 10 Classes Contents Creating Inheritance Methods Lab 10 The GSquare Definition The GSmartSquare Class Definition Notes Access Review

Access Privileges

public All classes in the program have access; public entries in a class are said to be exported by that class. private Access is limited to the class itself, making that entry completely invisible outside the class. protected Access is restricted to the class that defines these entities, along with any of its subclasses or any classes in the same package. (no keyword) The entry is visible only to classes in the same package, and are called package–private.

slide-27
SLIDE 27

Mat 2170 Week 10 Classes & Objects Week 10 Classes Contents Creating Inheritance Methods Lab 10 The GSquare Definition The GSmartSquare Class Definition Notes Access Review

The Structure of Memory

bit: a fundamental unit of information found in one of two possible states: off and on — or false / true — or 0 / 1. byte: 8 bits nybble: 4 bits or half a byte word: 4 bytes kilo (K) = 210 = 1,024 mega (M) = 220 = 1,048,576 giga (G) = 230 = 1,037,741,824 A 64KB computer from the early 1970’s would have had 64 × 1024 or 65,536 bytes of memory. A 512MB machine would have 512 × 1,048,576 or 536,870,912 bytes of memory.

slide-28
SLIDE 28

Mat 2170 Week 10 Classes & Objects Week 10 Classes Contents Creating Inheritance Methods Lab 10 The GSquare Definition The GSmartSquare Class Definition Notes Access Review

Binary notation — a Byte

128 64 32 16 8 1 = = = = = = = = 32 8 2 4 2

1 1 1

42

slide-29
SLIDE 29

Mat 2170 Week 10 Classes & Objects Week 10 Classes Contents Creating Inheritance Methods Lab 10 The GSquare Definition The GSmartSquare Class Definition Notes Access Review

Hexadecimal — Base 16 — 4 Bits Each Hex digit Value A 10 B 11 C 12 D 13 E 14 F 15

1 1 1 2 A 2 A

10 x 1 = 10 2 x 16 = 32 42

slide-30
SLIDE 30

Mat 2170 Week 10 Classes & Objects Week 10 Classes Contents Creating Inheritance Methods Lab 10 The GSquare Definition The GSmartSquare Class Definition Notes Access Review

Memory and Addresses

Within the memory system of a typical computer, every byte is identified by a numeric address, starting at 0.

0001 1001 1002 0002 0003 1000

FFFE FFFD

0000

FFFF

If we use two bytes to hold addresses, the largest address is: FFFF16 or 15 × 163 + 15 × 162 + 15 × 161 + 15 × 160 or 65,535. It takes 4 bytes, or a word, to be able to address a gigabyte of memory.

slide-31
SLIDE 31

Mat 2170 Week 10 Classes & Objects Week 10 Classes Contents Creating Inheritance Methods Lab 10 The GSquare Definition The GSmartSquare Class Definition Notes Access Review

Allocating Memory

Whenever a variable or object is declared, the compiler must reserve space in memory to hold its value/s. allocation: the process of reserving memory space. Memory is allocated from one of three different regions of memory depending on how an object is declared:

  • 1. The beginning of memory
  • 2. The stack
  • 3. The heap
slide-32
SLIDE 32

Mat 2170 Week 10 Classes & Objects Week 10 Classes Contents Creating Inheritance Methods Lab 10 The GSquare Definition The GSmartSquare Class Definition Notes Access Review

memory for program code and static data pool of memory (the "stack") local variables memory for

toward lower addresses the stack grows toward higher addresses the heap grows

(the "heap") available for objects

Memory regions

slide-33
SLIDE 33

Mat 2170 Week 10 Classes & Objects Week 10 Classes Contents Creating Inheritance Methods Lab 10 The GSquare Definition The GSmartSquare Class Definition Notes Access Review

Allocation Strategies — Beginning or Low Memory

Static variables and constants. static variables belong to the whole class, not to an individual object. This group is usually allocated at the beginning of the memory space. Program instructions are also stored at the beginning of memory.

slide-34
SLIDE 34

Mat 2170 Week 10 Classes & Objects Week 10 Classes Contents Creating Inheritance Methods Lab 10 The GSquare Definition The GSmartSquare Class Definition Notes Access Review

Allocation Strategies — The Heap

Dynamically allocated objects. All objects created using new are assigned storage from a region of memory called the heap. Convention places the heap memory immediately after the fixed region assigned to the static declarations of a class. The heap grows toward higher addresses.

slide-35
SLIDE 35

Mat 2170 Week 10 Classes & Objects Week 10 Classes Contents Creating Inheritance Methods Lab 10 The GSquare Definition The GSmartSquare Class Definition Notes Access Review

Allocation Strategies — The Stack

Local variables. All variables that are declared as local variables inside a method are allocated from a region of memory called the stack. Convention places the beginning of stack memory at the highest legal address in memory. The stack grows toward lower addresses as new methods are called.

slide-36
SLIDE 36

Mat 2170 Week 10 Classes & Objects Week 10 Classes Contents Creating Inheritance Methods Lab 10 The GSquare Definition The GSmartSquare Class Definition Notes Access Review

Local variables — Continued. When a method is invoked, the size of the stack increases by the amount needed to hold the local variables that method declares. The memory set aside for a particular method is called a stack frame. When a method returns, its stack frame is discarded, restoring the frame of its caller.

slide-37
SLIDE 37

Mat 2170 Week 10 Classes & Objects Week 10 Classes Contents Creating Inheritance Methods Lab 10 The GSquare Definition The GSmartSquare Class Definition Notes Access Review

WIK: What’s Important to Know

A reference is simply the memory address of an object. We are able to send a message to an object via a reference to it. When a parameter is passed to a method, a copy of its value is made.

slide-38
SLIDE 38

Mat 2170 Week 10 Classes & Objects Week 10 Classes Contents Creating Inheritance Methods Lab 10 The GSquare Definition The GSmartSquare Class Definition Notes Access Review

WIK

For primitive types, this means a copy of the value is made — the original remains untouched. When an class object is passed as a parameter to a method, a copy of the reference to that object (its address in memory) is made. Changes, if any, are then made to the original object. Non-primitive variables (objects) are references (memory addresses).

slide-39
SLIDE 39

Mat 2170 Week 10 Classes & Objects Week 10 Classes Contents Creating Inheritance Methods Lab 10 The GSquare Definition The GSmartSquare Class Definition Notes Access Review

WIK

When we pass a reference as a parameter to a method the method still just gets a copy of the argument. However, having a copy of a reference is almost like having the original, since it’s the memory address of the object. The null literal is a reference that refers to no object. Any non-primitive variable can be set equal to null, which is sometimes a useful way to initialize it.

slide-40
SLIDE 40

Mat 2170 Week 10 Classes & Objects Week 10 Classes Contents Creating Inheritance Methods Lab 10 The GSquare Definition The GSmartSquare Class Definition Notes Access Review

The javadoc Documentation System

One of the important ways in which Java works together with the World–Wide–Web is in the design of its documentation system, javadoc. The javadoc program reads Java source files and generates documentation for each class in the file. ACM Java Libraries complete documentation located at: http://jtf.acm.org/javadoc/student/

slide-41
SLIDE 41

Mat 2170 Week 10 Classes & Objects Week 10 Classes Contents Creating Inheritance Methods Lab 10 The GSquare Definition The GSmartSquare Class Definition Notes Access Review

Writing javadoc Comments

The javadoc program from the Java developer’s kit (JDK) produces nicely formatted HTML documentation from special comments embedded in the source code. To make this work with your own programs, you need to add specially formatted comments to your code. A javadoc comment begins with the characters /** and extends up to the closing */ just as a regular comment does. Using documentation comments helps programmers produce accurate and consistent documentation.

slide-42
SLIDE 42

Mat 2170 Week 10 Classes & Objects Week 10 Classes Contents Creating Inheritance Methods Lab 10 The GSquare Definition The GSmartSquare Class Definition Notes Access Review

Documentation Comments

Documentation comments begin with /** and end with */. Lines that begin with @ are tags that have special meaning. @author Starts a paragraph where one or more author names may be entered @param Starts a parameter description for a function pa- rameter with the given name @return Starts a return value description for a function Java defines a few more tags and it is possible to define your own. However, you only need to know the above tags for now. Note: you can also add html formatting to your documentation comments.

slide-43
SLIDE 43

Mat 2170 Week 10 Classes & Objects Week 10 Classes Contents Creating Inheritance Methods Lab 10 The GSquare Definition The GSmartSquare Class Definition Notes Access Review

Documentation Comments: Examples

/** * Moves the GMovingSquare according to the current * displacement. */ public void move() /** * Returns a copy of the current displacement (velocity) of * the GMovingSquare. * @return a copy of the current velocity */ public GPoint getDisplacement() /** * Sets the displacement (velocity) of the GMovingSquare * to (x,y). * @param x the x component of the new displacement * @param y the y component of the new displacement */ public void setDisplacement(double x, double y)

slide-44
SLIDE 44

Mat 2170 Week 10 Classes & Objects Week 10 Classes Contents Creating Inheritance Methods Lab 10 The GSquare Definition The GSmartSquare Class Definition Notes Access Review

Sample javadoc Pages — RandomGenerator

slide-45
SLIDE 45

Mat 2170 Week 10 Classes & Objects Week 10 Classes Contents Creating Inheritance Methods Lab 10 The GSquare Definition The GSmartSquare Class Definition Notes Access Review

Constructor and Method Summaries

slide-46
SLIDE 46

Mat 2170 Week 10 Classes & Objects Week 10 Classes Contents Creating Inheritance Methods Lab 10 The GSquare Definition The GSmartSquare Class Definition Notes Access Review

Constructor and Method Details

slide-47
SLIDE 47

Mat 2170 Week 10 Classes & Objects Week 10 Classes Contents Creating Inheritance Methods Lab 10 The GSquare Definition The GSmartSquare Class Definition Notes Access Review

slide-48
SLIDE 48

Mat 2170 Week 10 Classes & Objects Week 10 Classes Contents Creating Inheritance Methods Lab 10 The GSquare Definition The GSmartSquare Class Definition Notes Access Review