5.1 Basics of defining and using classes. 5.1.1 A review of class and object definitions A class is...

Post on 19-Jan-2018

219 views 0 download

description

5.2.1 Designing classes

Transcript of 5.1 Basics of defining and using classes. 5.1.1 A review of class and object definitions A class is...

5.1 Basics of defining and using classes

5.1.1 A review of class and object definitions

• A class is a template or blueprint for an object• A class defines an object’s behavior &

attributes• An object knows something & how to do

something• An object stores information about what it knows• When an object is created from a class it is

called an instance of the class• Objects are referenced by reference variables• Object behavior is controlled by methods

5.2.1 Designing classes

5.2.2 Using classes

• Use the class keyword to define the class, along with attributes and methods

• Two steps to create an object: Declare a reference variable, then use the new operator to create an instance of the class

• Use the dot operator to access methods and attributes of the object

5.3.1 Attributes

5.3.2 Variables

• Instance variables hold data that is associated with each object, or instance of the class

• Class data is held in static variables. The class has only one copy of the class data, and it is available to all objects created from the class.

• To make a value immutable, use the final keyword.

5.4.2 Encapsulation & Access modifiers

• Encapsulation is the concept of hiding the internal details of an object

• Access modifiers are used to control access to object or class data

5.5.1 The constructor process • Objects are constructed when the new operator is used• An objects instance variables are initialized to default

variables• Method variables are not assigned default values

5.5.2 Constructors

• Constructor methods are called when the object is created

• If the class does not define a constructor, the compiler inserts one that does nothing

• If a class does include a constructor, the compiler does not insert one

5.6.1 Methods & method signatures

• An object invokes the method of another object to calling it using the method signature

• The method signature consists of the method name and the arguments and the return type

• The type of value returned by a method is declared in the method signature

• To declare that a method returns no value, use the void keyword

5.6.6 Concept of pass-by-value

• When a method is called, the method receives a copy of the argument

• Therefore the value can be altered by the method without affecting the original

• However, if the argument is a reference to an object, the method receives a copy of the reference

• This means that the method can access the object’s non-private data

5.6.7 Method types• Instance methods are methods that are created

when the object is created• Class methods exist once the class is loaded,

not as a part of an instance• The static keyword is used to declare methods

as class methods• A special class method is the main method,

which is used as an entry point for the JVM

public static void main ( String args[ ] )

5.7.1 Using this in constructors and methods

• Every instance method has a variable called this

• this is a reference to the current object

5.8.2 Encapsulation & overloading

• Encapsulation hides the details of a class• In general, instance variables should be

private and methods should be public• Hidden instance data can be altered and

retrieved using getter and setter methods• Overloading involves having several methods

with the same name• The JVM knows the correct method to use by

the number and type of the arguments

5.10.1 Initializing data

• Instance variables are initialized with a default value based on the data type

• Instance variables can be explicitly initialized when they are declared

• Initialization blocks are blocks of statements that can initialize instance variables

• Initialization blocks can be static or non-static

5.10.2 Scope of variables

• Instance data is available as long as the object is in use or a reference to it exists

• Class data exists as soon as the class is loaded, and before any objects are created

• Method variables only exist while the method is in use, and only in the block where they were declared

• Arguments are available for the life of the method

5.10.6 Finalizers

• All objects have a finalizer method, that is called just before the JVM disposes of the object

• Any clean-up code can be included in the finalizer method

• Because the garbage collection is automatic, use of the finalizer method is often not necessary