Object Based Programming Chapter 8. 2 In This Chapter We will learn about classes Garbage Collection...

29
Object Based Programming Chapter 8

Transcript of Object Based Programming Chapter 8. 2 In This Chapter We will learn about classes Garbage Collection...

Page 1: Object Based Programming Chapter 8. 2 In This Chapter We will learn about classes Garbage Collection Data Abstraction and encapsulation.

Object Based Programming

Chapter 8

Page 2: Object Based Programming Chapter 8. 2 In This Chapter We will learn about classes Garbage Collection Data Abstraction and encapsulation.

2

In This Chapter We will learn about

classes

Garbage Collection

Data Abstraction and encapsulation

Page 3: Object Based Programming Chapter 8. 2 In This Chapter We will learn about classes Garbage Collection Data Abstraction and encapsulation.

3

Contrast Procedural Languages

Action orientedConcentrate on writing functionsData supports the actions

Object Oriented LanguagesConcentrate on creating reference typesLook for nouns which indicate classes

Fields are data members Methods support manipulation of the objects

Page 4: Object Based Programming Chapter 8. 2 In This Chapter We will learn about classes Garbage Collection Data Abstraction and encapsulation.

4

Find the Objects … the Verbs

In this game, what are nouns that are potential classes?

In this game, what are nouns that are potential classes?

What are verbs that represent things that the

objects do?

What are verbs that represent things that the

objects do?

Page 5: Object Based Programming Chapter 8. 2 In This Chapter We will learn about classes Garbage Collection Data Abstraction and encapsulation.

5

Abstract Data Types (ADTs) Classes in Java make creation of ADTs

easier Implementation details hidden from users of the

classClient code not dependent on implementation

Example of a classFigure 8.1 Time class – keeps track of time in 24 hour

format

Page 6: Object Based Programming Chapter 8. 2 In This Chapter We will learn about classes Garbage Collection Data Abstraction and encapsulation.

6

Declaring Classes Usually data fields are specified as private Methods are declared as public Any class member (data or method) which

does not need to be accessed outside the class should be private

It does not matter which comes first, data fields or methodsThe author prefers data fields first

Page 7: Object Based Programming Chapter 8. 2 In This Chapter We will learn about classes Garbage Collection Data Abstraction and encapsulation.

7

Constructors Methods which have the same name as the

class Included in a class to ensure that instance

variables contain valid values when objects are created

The constructor is called when a class object is created with the new

Note:Do not have the constructor return a value

Page 8: Object Based Programming Chapter 8. 2 In This Chapter We will learn about classes Garbage Collection Data Abstraction and encapsulation.

8

Using Classes View Figure 8.2 Note

Creation of a time object with new Call of the constructor Use of public methods to get and set the private data

values Use of toString functions

Advantages of classes Simplifies client perception of classes Reduces number of parameters

Page 9: Object Based Programming Chapter 8. 2 In This Chapter We will learn about classes Garbage Collection Data Abstraction and encapsulation.

9

Class Scope Variables and methods belong to the class's

scope Inside the class's scope

All class members are available, accessible Outside the class's scope

Client code that uses the classOnly public members are available, accessible

Access modifiers public and private control this availability

Page 10: Object Based Programming Chapter 8. 2 In This Chapter We will learn about classes Garbage Collection Data Abstraction and encapsulation.

10

Controlling Access to Members The following would not be allowed – why?

The data members are private.

public class TimeTest2 { public static void main( String args[] ) { Time1 t = new Time1(); t.hour = 7; }}

public class TimeTest2 { public static void main( String args[] ) { Time1 t = new Time1(); t.hour = 7; }}

Page 11: Object Based Programming Chapter 8. 2 In This Chapter We will learn about classes Garbage Collection Data Abstraction and encapsulation.

11

Using the this Reference Every object can access a reference to itself

Use the keyword this If a method has a local variable with same

name as class variable, use the this to access the class variable

Explicit use of this can increase program clarity for human readers

Note example, Figure 8.4

Page 12: Object Based Programming Chapter 8. 2 In This Chapter We will learn about classes Garbage Collection Data Abstraction and encapsulation.

12

Using Overloaded Constructors Multiple constructors for the same class are

allowed The signature of the constructors must be

differentDifferent numbers and/or types of parameters

See example in Figure 8.5 and 8.6

Page 13: Object Based Programming Chapter 8. 2 In This Chapter We will learn about classes Garbage Collection Data Abstraction and encapsulation.

13

Using Set and Get Methods Private data fields manipulated only by

provided public methodsSome methods used to set the data valuesOthers used to get the values

Return the value directly Print the value

Note: this is not the same as making the data values publicThe set methods are written to ensure valid data

values

Page 14: Object Based Programming Chapter 8. 2 In This Chapter We will learn about classes Garbage Collection Data Abstraction and encapsulation.

14

Composition A class can have references to objects of

other classes 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, Figure 8.7 Employee Class Figure 8.8 Employee Test program Figure 8.9

Page 15: Object Based Programming Chapter 8. 2 In This Chapter We will learn about classes Garbage Collection Data Abstraction and encapsulation.

15

Enumerations – enum typesDeclared with an enum declarationA comma-separated list of enum

constantsDeclares an enum class with the following

restrictions:enum types are implicitly finalenum constants are implicitly staticAttempting to create an object of an enum

