lecture 9 objects and classes 3

Upload: maine-basan

Post on 04-Apr-2018

222 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/30/2019 Lecture 9 Objects and Classes 3

    1/27

    CMSC22OOP

    ObjectsandClasses3

    HappyNewYearJ

  • 7/30/2019 Lecture 9 Objects and Classes 3

    2/27

    Objec:ves

    ReviewofObjectsandClasses ClassMembers

    Instancevariables/methodsClassvariables/methods

    TypesofClasses ClassesonotherOOPLs.

  • 7/30/2019 Lecture 9 Objects and Classes 3

    3/27

    ReviewClasses

    Classesdefinetheproper:esandbehaviors(methods)objectsthatbelongtoithave.

    AClassisastructurecomposedofAributes(instanceandclassvariablesMethods/Opera:onthatmanipulatethedata(instanceandclassmethods)

  • 7/30/2019 Lecture 9 Objects and Classes 3

    4/27

    publicclassCar{

    Stringcolor;

    Stringplatenumber;intspeed;

    //constructorwithparametersforini2aliza2on

    publicCar(Stringnplatenumber,Stringncolor){

    platenumber=nplatenumber; color=ncolor;

    }

    publicvoidstop()

    { speed=0;

    }

    }

  • 7/30/2019 Lecture 9 Objects and Classes 3

    5/27

    publicclassCarExample{

    publicsta:cvoidmain(Stringargs[]){

    //passtheparameterstotheconstructor

    Carcar1=newCar(XYZ123,RED);

    Carcar2=newCar(ABC456,GREEN);

    }

    }

    UsingConstructorswith

    Parameters

  • 7/30/2019 Lecture 9 Objects and Classes 3

    6/27

    Car

    Stringplatenumber;

    Stringcolor;

    intspeed;

    stop()

    accelerate(int)

    repaint(String)

    Car.javaProgram

    car1

    platenumber:XYZ13

    color:Red

    speed:0

    stop()

    accelerate(int)

    repaint(String)

    car1

    platenumber:ABC456

    color:Green

    speed:0

    stop()

    accelerate(int)

    repaint(String)

  • 7/30/2019 Lecture 9 Objects and Classes 3

    7/27

    Addi:onalTerms

    Instancearibute/variableAributespecifictoaninstance/object.Eachobjectoftheclasshavethesamesetofproper:esBUTmayhavedifferentvaluesforeachproperty.

    Instancemethod/opera:onMethodwheretheresultofwhichisdependentonthecurrentstateoftheobjectinques:on.

  • 7/30/2019 Lecture 9 Objects and Classes 3

    8/27

    DeclaringanInstanceVariable/

    InstanceMethod

    publicclassCar{

    Stringcolor;

    Stringplatenumber;

    intspeed;

    publicvoidstop(){

    speed=0;

    }

    }

    //InstanceVariables

    //InstanceMethods

  • 7/30/2019 Lecture 9 Objects and Classes 3

    9/27

    Car

    Stringplatenumber;Stringcolor;

    intspeed;

    stop()

    Car.javaProgram

    car1

    platenumber:XYZ13

    color:Red

    speed:0

    stop()

    car1

    platenumber:ABC456

    color:Green

    speed:0

    stop()

  • 7/30/2019 Lecture 9 Objects and Classes 3

    10/27

    Accessinginstanceaributesand

    methodsofanobject(JavaStyle)

    InstanceAributes.;Ex.car1.color;

    InstanceMethodInvoca:on.();Ex.car1.stop();

  • 7/30/2019 Lecture 9 Objects and Classes 3

    11/27

    Addi:onalTerms

    Classaribute/variableAributethatiscommonto/reflectedinallobjectsintheclass.

    I.e.Global(butnotexactly). Classmethod/opera:on

    MethodthatiscommontoallobjectsoftheclassandthattheresultofwhichisNOT:edtoany

    par:cularinstanceoftheclass.

  • 7/30/2019 Lecture 9 Objects and Classes 3

    12/27

    DeclaringanInstanceVariable/

    InstanceMethod

    publicclassCar{

    Stringcolor;

    Stringplatenumber;

    intspeed;

    sta2cintnumberOfCars;

    publicsta2cintgetNumberOfCars(){

    returnnumberOfCars;

    }

    }

    //ClassVariable

    //ClassMethod

  • 7/30/2019 Lecture 9 Objects and Classes 3

    13/27

    Car

    Stringplatenumber;Stringcolor;

    intspeed;

    IntnumberOfCars;

    stop()

    getNumberOfCars()

    Car.javaProgram

    car1

    platenumber:XYZ13

    color:Red

    speed:0

    stop()

    car1

    platenumber:ABC456

    color:Green

    speed:0

    stop()

  • 7/30/2019 Lecture 9 Objects and Classes 3

    14/27

    Accessingclassaributesand

    methodsofanobject(JavaStyle)

    ClassAributes.;Ex.Car.numberOfCars;

    ClassMethodInvoca:on.();Ex.Car.getNumberOfCars();

  • 7/30/2019 Lecture 9 Objects and Classes 3

    15/27

    Also

    Instancemethodscanaccessinstancevariablesandinstancemethodsdirectly.

    Instancemethodscanaccessclassvariablesandclassmethodsdirectly.

    Classmethodscanaccessclassvariablesandclassmethodsdirectly.

    Classmethods

    cannot

    accessinstancevariablesorinstancemethodsdirectly

  • 7/30/2019 Lecture 9 Objects and Classes 3

    16/27

    NoteonDestructors

    DestructorsSpecializedmethodsthatdeallocatememory.Maybeimplicitlyorexplicitlycalled(i.e.inC++)Javadoesnthavedestructorsbecauseofitsautogarbagecollec:onfeatures.

  • 7/30/2019 Lecture 9 Objects and Classes 3

    17/27

    Note

    ReusabilityUsefulnessoftheclass/objectsinmorethanoneobject.

  • 7/30/2019 Lecture 9 Objects and Classes 3

    18/27

    TypesofClasses

    En2tyClasses/DataClassesClasseswhoseobjectsencapsulatethedatausedinasystem

    Data-relatedobjects.E.g.String,Student,Car

  • 7/30/2019 Lecture 9 Objects and Classes 3

    19/27

    TypesofClasses

    U2lityClassesClassesthatmaintainu:litymethodsandusuallyarenotinstan:atable

    java.lang.Math

  • 7/30/2019 Lecture 9 Objects and Classes 3

    20/27

    TypesofClasses

    SingletonObjects{Class}Objectwheninstan:ated,nootherobjectofthesameclassexistsinthegivencontext.

    E.g.java.lang.System

  • 7/30/2019 Lecture 9 Objects and Classes 3

    21/27

    TypesofClasses

    ControlClasses,View(Interface)ClassesControlClassesforLogicViewClassesfortheUserInterface

  • 7/30/2019 Lecture 9 Objects and Classes 3

    22/27

    CLASSESONOTHERPROGRAMMINGLANGUAGES

  • 7/30/2019 Lecture 9 Objects and Classes 3

    23/27

  • 7/30/2019 Lecture 9 Objects and Classes 3

    24/27

  • 7/30/2019 Lecture 9 Objects and Classes 3

    25/27

  • 7/30/2019 Lecture 9 Objects and Classes 3

    26/27

    CreateaClass

    Fourinstancevariables Twoclassvariables Threeinstancemethods Oneclassmethod

  • 7/30/2019 Lecture 9 Objects and Classes 3

    27/27

    SummarySomeques:ons?

    Whatisaclass? Whatisanobject? Whatisabstrac:on? Whatarethemembersofaclass? Whatisthedifferencebetweenaninstancemethod/variableandaclassmethod/variable?

    Whatareconstructors/destructors?