Title: Classes and Objects: A look inside
1Classes and ObjectsA look inside
2Classes 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.
3Class has data and methods
- Data holds the state, or content, of what the
object represents. - Methods indicate which operations can be
performed on an object, or which messages we can
send. - In the String class
- data holds the characters forming the string.
- Methods perform operations on the String
(converting to upper/lower case, measuring
length, ) - String class holds only one item of data.
4Class has data and methods(contd)
- In the GregorianCalendar class
- Data holds number of milliseconds since Jan 1
1970. - Data also indicates when change from Julian to
Gregorian occurred, and the time zone where this
calendar is defined. - Methods are used to change dates in the calendar,
extract information,
5Class has data and methods(contd)
- How data is organized and methods operate is
hidden from the outside. The user (program using
a class) needs only know how to create an object,
how to send messages and how to interpret return
values.
6How classes are used
public static void main()
GregorianCalendar object
cal new GregorianCalendar() . . .
Creates
time in millis
executes
cal.add(Calendar.day, 178) . . .
Void add(int field, int nb) . . .
Returns to
7UML Notation
ClassName
Data
List of variables with their types. Can
be primitive types or objects.
Constructor
Each constructor defined by list of arguments.
Methods
Each method defined by its name, list
of arguments and return type.
8Creating our first class
- We will pretend that there is no String class
defined in Java, and write our own version called
SimpleString with the following functionality. - An object can be initialized with an array of
characters. - It will convert to upper and lower case.
- It will let the user append characters at the
end. - It will let user delete a sequence of characters
starting at an arbitrary position. - It will return the content as an array of
characters.
9UML Notation for SimpleString
SimpleString
content char currentStringLength int
SimpleString() SimpleString(char str)
toUpperCase() SimpleString toLowerCase()
SimpleString append(char newStr)
void getContent() char delete(int index, int
length) void
10Class implementation
- A class is implemented Java using the same syntax
that we used so far. - All the main programs we wrote a classes with
only one method called main(). - Most classes will not have a main method.
11Structure of a class
public class SimpleString // Variables
public final int MAX_STRING_LENGTH 65243
public int currentStringLength // Bad idea Just
for example private char contents //
Constructors. There will be actual java code
between the s public SimpleString()
public SimpleString(char str) // Some of
the other methods. public SimpleString
toUpperCase() public void delete(int index,
int length) private int length() //
Again, just for example
12Structure of a class
- All variables or methods can be qualified as
either private or public. - A public variable or method can be accessed from
outside of the class, while a private variable or
method is only seen from the class itself. - The class SimpleString must be saved in a file
named SimpleString.java
13Structure of a class
- Order of the declarations is not important.
- The final keyword indicates that the value of the
variable will not change and can not be changed.
14Private vs Public
// Assume the following is in a file named
TestSimpleString.java public static void
main(String args) char someCharacters
new char25 // Assume the is some code to
assign values to someCharacters. SimpleString
str new SimpleString() str.append(someCharac
ters) int length str.currentStringLength
// OK. currentStringLength is public char
thirdChar str.content2 // Wrong. But would
be allowed if
// content was declared as
public. SimpleString upper
str.toUpperCase() // OK int length
str.length() // Cant do it. length() is public.
15Private vs Public
- Public variables and methods are meant to be
accessed and used from outside the class. - Private variables and methods are to be used by
the class itself. Other classes dont need to
know and shouldnt know about them. - The only public variables (that should be visible
from outside the class) are those declared as
final.
16Private vs Public
- We have already used public variables Math.PI,
Calendar.DAY_OF_YEAR. - A class should only allow its variables to be
read/modified from the outside through methods.
For instance, currentStringLength should be made
private and a public method named
currentStringLength() (or simply length()) be
created.
17Private vs Public
- A method to assign a value to a class variable is
usually called setXXX. For instance,
setTimeInMillis(long timeInMillis) in the
GregorianCalendar class. After using this method,
a call such as cal.get(Calendar.MONTH) will
return the month corresponding to the time
indicated by timeInMillis - There is no need for a method such as
setCurrentLength(int l) in the SimpleString
method because
18Private vs Public
- There is no need for a method such as
setCurrentLength(int l) in the SimpleString
method because - The idea of the class is to remove the burden of
keeping track of those details. - What would it mean anyway? What if the value of
currentStringLength is set to -12, or 5 greater
than the length of the actual string?
19Private vs Public
- In some way, variables are made private to
protect a user against himself. - By using methods to access variables, the code in
the class itself can be modified without
affecting the code using the classes.
20Implementing the classclass variables
- Class variables are declared outside any method,
but inside the public class SimpleString .
block. - They can be used by all the methods.
- Except for some special cases, they should all be
declared together, either at the beginning or the
end of the class. - The can be assigned an original value.
21Implementing the classclass variables code
example
public class SimpleString // Variables
public final int MAX_STRING_LENGTH 65536
private int currentStringLength private char
contents new char MAX_STRING_LENGTH . . .
In this example, when the new SimpleString()
statement is executed from another class and a
new SimpleString object is created, the array
contents is created to hold up to 65536
characters.
22Implementing the classconstructors
- Each constructors is defined with
- Public SimpleString(ltarg listgt)
23Implementing the classconstructors
public class SimpleString // Variables
public final int MAX_STRING_LENGTH 65536
private int currentStringLength private char
contents new char MAX_STRING_LENGTH //
Default constructor public SimpleString()
currentStringLength 0 // Could have
initialized at declaration. public
SimpleString (char str) for (int i 0
I lt str.length i) contentsi
stri currentStringLength
str.length
24Implementing the classmethods
- A constructor is a special method.
- The other methods are defined by their name,
argument and return type. - Two methods can have the same name, but there
must be some difference in the types of the
arguments. - A method may return a value of a predefined type.
The keyword return indicates which value is
returned.
25Implementing the classmethods
public class SimpleString // Variables and
constructors public final int MAX_STRING_LENGTH
65536 private int currentStringLength
private char contents new char
MAX_STRING_LENGTH // Since
currentStringLength is private, user can not
access directly public int length()
return currentStringLength public void
append(char charsToAdd) for (int i 0
I lt charsToAdd.length i)
contentsicurrentStringLength charsToAddi
currentStringLength
charsToAdd.length
26Next
- Private methods, static variables and methods