Objects  and  Classes  3  

Happy  New  Year  J  

CMSC22  OOP  

Objec:ves  
•  Review  of  Objects  and  Classes   •  Class  Members  
–  Instance  variables/methods   –  Class  variables/methods  

•  Types  of  Classes   •  Classes  on  other  OOPLs.  

Review:  Classes   •  Classes  define  the  proper:es  and  behaviors   (methods)  objects  that  belong  to  it  have.   •  A  Class  is  a  structure  composed  of   –  ANributes  (instance  and  class  variables   –  Methods/Opera:on  that  manipulate  the  data   (instance  and  class  methods)   .

   //constructor  with  parameters  for  ini2aliza2on    public  Car(String  nplatenumber.    }     }   .   public  class  Car{    String  color.    int  speed.      color  =  ncolor.            String  platenumber.    }          public  void  stop()    {      speed  =  0.  String  ncolor){      platenumber  =  nplatenumber.

     }     }   Using  Constructors  with   Parameters   .”RED”).      Car  car2  =  new  Car(”ABC456”.”GREEN”).public  class  CarExample{      public  sta:c  void  main(String  args[]){        //pass  the  parameters  to  the  constructor      Car  car1  =  new  Car(”XYZ123”.

  int  speed.   stop()   accelerate(int)   repaint(String)   Program     car1   platenumber:  “XYZ123”     color:  “Red”   speed:  0   stop()   accelerate(int)   repaint(String)   car1   platenumber:  “ABC456”     color:  “Green”   speed:  0   stop()   accelerate(int)   repaint(String)   .  java   Car   String  platenumber.Car.   String  color.

  –  Each  object  of  the  class  have  the  same  set  of   proper:es  BUT  may  have  different  values  for  each   property.Addi:onal  Terms:   •  Instance  aNribute  /variable   –  ANribute  specific  to  an  instance/object.     .   •  Instance  method/opera:on   –  Method  where  the  result  of  which  is  dependent   on  the  current  state  of  the  object  in  ques:on.

     int  speed.Declaring  an  Instance  Variable/ Instance  Method   public  class  Car{    String  color.    public  void  stop(){   //Instance  Methods      speed  =  0.    }     }     .   //Instance  Variables          String  platenumber.

Car.   int  speed.   String  color.  java   Car   String  platenumber.   Program     stop()   car1   platenumber:  “XYZ123”     color:  “Red”   speed:  0   stop()   car1   platenumber:  “ABC456”     color:  “Green”   speed:  0   stop()   .

 <property>.color.    car1.   •  Instance  Method  Invoca:on   –  <object  variable>  .  car1.Accessing  instance  aNributes  and   methods  of  an  object  (Java  Style)   •  Instance  ANributes   –  <object  variable>  .  <methodname>(<param>).  stop().   .   –  Ex.   –  Ex.

e.Addi:onal  Terms   •  Class  aNribute/variable   –  ANribute  that  is  common  to/reflected  in  all   objects  in  the  class.   •  Class  method/opera:on   –  Method  that  is  common  to  all  objects  of  the  class   and  that  the  result  of  which  is  NOT  :ed  to  any   par:cular  instance  of  the  class.   –  I.   .  Global  (but  not  exactly).

   sta2c  int  numberOfCars.      int  speed.Declaring  an  Instance  Variable/ Instance  Method   public  class  Car{    String  color.   //Class  Variable    public  sta2c  int  getNumberOfCars(){      return  numberOfCars.          String  platenumber.   //ClassMethod    }     }   .

  String  color.   stop()   getNumberOfCars()   Program     car1   platenumber:  “XYZ123”     color:  “Red”   speed:  0   stop()   car1   platenumber:  “ABC456”     color:  “Green”   speed:  0   stop()   .   int  speed.Car.  java   Car   String  platenumber.   Int  numberOfCars.

  –  Ex.Accessing  class  aNributes  and   methods  of  an  object  (Java  Style)   •  Class  ANributes   –  <classname>  .   –  Ex.  getNumberOfCars().  Car.   •  Class  Method  Invoca:on   –  <classname>  .  <methodname>(<param>).   .numberOfCars.    Car.  <property>.

Also     •  Instance  methods  can  access  instance   variables  and  instance  methods  directly.   •  Class  methods  can  access  class  variables  and   class  methods  directly.   •  Instance  methods  can  access  class  variables   and  class  methods  directly.   •  Class  methods  cannot  access  instance   variables  or  instance  methods  directly   .

Note  on  Destructors   •  Destructors   –  Specialized  methods  that  deallocate  memory.   .  in  C++)   –  Java  doesn’t  have  destructors  because  of  its  auto   garbage  collec:on  features.   –  May  be  implicitly  or  explicitly  called  (i.e.

  .Note   •  Reusability   –  Usefulness  of  the  class/objects  in  more  than  one   object.

    –  E.Types  of  Classes   •  En2tyClasses  /  DataClasses     –  Classes  whose  objects  encapsulate  the  data  used   in  a  system   –  Data-­‐related  objects.  String.g.  Car     .  Student.

lang.Types  of  Classes   •  U2lityClasses   –  Classes  that  maintain  u:lity  methods  and  usually   are  not  instan:atable   –  java.Math     .

 no  other  object  of  the   same  class  exists  in  the  given  context.  java.lang.   –  E.g.Types  of  Classes   •  Singleton  Objects  {Class}   –  Object  when  instan:ated.System   .

View(Interface)Classes     –  Control  Classes  –  for  Logic   –  View  Classes  –  for  the  User  Interface   .Types  of  Classes   •  ControlClasses.

CLASSES  ON  OTHER   PROGRAMMING  LANGUAGES   .

.

.

.

Create  a  Class   •  •  •  •  Four  instance  variables   Two  class  variables   Three  instance  methods   One  class  method   .

Summary:  Some  ques:ons?   What  is  a  class?   What  is  an  object?   What  is  abstrac:on?   What  are  the  members  of  a  class?   What  is  the  difference  between  an  instance   method/variable  and  a  class  method/variable?   •  What  are  constructors/destructors?   •  •  •  •  •  .

Sign up to vote on this title
UsefulNot useful

Master Your Semester with Scribd & The New York Times

Special offer for students: Only $4.99/month.

Master Your Semester with a Special Offer from Scribd & The New York Times

Cancel anytime.