Classes and Objects - PowerPoint PPT Presentation

1 / 27
About This Presentation
Title:

Classes and Objects

Description:

Calendar cal = new Integer(20); Sending messages to objects ... In pictures: objects (cont'd) c2 = c1; 0x1112. c1: 0x1112. c2: No new object is created. ... – PowerPoint PPT presentation

Number of Views:16
Avg rating:3.0/5.0
Slides: 28
Provided by: csGen
Category:
Tags: classes | objects

less

Transcript and Presenter's Notes

Title: Classes and Objects


1
Classes and Objects
  • CSci 119
  • 8 Sept. 2003

2
Classes and Objects
  • Objects
  • Things that must be created during the program
    execution.
  • Contain data, which other object can manipulate
    and query by sending messages.
  • Classes
  • Definition from which we create objects.

3
Creating (Instantiating) Objects
  • An object is created using a constructor defined
    by the class.
  • Typical object creation statement
  • ltclass name1gt indicates the type of ltvar namegt.
  • new is a keyword that indicates that an object of
    type ltclass name2gt is to be created.

ltclass name1gt ltvar namegt new ltclass name2gt
(ltargsgt)
4
Creating (Instantiating) Objects (contd)
  • ltclass name1gt and ltclass name2gt are usually the
    same.
  • There can be any number of arguments, including
    0.

5
Object creation examples in Java
// With no arguments. Defaults to
today. GregorianCalendar cal new
GregorianCalendar() // With arguments to set to
a specific value. // Corresponds to 25 Oct 1929,
not Sept. Need to read docs. GregorianCalendar
cal new GregorianCalendar(1929, 9, 25) // To
eventually transform String to ints. Integer
intObject new Integer(224)
6
Object creation examples in Java
// Can separate variable declaration from object
creation GregorianCalendar cal // Right now
cal null cal new GregorianCalendar()
null is a java keyword to indicate that no value
is assigned to a variable. A variable is not of
much use until it has been assigned a value.
7
Exceptions to typical object creation.
  • The String class is a special case that does not
    require the use of the new keyword.

String str hello
8
Exceptions to typical object creation.
  • Through inheritance, the variable does not need
    to be the same type as the object created.

// GregorianCalendar is a subclass of
Calendar Calendar cal new GregorianCalendar() S
ystem.out.println(Month is
cal.get(Calendar.month)) // The following will
not work Calendar cal new Integer(20)
9
Sending messages to objects
  • Objects communicate with each other by sending
    messages.
  • They send messages by calling methods defined by
    the class from which the object is created.
  • Messages can be used for modifying the data in
    the object or querying about the content of the
    object.

10
Methods
  • A method declaration is of the form
  • Where
  • ltreturn-typegt can be a primitive type, a class
    name or void.
  • ltmethod namegt is a valid identifier.
  • ltargsgt is a list of comma-separated objects,
    constants or variable. The list can be empty.
  • For now all methods will be public. We will look
    at the role of static later

public ltstaticgtltreturn typegt ltmethod
namegt(ltargsgt)
11
Examples of methods
// A StringBuffer is a more powerful
String StringBuffer strBuffer new
StringBuffer(This is ) // Append does not
have a return value but the content of // the
strBuffer object will be modified. //
declaration public void append(String) strBuffer
.append(a long String) // toString does not
modify the content of strBuffer, but returns // a
String object representing the content of
strBuffer // declaration public String
toString() System.out.println(Value of
strBuffer strBuffer.toString())
12
Static methods (class methods)
  • Static methods are methods that send messages to
    the class itself, not the objects.
  • They are rarely used to modify data, but rather
    to get general information from a class.
  • Some argue that static methods are not proper for
    object-oriented programming. But they do prevent
    a lot of headaches.

13
Static method use
// From the Math class. All the methods are
static. // method declaration public static
double cos(double) float cosValue
Math.cos(0.23) // A class that creates objects
can also have static methods. // method
declaration public static int parseInt(String) in
t intVal Integer.parseInt(224)
14
static class variables
  • Most classes and objects have variables that you
    can not see from the outside. For instance
  • The argument 544 has to be stored somewhere in
    an Integer object. Is it stored as an int? a
    String? Something else?

Integer intObject new Integer(544)
15
static class variables (contd)
  • In general, we get the information about content
    of the object or class by sending methods. For
    instance
  • Many classes define static variable, which are
    usually constant, and can be accessed without
    using a method.

Integer intObject new Integer(23) int intVal
intObject.intValue()
16
static class variables (contd)
  • Static class variable are usually defined as
  • where the final indicates that the value can not
    be changed.
  • The convention is to capitalize all the letters
    in var name.

public static final lttypegt ltvar namegt ltvaluegt
17
Examples of static variable use
double pi Math.PI GregorianCalendar cal new
GregorianCalendar int monthIndex
Calendar.MONTH int monthValue
cal.get(monthIndex) // now monthValue contains
a number between 0 and 11. // It would be 8 if
ran in September. cal.add(Calendar.DATE,
1000) // The date now stored in cal in 1000 days
from today. Cal.add(Calendar.YEAR, 5) // We
added another 5 years to the date
18
Static class variables
  • In the previous example
  • Math.PI referred to a well known constant.
  • Calendar.MONTH, Calendar.DATE and Calendar.YEAR
    are determined by the way the Calendar class
    stores the data internally. It is easier and less
    error prone for someone to write
    cal.get(Calendar.MONTH) than to write cal.get(2)

19
Moving objects around
  • Consider this

int i 10 int j i // Now both i and j hold a
value of 10. i 20 // now i 20, j 10. But
StringBuffer str1 new StringBuffer(This is
) Integer str2 str1 // both str1.toString()
and str2.toString would return This is
. str2.append(a long String) // both
str1.toString() and str2.toString would return //
This is a long String
20
Whats the difference?
  • int is a primitive type, while StringBuffer is a
    class.
  • A variable of a primitive type is assigned a
    value, while a variable of a class type is
    assigned a handle to an object which exists
    independently of the variable.

21
In pictures primitive types
int i, j
j 30
30 is written is space allocated for j. There is
no connection between i and j.
Space is allocated for i and j. Values undefined
20
i
undef
i
4)
1)
30
j
undef
j
i 20
20 is written is space allocated for i
20
i
2)
undef
j
j i
20 is written is space allocated for j
20
i
3)
20
j
22
In pictures objects
GregorainCalendar c1, c2
null
c1
Space is allocated for c1 and c2. Values set to
null
1)
null
c2
c1 new GregorianCalendar()
GC object
An object is created for the GregorianCalendar.
But the value of c1 is really the address
(reference) where the object is located.
0x1112
c1
2)
month8 year2003
null
c2
23
In pictures objects (contd)
c2 c1
GC object
0x1112
c1
No new object is created. Both c1 and c2 point to
the same object.
month8 year2003
0x1112
c2
c2.add(Calendar.MONTH, 2)
GC object
Content in the object is modified. We the
modification whether we access from c1 or c2.
0x1112
c1
month10 year2003
0x1112
c2
24
What if we want a copy of the class
  • Most class have a clone() method define. So after
  • c1 and c2 will refer to two different objects.
    Changing the content of one object wont affect
    the content of the other.

GregorianCalendar c1 new GregorianCalendar() Gr
egorianCalendar c2 c1.clone()
25
Java language loose ends keywords
  • Keywords are words reserved by the language for
    writing code. For instance,
  • public, static, void, int, float, double,
    boolean, for, while, if, else, class
  • are all keywords. There are many more.
  • Keywords CAN NOT be used as identifiers. You can
    not use public as a variable name.

26
Java language loose ends casting
  • A variable of a given type can be cast to a
    variable of a different to the cast operator ().
  • Not all casts are valid. What doesnt
    instinctively make sense is probably wrong.
  • Some casts are implicit. There is no need to
    specify.

27
Cast examples
int i 23 float f i // OK. Dont need to
specify int -gt int i f // Wont compile. Need
to be explicit about cast i (int) f //
OK boolean b (boolean)f // Wont compile //
Can cast objects as well. More on this when we
look // at inheritance.
Write a Comment
User Comments (0)
About PowerShow.com