Download letter

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Chapter 3
Classes and Methods
Knowledge Goals
• Appreciate the difference between a class in
the abstract sense and a class as a Java
construct
• Know what attributes are
• Know what responsibilities are
• Know how assignment of an object reference
differs from assignment of a standard type
2
Knowledge Goals
•
•
•
•
•
•
3
Appreciate how aliases can lead to errors
Know what garbage collection is
Understand the distinction between instance
methods and class methods
Know what a constructor does
Appreciate the difference between void and
value-returning methods
Understand what immutability is
Skill Goals
• Determine the attributes and responsibilities
of a class
• Write the heading for a new class
• Write the class declaration for a new class
• Write an instance method
• Write a class method
• Write a constructor
4
Skill Goals
• Write a helper method
• Write a value-returning method
• Assemble class declarations into a working
class
5
New Classes of Objects
A new class is just like an application
except that it doesn't have a main method
6
New Classes of Objects
Can you
name
some
attributes
for the
cat class
?
7
New Classes of Objects
Instance values
A value that is associated with a specific object
Class value
A value that is associated with a class and is the
same for every object of the class
Attributes
The values defined by a class that are used to
represent its objects; the combination of instance
and class values
Were any cat attributes class values?
8
New Classes of Objects
See the
difference
between
class
and
instance
values?
9
New Classes of Objects
A class
and
three
instantiated
objects
10
New Classes of Objects
With each new class comes responsibilities…
Responsibilities
The operations that are provided by a class of
objects
Class responsibility
An operation performed with respect to a class
of objects
Instance responsibility
An operation performed with respect to a
specific object
11
New Classes of Objects
Method
A Java subprogram
Class method
A method that implements a class responsibility
Instance method
A method that implements an instance
responsibility
12
New Classes of Objects
Constructor
An operation (responsibility) that creates a new
instance of a class
Constructor (in Java)
A method with the same name as the class,
which is neither a void or a value-returning
method
How many kinds of attributes and
responsibilities are there?
13
New Classes and Objects
Every class
must have
a
constructor;
if you don't
define one,
Java
supplies
one
14
Overview of Java Data Types
15
Overview of Java Data Types
Primitive values require a fixed number of bytes;
object values can be any size, so…
16
Overview of Java Data Types
A variable of a primitive type contains the
value of the variable
A variable of an object type contains the
address of where the value (object) begins
17
Overview of Java Data Types
18
Overview of Java Data Types
char letter;
String title;
letter
String book;
letter = ‘J’;
title = “Problem Solving”;
book = title;
19
Example
Walkthrough
Overview of Java Data Types
char letter;
String title;
letter
String book;
letter = ‘J’;
title
title = “Problem Solving”;
book = title;
20
Example
Walkthrough
Overview of Java Data Types
char letter;
String title;
letter
String book;
letter = ‘J’;
title = “Problem Solving”;
book = title;
21
title
book
Example
Walkthrough
Overview of Java Data Types
char letter;
String title;
letter
‘J’
String book;
letter = ‘J’;
title = “Problem Solving”;
book = title;
22
title
book
Example
Walkthrough
Overview of Java Data Types
Memory Location 2003
char letter;
“Problem Solving”
String title;
letter
‘J’
String book;
letter = ‘J’;
title = “Problem Solving”;
book = title;
23
title
2003
book
Example
Walkthrough
Overview of Java Data Types
Memory Location 2003
char letter;
“Problem Solving”
String title;
letter
‘J’
String book;
letter = ‘J’;
title = “Problem Solving”;
book = title;
24
title
2003
book 2003
Example
Walkthrough
Overview of Java Data Types
Memory Location 2003
char letter;
“Problem Solving”
String title;
letter
‘J’
String book;
letter = ‘J’;
title = “Problem Solving”;
book = title;
25
title
2003
book 2003
Walkthrough
Complete
Overview of Java Data Types
Alias
When
multiple
variables
refer
to the
same
object,
acting as
synonyms
26
Overview of Java Data Types
Address 000011010101 is unreachable
27
Recycling Discarded Objects
Garbage
Objects that are unreachable
Garbage collection
Recycling of memory when objects are no
longer needed
Explicit memory management
Recycling of object memory that must be
manually coded
Memory leak
Failure to recycle memory when explicit
memory management is being used
28
Java
has
automatic
garbage
collection
Responsibilities as Methods
Heading
Body
Parameter list contains information the
method must have in order to execute; this
information is given at the call
29
Responsibilities as Methods
Arguments
substituted
for
parameters
at time
of call
30
Responsibilities as Methods
31
Responsibilities as Methods
Class variable
Instance variables
How does Java distinguish them?
32
Responsibilities as methods
Review
void methods
Called as a statement
value-returning methods
Called in an expression; return a value
33
Responsibilities as methods
Constructor
An operation (method) that creates a new
instance of a class





34
Same name as class
Default constructor (no parameters)
Parameterized constructor (with parameters)
A class can have more than one constructor
Is almost always public (Why?)
Responsibilities as methods
Observer
An operation that returns information from an
object without changing the content of the object
Immutability
The property of a class that its instances cannot
be modified once they are created
Constructor-Observer classes
Classes that have only constructors and
observers and are therefore immutable (all
we've seen so far)
35
Cat Class
public class Cat
{
String name;
String color;
float weight;
// constructors
public Cat()
{
name = " ";
color = " ";
weight = 0.0;
}
36
Cat Class
public Cat(String newName, String newColor,
float newWeight)
{
name = newName;
weight = newWeight;
colr = newColor;
}
public String getName()
{
return name;
}
Methods that return the value of a variable
are often "get" + identifier. (Why?)
37
Cat Class
public float getWeight()
{
return weight;
}
public String getColor()
{
return color;
}
}
Is class Cat immutable?
38
Driver for Cat Class
public class CatDriver
{
public static void main (String[] args)
{
Cat myCat = new Cat("Meow", "ginger", 10.5);
Cat cat = new Cat();
System.out.print("myCat ");
System.out.println(myCat.getName() + " " +
myCat.getColor() + " " + myCat.getWeight());
System.out.print("cat ");
System.out.println(cat.getName() + " " +
cat.getColor() + " " + cat.getWeight());
}
}
39
Extras
I am
called the
first
programmer
Can you
remember
my name?
My
father's
name?
40
Related documents