1. Trang chủ
  2. » Công Nghệ Thông Tin

Web Programming with Java Java - Object-Oriented Programming doc

52 316 0
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Tiêu đề Java - Object-Oriented Programming
Tác giả Huynh Huu Viet
Trường học University of Information Technology
Chuyên ngành Web Programming with Java
Thể loại Lecture Notes
Năm xuất bản 2008
Thành phố Hanoi
Định dạng
Số trang 52
Dung lượng 486,43 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Objects and Classes™ Basic OO concepts ƒ Object, class, state, behavior, message ™ Creating Java classes, objects, properties and methods ™ Constructors ™ Package ™ Reference data type

Trang 1

Web Programming with Java

Java Object-Oriented Programming

Huynh Huu Viet Email: viethh@uit.edu.vn

Department of Information Systems

Trang 2

ƒ Inheritance & Polymorphism

™ Some useful Java classes

™ Exception Handling, I/O

™ Readings & Exercise

™ Discussion

Trang 3

Objects and Classes

™ Basic OO concepts

ƒ Object, class, state, behavior, message

™ Creating Java classes, objects,

properties and methods

™ Constructors

™ Package

™ Reference data type

™ Variable scope

Trang 4

Basic OO concepts : Object

™ Object: any thing, which can be described by

ƒ State: property, or attribute

ƒ Behavior: function/action, or method: ask the object to perform a task

™ Example

ƒ A student, a desk, a circle, a button, and even a loan

Trang 5

Basic OO concepts : Class

™ Class: a set of objects with common attributes and behaviors

ƒ An object is an instance of a class

™ Class is a template or blueprint that defines what an object's data and methods will be

Trang 6

Basic OO concepts : Message

™ Objects interact (communicate) by

passing messages

Trang 7

• Defining classes and their properties

• Manipulating classes/objects properties and behaviors

• Handling objects interactions

™ At execution time

ƒ Programs are executed through predefined

manipulations and interactions

Trang 8

ƒ A constructor without any parameter

ƒ If a programmer doesn’t define any constructor for a class, JRE will implicitly create a default

constructor

Trang 9

Using Constructor

behaviors of an object when it is initially created

™ Three differences with methods:

ƒ Must have the same name as the class itself.

ƒ Do not have a return type - not even void.

object is created

ClassName() //no “void” or any other data

type {

… }

Trang 10

Constructor Overloading

™ Like methods, constructors can be

overloaded

™ This offers greater flexibility and

convenience of creating objects

Trang 11

Using Objects (1)

™ Object initialization

™ Using Member Variables

ƒ Member variable declaration

• Any where in a class, outside all methods

ƒ Used within the class

• Can be used/referenced anywhere directly

ƒ Used with objects

• Using the "." operator and preceded with object name

• Ex: myCircle.radius

ClassName objectName;

objectName = new ClassName();

Trang 12

Using Objects: using methods

™ Defining methods

ƒ With a return value (type)

ƒ Without a return value (type) – void

™ Calling Methods

directly

ƒ Used with objects or outside the class

• Using the "." operator and preceded with object name

• Examples:

Trang 13

The “main” Method

™ Following OOP guidelines, the use of the

“main” method should be deemphasized

ƒ The “main” method is merely a starting point of the application

ƒ There should not be too many statements in the main method

ƒ Using objects and methods effectively

™ Typically (and ideally), you only do these

things in the main method

ƒ Creating objects

ƒ Calling class/object methods

Trang 14

™ Packages are used to group classes

™ Four reasons for using packages

ƒ To locate classes

ƒ To avoid naming conflicts

ƒ To distribute software conveniently

Trang 16

Using Package

™ Organizing your classes into packages

ƒ No duplicate class definition in the same

Trang 17

referenced outside the default package

Trang 18

Reference Data Type

™ Reference data type stores memory

address (reference) as its value

™ Objects are reference data type

Trang 20

Variable Scope

™ Member variable

ƒ Something like a global

variable within the class

™ Local variable

ƒ Method parameter

ƒ Method level variable

ƒ Block level variable

™ A variable is effective at

its declaration level and

all sub-levels

Trang 21

Variable Overriding

™ Variables cannot be declared twice at the same

level and its sub-levels

ƒ except class member variables (overridden by local

variables)

™ Use "this" keyword to refer to the class/object

itself to access (the overridden) member variables

this.title

this.couseId

this.getCourseInfo() //”this” can be used with method

™ It is good to use “this” explicitly for member

variables

Trang 22

ƒ Inheritance & Polymorphism

™ Some useful Java classes

™ Exception Handling, I/O

™ Readings & Exercise

™ Discussion

Trang 23

™ A concept to hide information or other details from the outside programs

™ Why? Should classes and methods

properties be available to other

programs (classes)?

ƒ Cohesion, modularity, integrity

ƒ Hides complexity, consistent and clear

interaction

Trang 24

Access Modifiers

™ Encapsulation is implemented through the use of access modifiers/specifiers

ƒ private - completely encapsulated within the class