type with new is a compilation error

Page 16: Object Based Programming Chapter 8. 2 In This Chapter We will learn about classes Garbage Collection Data Abstraction and encapsulation.

16

Enumerations – enum typesenum constants can be used anywhere

constants canenum constructor

Like class constructors, can specify parameters and be overloaded

Note use from chapter 6, Figure 6.9See example, Figure 8.10Note test program, Figure 8.11

Page 17: Object Based Programming Chapter 8. 2 In This Chapter We will learn about classes Garbage Collection Data Abstraction and encapsulation.

17

Enumerations – enum typesstatic method values

Generated by the compiler for every enumReturns an array of the enum’s constants in the

order in which they were declaredstatic method range of class EnumSet

Takes two parameters, the first and last enum constants in the desired range

Returns an EnumSet containing the constants in that range, inclusive

An enhanced for statement can iterate over an EnumSet as it can over an array

Page 18: Object Based Programming Chapter 8. 2 In This Chapter We will learn about classes Garbage Collection Data Abstraction and encapsulation.

18

Enumeration as a Class When an enumeration is defined

A class is createdDefault methods include

toString equals ordinal valueOf

Also possible to provide additional methods

Page 19: Object Based Programming Chapter 8. 2 In This Chapter We will learn about classes Garbage Collection Data Abstraction and encapsulation.

19

Enumeration as a Class

Consider the following example of an enumeration of card suits

View definition of class SuitNote constructor, getColor

View demonstration program

Page 20: Object Based Programming Chapter 8. 2 In This Chapter We will learn about classes Garbage Collection Data Abstraction and encapsulation.

20

View class LetterGrade An enumeration classNote class elements

Consider the following code

LetterGrade myGrade = LetterGrade.B_PLUS;

Discuss the results of calls to various LetterGrade methods

Enumeration as a Class

Page 21: Object Based Programming Chapter 8. 2 In This Chapter We will learn about classes Garbage Collection Data Abstraction and encapsulation.

21

Garbage Collection Garbage collection

JVM marks an object for garbage collection when there are no more references to that object

JVM’s garbage collector will retrieve those objects memory so it can be used for other objects

See lines 27 – 32 of Figure 8.13 finalize method

All classes in Java have the finalize method Inherited from the Object class

finalize is called by the garbage collector when it performs termination housekeeping

finalize takes no parameters and has return type void

Page 22: Object Based Programming Chapter 8. 2 In This Chapter We will learn about classes Garbage Collection Data Abstraction and encapsulation.

22

static Class Members Normally each class object has its own copy of the

instance variables Possible to specify a variable that is shared by all

existing objects of the class Called a static variable Also called a class variable – class wide information

Also possible to have a static method Can be called even if no object of the class has been

instantiated Use class name, then dot, then static method name

Note examples in Fig. 8.12, test pgm Fig. 8.13

Page 23: Object Based Programming Chapter 8. 2 In This Chapter We will learn about classes Garbage Collection Data Abstraction and encapsulation.

23

static Class MembersString objects are immutable

String concatenation operations actually result in the creation of a new String object

static methods cannot access non-static class members

Also cannot use the this reference

Page 24: Object Based Programming Chapter 8. 2 In This Chapter We will learn about classes Garbage Collection Data Abstraction and encapsulation.

24

static Class Members static method gc of class System

Indicates that the garbage collector should make a best-effort attempt to reclaim objects eligible for garbage collection

It is possible that no objects or only a subset of eligible objects will be collected

Note example call in Fig. 8.13

Page 25: Object Based Programming Chapter 8. 2 In This Chapter We will learn about classes Garbage Collection Data Abstraction and encapsulation.

25

static Import Recall static fields and methods of class

Math J2SE 5.0 enables importing static members

as if declared in class where used Note Figure 8.14import static java.lang.Math.*

Page 26: Object Based Programming Chapter 8. 2 In This Chapter We will learn about classes Garbage Collection Data Abstraction and encapsulation.

26

Final Instance Variables Principle of least privilege

Code should have only the privilege and access it needs to accomplish its task, but no more

final instance variablesKeyword final

Specifies that a variable is not modifiable (is a constant)

final instance variables can be initialized at their declaration

If they are not initialized in their declarations, they must be initialized in all constructors – Fig. 8.15

Page 27: Object Based Programming Chapter 8. 2 In This Chapter We will learn about classes Garbage Collection Data Abstraction and encapsulation.

27

Packages Packages enable grouping together multiple

related classes Specify a class to be part of a package with

first linepackage myStuff;

Place all classes in same directory which is named with the name of the package

In your program which uses the packageimport myStuff.*;

Page 28: Object Based Programming Chapter 8. 2 In This Chapter We will learn about classes Garbage Collection Data Abstraction and encapsulation.

28

Software Reusability Many classes exist in the Java API

Avoid "reinventing the wheel"Study capabilities of Java API If it has a class that fits your needs, use it rather

than creating your own

Page 29: Object Based Programming Chapter 8. 2 In This Chapter We will learn about classes Garbage Collection Data Abstraction and encapsulation.

29

Data Abstraction & Encapsulation Information hiding

Classes hide details of implementation from client

Example:Stack can be implemented with array or with

linked listClient program which depends on one

implementation will have problems if new version with different implementation comes out

Only use the methods provided