classes and objects java

Upload: rasika-jayawardana

Post on 01-Mar-2018

267 views

Category:

Documents


3 download

TRANSCRIPT

  • 7/25/2019 Classes and Objects java

    1/35

    Chapter 6 Objects and Classes

    OO Programming Concepts

    Creating Objects and Object Reference Variables

    Differences between primitive data type and object type

    Atomatic garbage collection Constrctors

    !odifiers "public# privateand static$

    %nstance and Class Variables and !ethods &cope of Variables

    'se the this (eyword

    Case &tdies "Mortgageclass and Countclass$

  • 7/25/2019 Classes and Objects java

    2/35

    OO Programming Concepts

    data field )

    method n

    data field n

    method )

    An object

    ***

    ***

    &tate

    +ehavior

    Data ,ield

    radis - .

    !ethod

    findArea

    A Circle object

  • 7/25/2019 Classes and Objects java

    3/35

    Class and Objects

    circle)/ Circle

    radis - 0

    new Circle"$

    circlen/ Circle

    radis - .

    new Circle"$

    ***

    '!1 2raphical notation for classes

    '!1 2raphical notation

    for objects

    Circle

    radis/ doble

    findArea"$/ doble

    '!1 2raphical notation for fields

    '!1 2raphical notation for methods

  • 7/25/2019 Classes and Objects java

    4/35

    Class Declaration

    class Circle

    {

    double radius = 1.0;

    double findArea()

    {

    return radius*radius*3.11!";

    ##

  • 7/25/2019 Classes and Objects java

    5/35

    Declaring Object Reference Variables

    Class$a%e ob&ect$a%e;

    34ample/

    Circle %'Circle;

  • 7/25/2019 Classes and Objects java

    6/35

    Creating Objects

    ob&ect$a%e = ne Class$a%e();

    34ample/

    %'Circle = ne Circle();

    5he object reference is assigned to the object

    reference variable*

  • 7/25/2019 Classes and Objects java

    7/35

    DeclaringCreating Objects

    in a &ingle &tepClass$a%e ob&ect$a%e = ne

    Class$a%e();

    34ample/

    Circle %'Circle = ne Circle();

  • 7/25/2019 Classes and Objects java

    8/35

    Differences between variables of

    primitive Data types and object types

    )

    c/ Circle

    radis - )

    Primitive type int i - ) i

    Object type Circle c c reference

    Created sing

    new Circle"$

  • 7/25/2019 Classes and Objects java

    9/35

    Copying Variables of Primitive

    Data 5ypes and Object 5ypes

    )

    c)/ Circle

    radis - .

    Primitive type assignment

    i - j

    +efore/

    i

    0j

    0

    After/

    i

    0j

    Object type assignment

    c) - c0

    +efore/

    c)

    c0

    After/

    c)

    c0

    c0/ Circle

    radis - 7

  • 7/25/2019 Classes and Objects java

    10/35

    2arbage Collection

    As shown in the previous

    figure, after the assignment

    statement c1 = c2, c1 points to

    the same object referenced byc2. The object previously

    referenced by c1 is no longer

    useful. This object is known as

    garbage. Garbage is

    automatically collected by JVM.

  • 7/25/2019 Classes and Objects java

    11/35

    2arbage 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.

  • 7/25/2019 Classes and Objects java

    12/35

    Accessing Objects

    Referencing the object8s data/

    ob&ect$a%e.data

    %'Circle.radius

    %nvo9ing the object8s method/

    ob&ect$a%e.%etod

    %'Circle.findArea()

  • 7/25/2019 Classes and Objects java

    13/35

    Example 6.1 Using Objects

    Objective/ Demonstrate creating objects#

    accessing data# and sing methods*

    TestCircleTestCircle RunRun

    http://testcircle.htm/http://var/www/apps/conversion/tmp/scratch_4/TestCircle.bathttp://var/www/apps/conversion/tmp/scratch_4/TestCircle.bathttp://var/www/apps/conversion/tmp/scratch_4/TestCircle.bathttp://testcircle.htm/http://testcircle.htm/
  • 7/25/2019 Classes and Objects java

    14/35

    Constrctors

    Circle(double r){

    radius = r;

    #

    Circle()

    {

    radius = 1.0;

    #

    %'Circle = ne Circle(!.0);

    Constrctors are a

    special 9ind ofmethods that are

    invo9ed to constrct

    objects*

  • 7/25/2019 Classes and Objects java

    15/35

    Constrctors# cont*

    A constructor with no parametersis referred to as a default

    constructor.

    Constructors must have thesame 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

  • 7/25/2019 Classes and Objects java

    16/35

    34ample 6*0 'sing Constrctors

    Objective/ Demonstrate the role of

    constrctors and se them to create

    objects*

    TestCircleWithConstructorsTestCircleWithConstructors RunRun

    http://testcirclewithconstructors.htm/http://var/www/apps/conversion/tmp/scratch_4/TestCircleWithConstructors.bathttp://var/www/apps/conversion/tmp/scratch_4/TestCircleWithConstructors.bathttp://var/www/apps/conversion/tmp/scratch_4/TestCircleWithConstructors.bathttp://testcirclewithconstructors.htm/http://testcirclewithconstructors.htm/
  • 7/25/2019 Classes and Objects java

    17/35

    Visibility !odifiers and

    Accessor !ethods+y defalt# the class# variable# or data can beaccessed by any class in the same pac9age*

    public

    5he class# data# or method is visible to any class in anypac9age*

    private

    5he data or methods can be accessed only by the declaringclass*

    5he get and set methods are sed to read and modify private

    properties*

  • 7/25/2019 Classes and Objects java

    18/35

    34ample 6*:

    'sing the private!odifier

    and Accessor !ethods

    TestCircleWithAccessorsTestCircleWithAccessors RunRun

    %n this e4ample# private data are sed for the radisand the accessor methods getRadis and setRadis

    are provided for the clients to retrieve and modify

    the radis*

    http://testcirclewithaccessors.htm/http://var/www/apps/conversion/tmp/scratch_4/TestCircleWithAccessors.bathttp://var/www/apps/conversion/tmp/scratch_4/TestCircleWithAccessors.bathttp://var/www/apps/conversion/tmp/scratch_4/TestCircleWithAccessors.bathttp://testcirclewithaccessors.htm/http://testcirclewithaccessors.htm/http://testcirclewithaccessors.htm/http://testcirclewithaccessors.htm/
  • 7/25/2019 Classes and Objects java

    19/35

    Passing Objects to !ethods

    Passing by vale "the vale is the reference

    to the object$

    34ample 6*: Passing Objects as Argments

    TestPassingObjectTestPassingObject RunRun

    http://testpassingobject.htm/http://testpassingobject.htm/http://var/www/apps/conversion/tmp/scratch_4/TestPassingObject.bathttp://var/www/apps/conversion/tmp/scratch_4/TestPassingObject.bathttp://var/www/apps/conversion/tmp/scratch_4/TestPassingObject.bathttp://testpassingobject.htm/http://testpassingobject.htm/
  • 7/25/2019 Classes and Objects java

    20/35

    %nstance

    Variables# and !ethods

    %nstance variables belong to a specific instance*

    %nstance methods are invo9ed by an instance of

    the class*

  • 7/25/2019 Classes and Objects java

    21/35

    Class Variables# Constants#

    and !ethods

    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*

  • 7/25/2019 Classes and Objects java

    22/35

    Class Variables# Constants#

    and !ethods# cont*

    5o declare class variables# constants# and methods#

    se the static modifier*

  • 7/25/2019 Classes and Objects java

    23/35

    Class Variables# Constants#

    and !ethods# cont*

    Circle;ith&taticVariable

    mOfObjects"$/ int

    =findArea"$/ doble

    ) radiscircle)/Circle

  • 7/25/2019 Classes and Objects java

    24/35

    34ample 6*.

    'sing %nstance and Class Variables

    and !ethod

    Objective/ Demonstrate the roles of

    instance and class variables and theirses* 5his e4ample adds a class variablenmOfObjects to trac9 the nmber of

    Circle objects created*

    Test CircleWithtatic!ariableTestCircleWithtatic!ariable RunRun

    http://testcirclewithstaticvariable.htm/http://testcirclewithstaticvariable.htm/http://var/www/apps/conversion/tmp/scratch_4/TestCircleWithStaticVariable.bathttp://var/www/apps/conversion/tmp/scratch_4/TestCircleWithStaticVariable.bathttp://var/www/apps/conversion/tmp/scratch_4/TestCircleWithStaticVariable.bathttp://testcirclewithstaticvariable.htm/http://testcirclewithstaticvariable.htm/http://testcirclewithstaticvariable.htm/http://testcirclewithstaticvariable.htm/
  • 7/25/2019 Classes and Objects java

    25/35

    &cope of Variables

    5he scope of instance and class variables is the

    entire class* 5hey can be declared anywhere inside

    a class* 5he scope of a local variable starts from its

    declaration and contines to the end of the bloc9

    that contains the variable* A local variable mst bedeclared before it can be sed*

  • 7/25/2019 Classes and Objects java

    26/35

    5he (eyword this

    'se this to refer to the crrent object*

    'se this to invo9e other constrctors of the

    object*

  • 7/25/2019 Classes and Objects java

    27/35

    Array of Objects

    Circle[] circleArray = newCircle[10];

    An array of objects is actuallyan array of reference variables. So

    invoking circleArray[1].findArea()

    involves two levels of referencing

    as shown in the next figure.circleArray references to the

    entire array. circleArray[1]

    references to a Circle object.

  • 7/25/2019 Classes and Objects java

    28/35

    Array of Objects# cont*

    reference Circle object ?circleArray@?

    B

    circleArray

    circleArray@)

    circleArray@7 Circle object 7

    Circle object )

  • 7/25/2019 Classes and Objects java

    29/35

    Array of Objects# cont*

    34ample 6*6/ &mmariing the areas of thecircles

    Demonstrate the roles of instance and class

    variables and their ses* 5his e4ample addsa class variable nmOfObjects to trac9 the

    nmber of Circle objects created*

    TotalAreaTotalArea RunRun

    http://totalarea.htm/http://var/www/apps/conversion/tmp/scratch_4/TotalArea.bathttp://var/www/apps/conversion/tmp/scratch_4/TotalArea.bathttp://var/www/apps/conversion/tmp/scratch_4/TotalArea.bathttp://totalarea.htm/http://totalarea.htm/
  • 7/25/2019 Classes and Objects java

    30/35

    Class Abstraction

    Class abstraction means to separate class

    implementation from the se of the class* 5he

    creator of the class provides a description of theclass and let the ser 9now how the class can be

    sed* 5he ser of the class does not need to

    9now how the class is implemented* 5he detail

    of implementation is encapslated and hidden

    from the ser*

  • 7/25/2019 Classes and Objects java

    31/35

    34ample 6* 5he !ortgage Class

    !ortgage

    mOfEears"nmOfEears/ int$/ void

    =set1oanAmont"loanAmont/ doble$/ void

    =monthlyPayment"$/ doble

    =totalPayment"$/ doble

    Test"ortgageClassTest"ortgageClass

    RunRun

    "ortgage"ortgage

    http://testmortgageclass.htm/http://testmortgageclass.htm/http://var/www/apps/conversion/tmp/scratch_4/TestMortgageClass.bathttp://var/www/apps/conversion/tmp/scratch_4/TestMortgageClass.bathttp://mortgage.htm/http://mortgage.htm/http://var/www/apps/conversion/tmp/scratch_4/TestMortgageClass.bathttp://mortgage.htm/http://mortgage.htm/http://testmortgageclass.htm/http://testmortgageclass.htm/
  • 7/25/2019 Classes and Objects java

    32/35

    34ample 6*F 5he CountClass

    CountCount

    RunRun

    TestCountTestCount

    Cont

  • 7/25/2019 Classes and Objects java

    33/35

    Gava AP% and Core Gava classes

    &ava.lang

    Contains core Gava classes# sch as nmericclasses# strings# and objects* 5his pac9age is

    implicitly imported to every Gava program*

    &ava.at

    Contains classes for graphics*

    &ava.applet

    Contains classes for spporting applets*

  • 7/25/2019 Classes and Objects java

    34/35

    &ava.ioContains classes #or input an$ outputstreams an$ %les.

    &ava.utilContains man& utilities' such as $ate.

    &ava.netContains classes #or supportingnet(or) communications.

    Gava AP% and Core Gava classes#

    cont*

  • 7/25/2019 Classes and Objects java

    35/35

    &ava.at.i%age

    Contains classes for managing bitmap images*

    &ava.at.peer

    Platform