Objects and Classes - PowerPoint PPT Presentation

About This Presentation
Title:

Objects and Classes

Description:

Objects and Classes – PowerPoint PPT presentation

Number of Views:240
Avg rating:3.0/5.0
Slides: 38
Provided by: YDanie5
Category:

less

Transcript and Presenter's Notes

Title: Objects and Classes


1
Objects and Classes
2
Objects and Classes
  • OO Programming Concepts
  • Creating Objects and Object Reference Variables
  • Differences between primitive data type and
    object type
  • Automatic garbage collection
  • Constructors
  • Modifiers (public, private and static)
  • Instance and Class Variables and Methods
  • Scope of Variables
  • Use the this Keyword
  • Case Studies (Mortgage class and Count class)

3
OO Programming Concepts
4
Class and Objects
5
Class Declaration
  • class Circle
  • double radius 1.0
  • double findArea()
  • return radius radius 3.14159

6
Declaring Object Reference Variables
  • ClassName objectReference
  • Example
  • Circle myCircle

7
Creating Objects
  • objectReference new ClassName()
  • Example
  • myCircle new Circle()
  • The object reference is assigned to the object
    reference variable.

8
Declaring/Creating Objectsin a Single Step
  • ClassName objectReference new ClassName()
  • Example
  • Circle myCircle new Circle()

9
Differences between variables of primitive Data
types and object types
10
Copying Variables of Primitive Data Types and
Object Types
11
Garbage Collection
  • As shown in the previous figure, after the
    assignment statement c1 c2, c1 points to the
    same object referenced by c2. The object
    previously referenced by c1 is no longer useful.
    This object is known as garbage. Garbage is
    automatically collected by JVM.

12
Garbage Collection, cont
  • TIP If you know that an object is no longer
    needed, you can explicitly assign null to a
    reference variable for the object. The Java VM
    will automatically collect the space if the
    object is not referenced by any variable.

13
Accessing Objects
  • Referencing the objects data
  • objectReference.data
  • myCircle.radius
  • Invoking the objects method
  • objectReference.method
  • myCircle.findArea()

14
Constructors
  • Circle(double r)
  • radius r
  • Circle()
  • radius 1.0
  • myCircle new Circle(5.0)

Constructors are a special kind of methods that
are invoked to construct objects.
15
Constructors, cont.
A constructor with no parameters is referred to
as a default constructor.        Constructors
must have the same name as the class itself.
       Constructors do not have a return
typenot even void.        Constructors are
invoked using the new operator when an object is
created. Constructors play the role of
initializing objects.
16
Visibility Modifiers and Accessor Methods
  • By default, the class, variable, or data can
    beaccessed by any class in the same package.
  • public
  • The class, data, or method is visible to any
    class in any package.
  • private
  • The data or methods can be accessed only by the
    declaring class.
  • The get and set methods are used to read and
    modify private properties.

17
Passing Objects to Methods, cont.
18
Instance Variables, and Methods
Instance variables belong to a specific
instance.Instance methods are invoked by an
instance of the class.
19
Class Variables, Constants, and Methods
Class variables are shared by all the instances
of the class.Class methods are not tied to a
specific object. Class constants are final
variables shared by all the instances of the
class.
20
Class Variables, Constants, and Methods, cont.
To declare class variables, constants, and
methods, use the static modifier.
21
Class Variables, Constants, and Methods, cont.
22
Scope of Variables
  • The scope of instance and class variables is the
    entire class. They can be declared anywhere
    inside a class.
  • The scope of a local variable starts from its
    declaration and continues to the end of the block
    that contains the variable. A local variable must
    be declared before it can be used.

23
The Keyword this
  • Use this to refer to the current object.
  • Use this to invoke other constructors of the
    object.

24
Array of Objects
  • Circle circleArray new Circle10
  • An array of objects is actually an array of
    reference variables. So invoking
    circleArray1.findArea() involves two levels of
    referencing as shown in the next figure.
    circleArray references to the entire array.
    circleArray1 references to a Circle object.

25
Array of Objects, cont.
  • Circle circleArray new Circle10

26
Class Abstraction
  • Class abstraction means to separate class
    implementation from the use of the class. The
    creator of the class provides a description of
    the class and let the user know how the class can
    be used. The user of the class does not need to
    know how the class is implemented. The detail of
    implementation is encapsulated and hidden from
    the user.

27
Example 6.8 The Mortgage Class
Mortgage
TestMortgageClass
Run
28
Example 6.9 The Count Class
Run
TestVoteCandidate
29
The Object Class
  • The Object class is the root of all Java classes.
  • The equals() method compares thecontents of two
    objects.
  • The toString() method returns a string
    representation of the object.
  • The clone() method copy objects

30
The Object Class, cont.
  • The equals() method compares thecontents of two
    objects. The default implementation of the equals
    method in the Object class is as follows
  • public boolean equals(Object obj)
  • return (this obj)

31
NOTE
The comparison operator is used for comparing
two primitive data type values or for determining
whether two objects have the same references. The
equals method is intended to test whether two
objects have the same contents, provided that the
method is modified in the defining class of the
objects. The operator is stronger than the
equals method, in that the operator checks
whether the two reference variables refer to the
same object.
32
The Object Class, cont.
  • The toString() method returns a string
    representation of the object. The default
    implementation returns a string consisting of a
    class name of which the object is an instance,
    the at sign (_at_), and a number representing this
    object.
  • The clone() method copy objects

33
The Object Class, cont.
  • To create a new object with separate memory
    space, you need to use the clone() method, as
    follows
  • newObject someObject.clone()
  • NOTE Not all objects can be cloned. For an
    object to be cloneable, its class must implement
    the java.lang.Cloneable interface. Interfaces are
    introduced in the section "Interfaces," in this
    chapter.

34
The final Modifier
  • The final class cannot be extended
  • final class Math
  • ...
  • The final variable is a constant
  • final static double PI 3.14159
  • The final method cannot bemodified by its
    subclasses.

35
Java API and Core Java classes
  • java.lang
  • Contains core Java classes, such as numeric
    classes, strings, and objects. This package is
    implicitly imported to every Java program.
  • java.awt
  • Contains classes for graphics.
  • java.applet
  • Contains classes for supporting applets.

36
Java API and Core Java classes, cont.
  • java.io
  • Contains classes for input and outputstreams and
    files.
  • java.util
  • Contains many utilities, such as date.
  • java.net
  • Contains classes for supportingnetwork
    communications.

37
Java API and Core Java classes, cont.
  • java.awt.image
  • Contains classes for managing bitmap images.
  • java.awt.peer
  • Platform-specific GUI implementation.
  • Others
  • java.sql
  • java.rmi
Write a Comment
User Comments (0)
About PowerShow.com