// GradeBook.javapublic class GradeBook { // display a welcome message to the GradeBook user public void displayMessage { System.out.println "Welcome to the Grade Book!" ; } } //
Trang 1Classes and
Objects in Java
Object-oriented programming
Trang 2 Classes
Working with objects
Attributes, methods, and access control
Constructors
Readings:
Trang 3Java 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
Trang 4// 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
Trang 5 Objects are manipulated via references
Object references play the roles similar to pointers
Objects must be explicitly created by new operator
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
Trang 6Objects 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()
Trang 7Attributes, 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
Trang 8// 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 Gradebook 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
Trang 9Overloading 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”);
Trang 10 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
Trang 11// 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“ );
…
Trang 12Implementation 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
Trang 13Encapsulation / 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