Classes and
Objects in Java
Object-oriented programming
Classes and objects in
Java
2
Đại học Công nghệ.
ĐHQG Hà Nội
Outline
Classes
Working with objects
Attributes, methods, and access control
Constructors
Readings:
Java how to program, chapter 3, 8
Classes and objects in
Java
3
Đại học Công nghệ.
ĐHQG Hà Nội
Java program
A Java program is a collection of objects
Each class is specified in one source file (file name is the same with class name)
Every line of code you write in Java must be
inside a class (not counting import directives)
Increase modularity
Easier to modified code, shorter compile time
3
Classes and objects in
Java
4
Đại học Công nghệ.
ĐHQG Hà Nội
// GradeBook.java
public class GradeBook
{
// display a welcome message to the GradeBook user
public void displayMessage()
{
System.out.println( "Welcome to the Grade Book!" );
}
} // end class GradeBook
// GradeBookTest.java
public class GradeBookTest
{
// main method begins program execution
public static void main( String args[] )
{
// create a GradeBook object and assign it to myGradeBook
GradeBook myGradeBook = new GradeBook();
// call myGradeBook's displayMessage method
myGradeBook.displayMessage();
}
} // end class GradeBookTest
Welcome to the Grade Book!
class declared as public must be
stored in a file with the same name
class declaration
begins / ends
main() is called automatically by
the Java Virtual Machine when
the program is executed
02 classes
in 02 files
creates an instant / object of the class
myGradeBook is the reference to it
Classes and objects in
Java
5
Đại học Công nghệ.
ĐHQG Hà Nội
5
Objects
Objects are manipulated via references
Object references play the roles similar to pointers
Objects must be explicitly created by new operator
5
public class GradeBookTest
{
// main method begins program execution
public static void main( String args[] )
{
// create a GradeBook object and assign it to myGradeBook
GradeBook myGradeBook = new GradeBook();
// call myGradeBook's displayMessage method
myGradeBook.displayMessage();
}
} // end class GradeBookTest
Classes and objects in
Java
6
Đại học Công nghệ.
ĐHQG Hà Nội
Objects and Object references
myGradeBook
GradeBook
Heap memory
// create a GradeBook object and assign it to myGradeBook
GradeBook myGradeBook = new GradeBook();
The object
reference
the object
created by
new GradeBook()
Classes and objects in
Java
7
Đại học Công nghệ.
ĐHQG Hà Nội
Attributes, methods,
and access control
Access modifiers:
Public
Accessible anywhere by anyone
Protected
Accessible only to the class itself and to its
subclasses or other classes in the same “package”
Private
Only accessible within this class
Classes and objects in
Java
8
Đại học Công nghệ.
ĐHQG Hà Nội
// GradeBook.java
public class GradeBook
{
private String courseName; // course name for this GradeBook
// method to set the course name
public void setCourseName( String name )
{
courseName = name; // store the course name
}
// method to retrieve the course name
public String getCourseName()
{
return courseName;
}
// display a welcome message to the GradeBook user
public void displayMessage()
{
System.out.println( "Welcome to the Grade Book!" );
}
} // end class GradeBook
attribute.
Each Gradeb ook object has its
own instant variable named
courseName
private:
accessed by
the class’s
methods only
access
modifiers
GradeBook
- courseName : String
+ setCourseName( name : String )
+ getCourseName() : String
+ displayMessage()
methods
Classes and objects in
Java
9
Đại học Công nghệ.
ĐHQG Hà Nội
Overloading methods
Methods can have the same name but different
argument lists.
class MyDate {
…
public boolean setMonth(int m) { …}
public boolean setMonth(String s) { …}
}
…
d.setMonth(9);
d.setMonth(”September”);
Classes and objects in
Java
10
Đại học Công nghệ.
ĐHQG Hà Nội
Constructors
Every class has a default “method” called a Constructor
Invoked when the object is to be “created” / “allocated” by using
“new”
Main purposes:
Initialise object’s attributes / data members
A class may have multiple constructors
Distinguished at compile time by having different arguments
The default constructor takes no arguments and is implicit when
no other constructors are specified
Classes and objects in
Java
11
Đại học Công nghệ.
ĐHQG Hà Nội
// GradeBook.java
public class GradeBook
{
private String courseName; // course name for this GradeBook
// constructor initializes courseName
public GradeBook( String name )
{
courseName = name; // initializes courseName 12
}
// method to set the course name
public void setCourseName( String name )
{
courseName = name; // store the course name
}
// method to retrieve the course name
public String getCourseName()
{
return courseName;
}
// display a welcome message to the GradeBook user
public void displayMessage()
{
System.out.println( "Welcome to the Grade Book!" );
}
} // end class GradeBook
GradeBook
- courseName : String
«constructor» GradeBook( name:
String )
+ setCourseName( name: String )
+ getCourseName() : String
+ displayMessage()
// GradeBookTest.java
…
// create a GradeBook object and assign it to myGradeBook
GradeBook myGradeBook =
new GradeBook( "CS101 Introduction to Java Programming“ );
…
Classes and objects in
Java
12
Đại học Công nghệ.
ĐHQG Hà Nội
Implementation vs. Interface
GradeBookTest: a “client” of GradeBook
Implementation
Data structures and code that implement the features (variables
and methods)
Usually more involved and may have complex inner workings
Clients don’t need to know
Interface
The controls exposed to the “client” by the implementation
The knobs on the block box
Classes and objects in
Java
13
Đại học Công nghệ.
ĐHQG Hà Nội
Encapsulation / information hiding
“Don’t expose internal data structures!”
Objects hold data and code
Neither is exposed to the end user
Objects expose an interface
Anthropomorphic nature of objects
Think of objects and people who have specialized roles!
Lawyer, Mechanic, Doctor
Complexity is hidden inside the object
Make life easier for clients
More modular approach
Implementation changes in one component doesn’t affect others
Less error-prone