You are on page 1of 22

Module 2 Object-Oriented Programming

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

In Java, classes support three key features of OOP:


Encapsulation the mechanism that binds together code and data that manipulates and keep both safe from outside interface or misuse. Inheritance the process by w/c one object acquires the properties of another object Polymorphism a feature that allows one interface to be used as a general class of actions.

Declaring Java Classes


Basic syntax of a Java class:
<class_declaration> ::= <modifier> class <name> { <attribute_declaration>* <constructor_declaration>* <method_declaration>* }

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; } }

See programs TestThing.java/Test.txt

Accessibility Criteria
Modifier public protected default private Same Class Yes Yes Yes Yes Same Package Yes Yes Yes Subclass Universe Yes Yes Yes

Accessing Object Members


The dot notation: <object>. <member> This is used to access object members including attributes and methods Examples:
thing.setX(47) thing.x = 47; //only
permissible if x is public

Information Hiding
The Problem:
MyDate
+day : int +month : int +year : int

Client code has direct access to internal data:


MyDate d = new MyDate() d.day = 32; //invalid day d.month = 2; d.day = 30; //plausible but wrong d.day = d.day+1; //no check for wrap around

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

//this will return false if wrap around needs to occur

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; } }

See programs TestThing1.java/Thing1.java

What are constructors?


A constructor is a set of instructions designed to initialize an instance. The name of the constructor must be the same as the class. Constructors are not methods. Constructor do not return a value and are not inherited. You can have more than one constructor as long as they differ in parameter. Constructors can call method but not vice versa.

The Default Constructor


There is always at least one constructor in every class If the writer does not supply any constructors, the default constructor will be present automatically
The default constructor takes no arguments The default constructor has no body

Enables you to create object instances with new Xxx() without having to write a constructor See program Student.txt

Source File Layout


Basic syntax of a java source file:
<source_file> ::= [<package_declaration>] <import_declaration>* <class_declaration>+

Example, the VehicleCapacityReport.java file


package shipping.reports.Web; import shipping.domain.*; import java.util.List; import java.io.*; public class VehicleCapacityReport { private List vehicles; public void generateReport(Writer output) {} }

Software Package
Packages can contain classes and sub-packages

shipping
GUI reports
domain
Company Vehicle

Truck

RiverBarge

The package Statement


Basic syntax of the package statement
<package_declaration> ::= package <top_pkg_name> [.<sub_pkg_name>]*;

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.

The import Statement


Basic syntax of the package statement
<import_declaration> ::= import <pkg_name> [.<sub_pkg_name>]*.<class_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

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

Using Java API Documentation


A set of hypertext markup language (HTML) files provides information about the API One package contains hyperlinks to information on all of the classes A class document includes the class hierarchy, a description of the class, a list of member variables, a list of constructors, and so on (See examples at C:\jdk1.3\docs\api\index.html)

End of Module 2

You might also like