Download Object Based Programming

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

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

Document related concepts
no text concepts found
Transcript
Object Based Programming
Chapter 8
Contrast
• Procedural Languages
– _____________ oriented
– Concentrate on writing _____________
– Data supports the actions
• Object Oriented Languages
– Concentrate on creating _________________
– Look for nouns which indicate classes
• ______________ are data members
• Methods support manipulation of the _____________
2
Find the Objects … the Verbs
What are verbs that
represent things that the
objects do?
In this game, what
are nouns that are
potential classes?
3
List Objects, Actions
Objects
Actions
4
Abstract Data Types (ADTs)
• ____________ in Java make creation of
ADTs easier
– Implementation details _________________
from users of the class
– Client code not dependent on _______________
• Example of a class
– Figure 8.1
– Time class – keeps track of time in 24 hour
format
5
Declaring Classes
• Usually data fields are specified as
________
• Methods are declared as ______________
• Any class member (data or method) which
does not need to be accessed __________
________________ should be private
• It does not matter which comes first, data
fields or methods
– The author prefers data fields first
6
Constructors
• Methods which have the _______________
as the class
• Included in a class to ensure that instance
variables contain __________________
when objects are created
• The constructor is called when a class object
is _____________________________
• Note:
– Do not have the constructor return a value
7
Using Classes
• View Figure 8.2
• Note
– Creation of a time object with new
– Call of the constructor
– Use of public functions to _____________________ the
private data values
– Use of toString functions
– Use of DecimalFormat class
• Advantages of classes
– Simplifies ________________________ of classes
– Reduces number of _____________________
8
Class Scope
• __________________________________
belong to the class's scope
• ______________ the class's scope
– All class members are available, accessible
• Outside the class's scope
– Client code that uses the class
– Only ______________ are available, accessible
• __________________ public and private
control this availability
9
Controlling Access to Members
• The following would not be allowed – why?
public class TimeTest2 {
public static void main( String args[] )
{
Time1 t = new Time1();
t.hour = 7;
}
}
• The data members are
.
10
Using the this Reference
• Every object can access a reference to itself
– Use the keyword _____________
• If a method has a local variable with
____________________ as class variable,
use the this to access the class variable
• Explicit use of this can increase program
_____________________ for human readers
• Note example, Figure 8.4 in text
11
Using Overloaded Constructors
• ________________________ for the same
class are allowed
• The ___________________________ of the
constructors must be different
– Different _____________ and/or ___________
of parameters
• See example in Figure 8.5 and 8.6
12
Using Set and Get Methods
• Private data fields manipulated only by
provided ________________
– Some methods used to set the data values
– Others used to get the values
• Return the value _____________
• _____________ the value
• Note: this is not the same as making the data
values _______________
– The set methods are written to
______________________________
13
Using Set and Get Methods
• View new version of Time class, Figure 8.7
• View the use of the Time3 class, Figure 8.8
• Note:
– Creation of the Time3 object
– _________________ the event handlers
– Use of if – else – if sequence to perform the
specified action
– The displayTime() method
– The tick() method
14
Composition
• A class can have references to objects of
_____________________ as members
• Example:
– An alarm clock class contains a Time object as
one of its members
– Employee class has Date object as one of its
members
– See
• Date Class
• Employee Class
15
Garbage Collection
• When constructors initialize an object's variables
– Memory is acquired, allocated by OS
– Eventually that memory must be _________________
lest this resource is squandered
• Java performs automatic ____________ collection
–
–
–
–
Returns memory of objects no longer in use
Happens when references to an object _____________
Object is ___________ for garbage collection
Garbage collection can be called for explicitly with the
____________________ function
• Exists for every class
• A void class, no parameters (See example Fig. 8.12, text)
16
Static Class Members
• Normally each class object has its own copy of the
__________________ variables
• Possible to specify a variable that is ___________
by all existing objects of the class
– Called a _________________ variable
– Also called a ________ variable – class wide information
• Also possible to have a static _____________
– Can be called even if _______________ of the class has
been instantiated
– Use ___________, then dot, then static method name
• Note example in Figure 8.12 in text
17
Final Instance Variables
• Like ___________ identifiers in C++
• Used when a variable is not supposed to be
modified
– Example of principle of __________________
• Must be initialized at __________________
– Otherwise cannot be modified!
• By convention, the variable name is ______
– Helps human readers see programmer intent
18
Creating Packages
• A ___________ is a group of related classes
and interfaces
• To create a package
– Declare a ________ class
– Specify a package name
•
•
•
•
________ per source code file
Must ________ all other declarations, statements
Last part of name is __________ name where stored
Example: package com.deitel.jhtp4.ch08;
19
static Import
• Recall static fields and methods of class
Math
• J2SE 5.0 enables importing static members
as if _________________________
• Note Figure 8.14
import static java.lang.Math.*
20
Creating Packages
– Compile class so placed in correct directories
• Rest of the name is _______________
• Compiler __________ directories as needed, places
compiled .class file in last of sequence
– Finally, _________ the
reusable class into a
program and use
the class
// Java extension packages
import javax.swing.*;
// Deitel packages
import com.deitel.jhtp4.ch08.Time2; . . .
21
Package Access
• When no access modifier specified for class
element
– Then it is considered to have _____________.
• If program uses multiple classes from same
package
– Classes can access each other's packageaccess elements directly through
________________________________
22
Package Access
• View example, Figure 8.18
• Every Java class has its own _________ file
• When PackageDataTest is compiled
– A ______________ for PackageDataTest class
is created
– A class file for the PackageData also created
– They are placed in same ________________
• Because they are part of the same package
– PackageDataTest allowed to _________
23
package access data of objects of PackageData
Software Reusability
• Many classes exist in the Java API
• Avoid "reinventing the wheel"
– _________________ of Java API
– If it has a class that fits your needs, use it rather
than creating your own
24
Data Abstraction & Encapsulation
• Information hiding
– Classes _______________ of implementation
from client
• Example:
– ____________ can be implemented with array or
with linked list
– _______________ which depends on one
implementation will have problems if new version
with different implementation comes out
• Only use the methods provided
25