Sun Educational Services
Module 2
Object-Oriented Programming
Java™ Programming Language
Sun Educational Services
Objectives
• Define modeling concepts: abstraction, encapsulation,
and packages
• Discuss why you can reuse Java technology application
code
• Define class, member, attribute, method, constructor, and
package
• Use the access modifiers private and public as
appropriate for the guidelines of encapsulation
• Invoke a method on a particular object
Java™ Programming Language Module 2, slide 2 of 30
Copyright 2002 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services, Revision E.2
Sun Educational Services
Objectives
• In a Java program, identify the following:
▼ The package statement
▼ The import statements
▼ Classes, methods, and attributes
▼ Constructors
• Use the Java technology application programming
interface (API) online documentation
Java™ Programming Language Module 2, slide 3 of 30
Copyright 2002 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services, Revision E.2
Sun Educational Services
Relevance
• What is your understanding of software analysis and
design?
• What is your understanding of design and code reuse?
• What features does the Java programming language
possess that make it an object-oriented language?
• Define the term object-oriented.
Java™ Programming Language Module 2, slide 4 of 30
Copyright 2002 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services, Revision E.2
Sun Educational Services
Software Engineering
Toolkits / Frameworks / Object APIs (1990s – up)
Java 2 SDK AWT / Swing Jini Java Beans JDBC
Object-Oriented Languages (1980s – up)
SELF Smalltalk Common Lisp Object System Effiel C++ Java
Libraries / Functional APIs (1960s – early 1980s)
NASTRAN TCP/IP ISAM X-Windows OpenLook
High-Level Languages (1950s –up) Operating Systems (1960s – up)
Fortran LISP C COBOL OS/360 UNIX MacOS MS-Windows
Machine Code (late 1940s – up)
Java™ Programming Language Module 2, slide 5 of 30
Copyright 2002 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services, Revision E.2
Sun Educational Services
The Analysis and Design Phase
• Analysis describes what the system needs to do:
▼ Modeling the real-world: actors and activities,
objects, and behaviors
• Design describes how the system does it:
▼ Modeling the relationships and interactions
between objects and actors in the system
▼ Finding useful abstractions to help simplify the
problem or solution
Java™ Programming Language Module 2, slide 6 of 30
Copyright 2002 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services, Revision E.2
Sun Educational Services
Abstraction
• Functions – Write an algorithm once to be used in many
situations
• Objects – Group a related set of attributes and
behaviors into a class
• Frameworks and APIs – Large groups of objects that
support a complex activity:
▼ Frameworks can be used “as is” or be modified to
extend the basic behavior
Java™ Programming Language Module 2, slide 7 of 30
Copyright 2002 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services, Revision E.2
Sun Educational Services
Classes as Blueprints for Objects
• In manufacturing, a blueprint describes a device from
which many physical devices are constructed
• In software, a class is a description of an object:
▼ A class describes the data that each object includes
▼ A class describes the behaviors that each object
exhibits
• In Java technology, classes support three key features
of object-oriented programming (OOP):
▼ Encapsulation
▼ Inheritance
▼ Polymorphism
Java™ Programming Language Module 2, slide 8 of 30
Copyright 2002 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services, Revision E.2
Sun Educational Services
Declaring Java Technology Classes
• Basic syntax of a Java class:
<modifiers> class <class_name> {
[<attribute_declarations>]
[<constructor_declarations>]
[<method_declarations>]
}
• Example:
public class Vehicle {
private double maxLoad;
public void setMaxLoad(double value) {
maxLoad = value;
}
}
Java™ Programming Language Module 2, slide 9 of 30
Copyright 2002 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services, Revision E.2
Sun Educational Services
Declaring Attributes
• Basic syntax of an attribute:
[<modifiers>] <type> <name> [ = <initial_value>];
• Examples:
public class Foo {
private int x;
private float y = 10000.0F;
private String name = "Bates Motel";
}
Java™ Programming Language Module 2, slide 10 of 30
Copyright 2002 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services, Revision E.2
Sun Educational Services
Declaring Methods
• Basic syntax of a method:
[<modifiers>] <return_type> <name>
([<argument_list>]) {
[<statements>]
}
• Examples:
public class Dog {
private int weight;
public int getWeight() {
return weight;
}
public void setWeight(int newWeight) {
weight = newWeight;
}
}
Java™ Programming Language Module 2, slide 11 of 30
Copyright 2002 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services, Revision E.2
Sun Educational Services
Accessing Object Members
• The “dot” notation: <object>.<member>
• This is used to access object members including attributes
and methods
• Examples:
d.setWeight(42);
d.weight = 42; // only permissible if weight is public
Java™ Programming Language Module 2, slide 12 of 30
Copyright 2002 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services, Revision E.2
Sun Educational Services
Information Hiding
The Problem:
MyDate
Client code has direct access to internal data
+day
(d refers to a MyDate object):
+month
+year d.day = 32;
// invalid day
d.month = 2; d.day = 30;
// plausible but wrong
d.day = d.day + 1;
// no check for wrap around
Java™ Programming Language Module 2, slide 13 of 30
Copyright 2002 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services, Revision E.2
Sun Educational Services
Information Hiding
The Solution:
MyDate Client code must use setters/getters to access
-day internal data:
-month
-year MyDate d = new MyDate();
+getDay()
+getMonth() d.setDay(32);
+getYear() // invalid day, returns false
+setDay(int)
+setMonth(int) d.setMonth(2);
+setYear(int) d.setDay(30);
// plausible but wrong, setDay returns false
d.setDay(d.getDay() + 1);
verify days in month // this will return false if wrap around
// needs to occur
Java™ Programming Language Module 2, slide 14 of 30
Copyright 2002 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services, Revision E.2
Sun Educational Services
Encapsulation
• Hides the implementation details of a class
• Forces the user to use an interface to access data
• Makes the code more maintainable
MyDate
-date
+getDay()
+getMonth()
+getYear()
+setDay(int)
+setMonth(int)
+setYear(int)
-validDay(int)
Java™ Programming Language Module 2, slide 15 of 30
Copyright 2002 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services, Revision E.2
Sun Educational Services
Declaring Constructors
• Basic syntax of a constructor:
[<modifier>] <class_name> ([<argument_list>]) {
[<statements>]
}
• Example:
1 public class Dog {
2 private int weight;
3
4 public Dog() {
5 weight = 42;
6 }
7
8 public int getWeight() {
9 return weight;
10 }
11 public void setWeight(int newWeight) {
12 weight = newWeight;
13 }
14}
Java™ Programming Language Module 2, slide 16 of 30
Copyright 2002 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services, Revision E.2
Sun Educational Services
The Default Constructor
• There is always at least one constructor in every class.
• If the writer does not supply any constructors, the
default constructor is present automatically:
▼ The default constructor takes no arguments
▼ The default constructor body is empty
• Enables you to create object instances with
new Xxx()without having to write a constructor.
Java™ Programming Language Module 2, slide 17 of 30
Copyright 2002 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services, Revision E.2
Sun Educational Services
Source File Layout
• Basic syntax of a Java source file:
[<package_declaration>]
[<import_declarations>]
<class_declaration>+
• Example, the VehicleCapacityReport.java file:
package shipping.reports;
import shipping.domain.*;
import java.util.List;
import java.io.*;
public class VehicleCapacityReport {
private List vehicles;
public void generateReport(Writer output) {...}
}
Java™ Programming Language Module 2, slide 18 of 30
Copyright 2002 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services, Revision E.2
Sun Educational Services
Software Packages
• Packages help manage large software systems.
• Packages can contain classes and sub-packages.
shipping
GUI domain
owns 0..*
Company Vehicle
reports Truck RiverBarge
Java™ Programming Language Module 2, slide 19 of 30
Copyright 2002 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services, Revision E.2
Sun Educational Services
The package Statement
• Basic syntax of the package statement:
package <top_pkg_name>[.<sub_pkg_name>]*;
• Examples:
package shipping.reports;
• Specify the package declaration at the beginning of the
source file.
• Only one package declaration per source file.
• If no package is declared, then the class “belongs” to
the default package.
• Package names must be hierarchical and separated by
dots.
Java™ Programming Language Module 2, slide 20 of 30
Copyright 2002 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services, Revision E.2
Sun Educational Services
The import Statement
• Basic syntax of the import statement:
import <pkg_name>[.<sub_pkg_name>].<class_name>;
OR
import <pkg_name>[.<sub_pkg_name>].*;
• Examples:
import shipping.domain.*;
import java.util.List;
import java.io.*;
• Precedes all class declarations
• Tells the compiler where to find classes to use
Java™ Programming Language Module 2, slide 21 of 30
Copyright 2002 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services, Revision E.2
Sun Educational Services
Directory Layout and Packages
• Packages are stored in the directory tree containing the
package name.
• Example, the “shipping” application packages:
shipping/
domain/
Company.class
Vehicle.class
RiverBarge.class
Truck.class
GUI/
reports/
VehicleCapacityReport.class
Java™ Programming Language Module 2, slide 22 of 30
Copyright 2002 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services, Revision E.2
Sun Educational Services
Development
JavaProjects/
BankPrj/
src/
banking/
domain/
GUI/
doc/ reports/
class/
banking/
domain/
GUI/
Compiler/ reports/
src/
doc/
class/
Compiling using -d
cd JavaProjects/BankPrj/src
javac -d ../class banking/domain/*.java
Java™ Programming Language Module 2, slide 23 of 30
Copyright 2002 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services, Revision E.2
Sun Educational Services
Terminology Recap
• Class – The source-code blueprint for a run-time object
• Object – An instance of a class
Also known as: instance
• Attribute – A data element of an object
Also known as: data member, instance variable, data
field
• Method – A behavioral element of an object
Also known as: algorithm, function, procedure
• Constructor – A “method-like” construct used to
initialize a new object
• Package – A grouping of classes and/or sub-packages
Java™ Programming Language Module 2, slide 24 of 30
Copyright 2002 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services, Revision E.2
Sun Educational Services
Using the Java API Documentation
• A set of Hypertext Markup Language (HTML) files
provides information about the API.
• A frame describes a package and contains hyperlinks to
information describing each class in that package.
• A class document includes the class hierarchy, a
description of the class, a list of member variables, a list
of constructors, and so on.
Java™ Programming Language Module 2, slide 25 of 30
Copyright 2002 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services, Revision E.2
Sun Educational Services
Example API
Documentation Page
Java™ Programming Language Module 2, slide 26 of 30
Copyright 2002 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services, Revision E.2
Sun Educational Services
Exercise: Using Objects and Classes
• Exercise objectives:
▼ Implement the concepts presented in this module
• Tasks:
▼ Complete the tasks specified by the instructor
Java™ Programming Language Module 2, slide 27 of 30
Copyright 2002 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services, Revision E.2
Sun Educational Services
Check Your Progress
• Define modeling concepts: abstraction, encapsulation,
and packages
• Discuss why you can reuse Java technology application
code
• Define class, member, attribute, method, constructor, and
package
• Use the access modifiers private and public as
appropriate for the guidelines of encapsulation
• Invoke a method on a particular object
Java™ Programming Language Module 2, slide 28 of 30
Copyright 2002 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services, Revision E.2
Sun Educational Services
Check Your Progress
• In a Java technology program, identify the following:
▼ The package statement
▼ The import statements
▼ Classes, methods, and attributes
▼ Constructors
• Use the Java technology API online documentation
Java™ Programming Language Module 2, slide 29 of 30
Copyright 2002 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services, Revision E.2
Sun Educational Services
Think Beyond
• What do you expect to achieve through analysis and
design?
• What domain objects and relationships appear in your
existing applications?
Java™ Programming Language Module 2, slide 30 of 30
Copyright 2002 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services, Revision E.2