Professional Documents
Culture Documents
Object-Oriented Programming
Objectives: Upon completion of this module, you should be able to: Define modeling concepts : abstraction, encapsulation, and packages Discuss why Java technology application code is reusable Define class, member, attribute, method, constructor, and package Use the access modifiers private and public as appropriate for the guidelines of encapsulation
Object-Oriented Programming
Objectives Invoke a method on a particular object In a Java technology program, identify the following:
The package statement The import statement Classes, methods, and attributes Constructors
Use the Java technology application programming interface (API) online documentation
Classes
A class is a description of an object:
A class describes the data that each object includes A class describes the behaviors that each objects exhibits
Example
public class Vehicle { private double maxLoad; public void setMaxLoad(double value) { maxLoad = value; } }
Declaring Attributes
Basic syntax of an attribute:
<attribute_declaration ::= <modifier> <type> <name> [= <default_value>];
<type> ::= byte | short | int | long | char | float | double | boolean | <class>
Examples:
public class Foo { public int x; private float y = 10000.0F; private String name = Fred Flintstone; }
Declaring Methods
Basic Syntax of a method:
<method_declaration> ::= <modifier> <return_type> <name> (<parameter>*) { <statement>* } <parameter> ::= <parameter_type> <parameter_name>
Examples:
public class Thing { private int x; public int getX() { return x; } public void setX(int new_x) { x=new_x; } }
Accessibility Criteria
Modifier public protected default private Same Class Yes Yes Yes Yes Same Package Yes Yes Yes Subclass Universe Yes Yes Yes
Information Hiding
The Problem:
MyDate
+day : int +month : int +year : int
Information Hiding
The Solution:
MyDate
-day : int -month : int
Client code must use setters/getters to access internal data: MyDate d = new MyDate() d.setDay(32);//invalid day, return false d.setMonth(2); d.setDay(30); //plausible but wrong, setDay returns false d.setDay(d.getDay() + 1);
-year : int
+getDay() : int +getMonth() : int +getYeat() : int +setDay(d: int) : boolean
+setMonth(m : int)
+setYear(Y: int) -validDay(d: int) : boolean
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 : long
+getDay() : int +getMonth() : int
+getYear() : int
+setDay(d : int) +setMonth(m : int) +setYear(y: int) -validDay(d:int) : boolean
Declaring Constructors
Basic syntax of a constructor:
<constructor_declaration> ::= <modifier> <class_name> (<parameter>*) { <statement>* }
Examples:
public class Thing { private int x; public Thing() { x=47; } public Thing (int new_x) { x = new_x; } }
Enables you to create object instances with new Xxx() without having to write a constructor See program Student.txt
Software Package
Packages can contain classes and sub-packages
shipping
GUI reports
domain
Company Vehicle
Truck
RiverBarge
Example:
package shipping.reports.web;
Specify the package declaration at the beginning of the source file Only one package declaration per source If no package is declared, then the class belongs to the default package Package names must be hierarchical and separated by dots Package name should be written in lowercase.
Examples:
import shipping.domain.*; Import java.util.List; import java.io.*;
Precedes all class declarations Tells the compiler where to find classes to use
End of Module 2