creating object

Upload: dhekled

Post on 14-Apr-2018

220 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/27/2019 Creating Object

    1/37

    Chapter 6 Objects and Classes

    OO Programming ConceptsCreating Objects and Object Reference Variables Differences between primitive data type and object type

    Automatic garbage collectionConstructorsModifiers ( public , private and static )

    Instance and Class Variables and MethodsScope of VariablesUse the this KeywordCase Studies ( Mortgage class and Count class)

  • 7/27/2019 Creating Object

    2/37

    OO Programming Concepts

    data field 1

    method n

    data field n

    method 1

    An object

    ...

    ...

    State

    Behavior

    Data Fieldradius = 5

    MethodfindArea

    A Circle object

  • 7/27/2019 Creating Object

    3/37

    Class and Objects

    circle 1 : Circle

    radius = 2

    new Circle()

    circle n : Circle

    radius = 5

    new Circle()

    ...

    UML Graphical notation for classes

    UML Graphical notationfor objects

    Circle

    radius: double

    findArea(): double

    UML Graphical notation for fields

    UML Graphical notation for methods

  • 7/27/2019 Creating Object

    4/37

    Class Declaration

    class Circle {double radius = 1.0;

    double findArea(){return radius * radius * 3.14159;

    }}

  • 7/27/2019 Creating Object

    5/37

    Declaring Object Reference Variables

    ClassName objectReference;

    Example:Circle myCircle;

  • 7/27/2019 Creating Object

    6/37

    Creating Objects

    objectReference = new ClassName();

    Example:myCircle = new Circle();

    The object reference is assigned to the objectreference variable.

  • 7/27/2019 Creating Object

    7/37

    Declaring/Creating Objects

    in a Single StepClassName objectReference = new ClassName()

    Example:Circle myCircle = new Circle();

  • 7/27/2019 Creating Object

    8/37

    Differences between variables of primitive Data types and object types

    1

    c: Circle

    radius = 1

    Primitive type int i = 1 i

    Object type Circle c c reference

    Created usingnew Circle()

  • 7/27/2019 Creating Object

    9/37

    Copying Variables of PrimitiveData Types and Object Types

    1

    c1: Circle

    radius = 5

    Primitive type assignmenti = j

    Before:

    i

    2 j

    2

    After:

    i

    2 j

    Object type assignmentc1 = c2

    Before:

    c1

    c2

    After:

    c1

    c2

    c2: Circle

    radius = 9

  • 7/27/2019 Creating Object

    10/37

    Garbage Collection

    As shown in the previous figure,after the assignment statement c1 =c2, c1 points to the same objectreferenced by c2. The objectpreviously referenced by c1 is nolonger useful. This object is knownas garbage. Garbage isautomatically collected by JVM.

  • 7/27/2019 Creating Object

    11/37

    Garbage Collection, cont

    TIP: If you know that an object is nolonger needed, you can explicitlyassign null to a reference variablefor the object. The Java VM willautomatically collect the space if theobject is not referenced by anyvariable.

  • 7/27/2019 Creating Object

    12/37

    Accessing Objects

    Referencing the objects data: objectReference.data

    myCircle.radius

    Invoking the objects method: objectReference.method

    myCircle.findArea()

  • 7/27/2019 Creating Object

    13/37

    Example 6.1 Using Objects

    Objective: Demonstrate creating objects,

    accessing data, and using methods.

    TestCircle Run

    http://localhost/var/www/apps/conversion/tmp/scratch_8/TestCircle.htmhttp://localhost/var/www/apps/conversion/tmp/scratch_8/TestCircle.htm
  • 7/27/2019 Creating Object

    14/37

    Constructors

    Circle(double r) {radius = r;

    }

    Circle() {radius = 1.0;

    }

    myCircle = new Circle(5.0);

    Constructors are aspecial kind of methods that areinvoked to constructobjects.

  • 7/27/2019 Creating Object

    15/37

    Constructors, cont.

    A constructor with no parameters is referred toas a default constructor .

    Constructors must have the same name

    as the class itself. Constructors do not have a return type

    not even void.

    Constructors are invoked using the newoperator when an object is created.Constructors play the role of initializingobjects.

  • 7/27/2019 Creating Object

    16/37

    Example 6.2 Using Classes from

    the Java Library Objective: Demonstrate using classes from theJava library. U se the JFrame class in the

    javax.swing package to create two frames;use the methods in the JFrame class to setthe title, size and location of the frames andto display the frames.

    TestFrame Run

    http://localhost/var/www/apps/conversion/tmp/scratch_8/TestFrame.htmhttp://localhost/var/www/apps/conversion/tmp/scratch_8/TestFrame.htm
  • 7/27/2019 Creating Object

    17/37

    Example 6.3 Using Constructors

    Objective: Demonstrate the role of constructors and use them to createobjects.

    TestCircleWithConstructors Run

    http://localhost/var/www/apps/conversion/tmp/scratch_8/TestCircleWithConstructors.htmhttp://localhost/var/www/apps/conversion/tmp/scratch_8/TestCircleWithConstructors.htm
  • 7/27/2019 Creating Object

    18/37

    Visibility Modifiers andAccessor 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 declaringclass.

    The get and set methods are used to read and modify private properties.

  • 7/27/2019 Creating Object

    19/37

    Example 6.4Using the private Modifier

    and Accessor Methods

    TestCircleWithAccessors Run

    In this example, private data are used for theradius and the accessor methods getRadius andsetRadius are provided for the clients to retrieve

    and modify the radius.

    http://localhost/var/www/apps/conversion/tmp/scratch_8/TestCircleWithAccessors.htmhttp://localhost/var/www/apps/conversion/tmp/scratch_8/TestCircleWithAccessors.htm
  • 7/27/2019 Creating Object

    20/37

    Passing Objects to Methods

    Passing by value (the value is the referenceto the object)

    Example 6.5 Passing Objects as Arguments

    TestPassingObject Run

    http://localhost/var/www/apps/conversion/tmp/scratch_8/TestPassingObject.htmhttp://localhost/var/www/apps/conversion/tmp/scratch_8/TestPassingObject.htm
  • 7/27/2019 Creating Object

    21/37

    Passing Objects to Methods, cont.

    mainmethod

    Reference myCircle

    5n 5

    times

    printAreasmethod

    Reference

    c

    myCircle: Circle

    radius = 1

    Pass by value (here the value is 5)

    Pass by value (here the value is thereference for the object)

  • 7/27/2019 Creating Object

    22/37

    InstanceVariables, and Methods

    Instance variables belong to a specific instance.

    Instance methods are invoked by an instance of the class.

  • 7/27/2019 Creating Object

    23/37

  • 7/27/2019 Creating Object

    24/37

    Class Variables, Constants,and Methods, cont.

    To declare class variables, constants, and methods,use the static modifier.

  • 7/27/2019 Creating Object

    25/37

    Class Variables, Constants,and Methods, cont.

    CircleWithStaticVariable

    -radius-numOfObjects

    +getRadius(): double+setRadius(radius: double): void+getNumOfObjects(): int+findArea(): double

    1 radiuscircle1:Circle

    -radius = 1-numOfObjects = 2

    instantiate

    instantiate

    Memory

    2

    5 radius

    numOfObjects

    radius is an instancevariable, andnumOfObjects is aclass variable

    UML Notation:+: public variables or methods-: private variables or methodsunderline: static variables or metods

    circle2:Circle

    -radius = 5-numOfObjects = 2

  • 7/27/2019 Creating Object

    26/37

  • 7/27/2019 Creating Object

    27/37

    Scope of Variables

    The scope of instance and class variables is theentire 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 bedeclared before it can be used.

  • 7/27/2019 Creating Object

    28/37

    The Keyword this

    Use this to refer to the current object.

    Use this to invoke other constructors of the

    object.

  • 7/27/2019 Creating Object

    29/37

    Array of Objects

    Circle[] circleArray = new Circle[10];

    An array of objects is actually anarray of reference variables . Soinvoking circleArray[1].findArea()involves two levels of referencing asshown in the next figure. circleArrayreferences to the entire array.circleArray[1] references to a Circleob ect.

  • 7/27/2019 Creating Object

    30/37

    Array of Objects, cont.

    reference Circle object 0circleArray[0]

    circleArraycircleArray[1]

    circleArray[9] Circle object 9

    Circle object 1

    Circle[] circleArray = new Circle[10];

  • 7/27/2019 Creating Object

    31/37

  • 7/27/2019 Creating Object

    32/37

    Class Abstraction

    Class abstraction means to separate classimplementation from the use of the class. Thecreator of the class provides a description of theclass and let the user know how the class can beused. The user of the class does not need toknow how the class is implemented. The detailof implementation is encapsulated and hiddenfrom the user.

  • 7/27/2019 Creating Object

    33/37

    Example 6.8 The Mortgage Class

    Mortgage

    -annualInterestRate: double-numOfYears: int

    -loanAmount: double

    +Mortgage()+Mortgage(annualInterestRate: double,

    numOfYears: int, loanAmount: double)+getAnnualInterestRate(): double+getNumOfYears(): int

    +getLoanAmount(): double+setAnnualInterestRate(annualInteresteRate: double): void+setNumOfYears(numOfYears: int): void+setLoanAmount(loanAmount: double): void+monthlyPayment(): double+totalPayment(): double

    TestMortgageClass

    Run

    Mortgage

    http://localhost/var/www/apps/conversion/tmp/scratch_8/TestMortgageClass.htmhttp://localhost/var/www/apps/conversion/tmp/scratch_8/Mortgage.htmhttp://localhost/var/www/apps/conversion/tmp/scratch_8/Mortgage.htmhttp://localhost/var/www/apps/conversion/tmp/scratch_8/TestMortgageClass.htm
  • 7/27/2019 Creating Object

    34/37

    Example 6.9 The Count Class

    Run TestV oteCandidate

    http://localhost/var/www/apps/conversion/tmp/scratch_8/TestCountClass.htmhttp://localhost/var/www/apps/conversion/tmp/scratch_8/TestCountClass.htm
  • 7/27/2019 Creating Object

    35/37

    Java API and Core Java classes

    java.lang Contains core Java classes, such as numericclasses, 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.

  • 7/27/2019 Creating Object

    36/37

    java.io Contains classes for input and outputstreams and files.

    java.util Contains many utilities, such as date.

    java.net Contains classes for supporting

    network communications.

    Java API and Core Java classes,cont.

  • 7/27/2019 Creating Object

    37/37

    java.awt.image Contains classes for managing bitmap images.

    java.awt.peer Platform-specific GUI implementation.

    Others:

    java.sql java.rmi

    Java API and Core Java classes,cont .