private int length;

ƒ default (no modifier) - within the same package

int length;

ƒ public - completely open

public int length;

ƒ (protected)

Trang 25

Access Modifiers for Classes

™ If a class is defined public , then it can

scope)

public class Book { … }

™ Otherwise, if it is defined “default”,

then it can be referenced only by

those in the same package

class Book { … }

™ Note: any java source file can have

only one public class

Trang 26

Java Access Modifiers

Trang 27

™ OO modeling

ƒ Class B is part of class A, or class A consists

of class B (one or many)

™ For example

Trang 28

Aggregation Implementation

™ Aggregation is very common in object modeling and Java programs

™ In Java, this relationship is

implemented as a class member

variable is of a class/object type,

rather than primitive type

Trang 29

Modeling One-to-One

™ Class A has a class B

™ A class (A)’s member property (p) is directly defined as another class (B) type

Trang 30

Modeling One-to-Many

™ Class A has several class B’s

™ A class (A)’s member property (p) is

defined as a collection of class (B)

Trang 31

™ OO modeling

ƒ “Is-A” relationship between two classes

ƒ One class (child/subclass) will inherit

properties and behaviors of another class

(parent/super class)

™ For example, if “Faculty” is a

“Employee”

ƒ “Faculty” is the subclass

Trang 32

Inheritance in Java

™ In Java class implementation

ƒ A subclass will inherit all available member

variables and methods of its super class

(availability is defined using access modifiers)

™ Use the statement “extends” to define inheritance

class Faculty extends Employee

{ … }

™ Using the super Keyword refers to the superclass

Trang 33

Overriding Methods

™ Overriding method : the method is

defined in the subclass using the

same signature and same return type

as in its superclass

™ Overriding vs Overloading

™ Example: Section 9.4

Trang 34

™ Polymorphism allows methods to be used

generically for a wide range of object

arguments (generic programming)

™ Example: 9.7

™ Casting

ƒ Implicit casting: Object o = new Student();

ƒ Explicit casting: Student b = (Student) o

™ Demonstrating Polymorphism and Casting :

TestPolymorphismCasting.java

Trang 35

Abstract Classes

™ Abstract classes

ƒ But you cannot create instances of abstract

™ An abstract method

ƒ Its implementation is provided by the

subclasses

be declared abstract

™ Example TestGeometricObject.java

Trang 36

Interfaces

™ An interface is a classlike construct

that contains only constants and

Trang 37

Interface vs Abstract class

Trang 38

Multiple inheritance

™ Java allows

ƒ Only single inheritance for class extension

ƒ But multiple extensions for interfaces

™ Subinterface:

ƒ An interface can inherit other interfaces using the extends keyword

public class NewClass extends BaseClass

implementes Interface1, , InterfaceN{

}

public interface NewInterface extends Interface1, , InterfaceN {

// constants and abstract methods

Trang 39

™ Some useful Java classes

™ Exception Handling, I/O

™ Readings & Exercise

™ Discussion

Trang 40

Some useful Java classes (1)

Trang 41

Some useful Java classes (2)

™ StringBuilder/StringBuffer Class

ƒ Use StringBuffer if it may be accessed by

multiple tasks concurrently

ƒ StringBuilder (1.5 or above) is more efficient if

it is accessed by a single task

™ Modifying Strings in the Buffer with

methods:

ƒ insert()

ƒ delete()

Trang 42

Some useful Java classes (3)

™ Reading text: java.util.Scanner

Scanner input = new Scanner(System.in);

Or Scanner input = new Scanner(new File(filename));

Trang 43

Some useful Java classes (4)

Trang 44

™ Some useful Java classes

™ Exception Handling, I/O

™ Readings & Exercise

™ Discussion

Trang 45

Exception Handling, I/O (1)

™ Run-time error occurs when a program

encounters an event that will break the

normal execution of the program

™ Traditional error handling is unstructured

and tightly binds the error handling code in the program flow

™ Java handles errors (exceptions)

ƒ in an object oriented way

ƒ with a systematical and structured mechanism

Trang 46

Exception Handling, I/O (2)

™ Exception handling advantages

classified – easy learning and prediction

ƒ Flexible for user application to handle the

exception

Trang 47

try {

statements that might cause errors }

statements to handle errors }

finally {

statements that will be executed under all conditions

}

Trang 48

Handling Multiple Exceptions

™ Exception handling is very easy

effective when handling multiple

potential exceptions

™ Using “java.lang.Exception” to

capture all potential exceptions and deal with them consistently

™ Using multiple “catch” block to

capture specific exceptions and deal

Trang 49

Typical Java Exceptions

™ Exceptions are defined using various classes

in various packages, but all inherit the

java.lang.Exception class

Trang 50

Understanding Exception Handling

™ Java's exception-handling model with

3 operations:

ƒ Declaring an exception,

Trang 51

Readings & Exercise

Trang 52

Discussion

Ngày đăng: 27/06/2014, 21:20

TỪ KHÓA LIÊN QUAN