Chapter 2: Java OOP I

54
Xiang Zhang [email protected] Chapter 2: Java OOP I

Transcript of Chapter 2: Java OOP I

Page 1: Chapter 2: Java OOP I

X i a n g Z h a n g

j a v a c o s e @ q q . c o m

Chapter 2: Java OOP I

Page 2: Chapter 2: Java OOP I

2

Content

OO Concepts

Class and Objects

Package

Field

Method

Main method

Object

Construct and Initialization

Access Control

Page 3: Chapter 2: Java OOP I

3

Object Oriented Programming

Object

Class

Abstraction

Design / Implementation

Inheritance

Common / Special

Polymorphism

Method / Behavior

Example:People live in houses(Class), and houses

usually look like this(abstraction). I have my

own house (Object). It is a little special

because it is in Georgian style (Inheritance).

So you can call it a house or a Georgian

house.

Page 4: Chapter 2: Java OOP I

Class: concepts that have attributes and behavior

Object: instances that can interact

Abstraction: design first, implement later

Inheritance: reuse the code by parent-child classes

Polymorphism: same method, different behavior

Page 5: Chapter 2: Java OOP I

5

Example on Polymorphism

declared type runtime type / actual type

is it correct?

Open Discussion: Why?

Page 6: Chapter 2: Java OOP I

6

Class and Object

Class

Often describes a hierarchical concepts, such as: Person、Bird、Order

A class usually has some attributes and behaviors:

Attributes are called Fields, such as the age of a Person

Most attributes character the difference between objects, but some

attributes are common, such as each Person has two legs, this kind of shared

attributes is called Static attributes

Behaviors are called Methods, and methods can be static too.

IN Java, the hierarchy of classes is a tree

Object

Instances of class. Such as a Person called tom, a octopus called paul…

Page 7: Chapter 2: Java OOP I

7

Class and Objects – Constructing a Class

name your package

name the classdetermine the

inheritance

determine the special attributes

and methodsinitialization

determine the access control

complete the class body

Page 8: Chapter 2: Java OOP I

8

A Simple Class

Page 9: Chapter 2: Java OOP I

9

Class Components

Package name/ Class name

import

Members

Field - static / non-static

Method - static / non-static

Access Modifier(Class / Field / Method)

public / abstract / final

public / protected / private

Page 10: Chapter 2: Java OOP I

10

Class and Object – Package

Package is a set of Classes

To avoid classes with same names

To manage classes

Define a package

package javacourse;

package cn.edu.seu.cose.javacourse;

Import a package or a class

import java.io.*;

import java.io.File;

Page 11: Chapter 2: Java OOP I

11

Class and Object – Field

Define a Field

Access Modifier

Static Modifier(

Optional)

Type

Name

Non-static

Static

Page 12: Chapter 2: Java OOP I
Page 13: Chapter 2: Java OOP I

the

correct

code

Page 14: Chapter 2: Java OOP I

14

Class and Object – Method

Define a Method

Access Modifier

Static Modifier (Optional)

Return Type

Name

Parameter List

(Type + Name)

Method Body

Can this method be static ??

Can this method be static ??

Anything wrong with this class?

Page 15: Chapter 2: Java OOP I

15

Class and Object – Method

Static Method

Page 16: Chapter 2: Java OOP I

16

More About Static – Static Variable

Person

Tom Jack Mary Kate

new new

18 20 25 16

age age age age

Non-static Variable

(Object-level Variable)

Variables are not shared, and

memory-stored with objects.

Person

Tom Jack Mary Kate

new new

Homo Sapiens

Variables are shared, and

memory-stored with class.

Static Variable

(Class-level Variable)

formal name

Page 17: Chapter 2: Java OOP I

17

More About Static – Static Method

Person

Tom Jack Mary Kate

new new

Tom,18 Jack,20 Mary,25 Kate,16

greet() greet() greet() greet()

Non-static Method

(Object-level Method)

Non-static methods are allowed to

visit non-static variables. Each person

may have different behavior.

Person

Tom Jack Mary Kate

new new

Static Variable

(Class-level Method)

Static methods are not allowed to visit

non-static variables. Each person has

the same behavior.

sneeze()

Page 18: Chapter 2: Java OOP I

18

Lab Work 1

Create a StudentInfoSystem (SIS in short) class

With an INNER class Student, which defines the id, name,

gender and javaScore(int);

In SIS,create a class with several students (max=20);

Using a static counter and getCounter() to count #students

Using a scanner and a menu to input student information;

Using a Student[] to store all the information;

Rank the list of students according to their javaScore;

Print out the ranked list of students

2009

Page 19: Chapter 2: Java OOP I

19

Lab Work 1 – Inner Class

2009

Page 20: Chapter 2: Java OOP I

20

Class and Object – Overloading

Method Overloading(重载)

Method Name

Method Signature

Method name

Number of Parameters

Types of Parameters

Multiple methods with same name in a class: OK (Overloading)

Multiple methods with same signature in a class: No!

Signature does not include return type, because signature

reflects the specification of behavior, not the result of behavior.

Page 21: Chapter 2: Java OOP I

21

Class and Object – Overloading

Examples:

Page 22: Chapter 2: Java OOP I

22

Class and Object – Parameters

Forget them:

Formal Parameter?

Actual Parameter?

Pass by Value?

Pass by Reference?

In Java, the Copy of Parameter is passed.

What is copied?

For primary types, their value is copied

For objects, the reference is copied. (What is a reference?)

Page 23: Chapter 2: Java OOP I

23

Class and Object – Parameter

Try:

Page 24: Chapter 2: Java OOP I

24

Class and Object – Method

Try again:

Page 25: Chapter 2: Java OOP I

String name “Tom”

Memory

Person String

“Jerry”

reference of tom

reference of jerry

String

Page 26: Chapter 2: Java OOP I
Page 27: Chapter 2: Java OOP I
Page 28: Chapter 2: Java OOP I

28

Self-study

Methods with variable number of parameters

Page 29: Chapter 2: Java OOP I

29

Class and Object – main method

Each class can have a main method or not

The main method indicates the entrance of

execution

Each main method looks like this:

Page 30: Chapter 2: Java OOP I

30

Class and Object – Object

Declare a reference of an object, but not create it

Declare a reference of an object, and create the object

Null reference: Person tom = null

Security: Reference >> Pointer

Page 31: Chapter 2: Java OOP I

31

Self-study

Storage of Objects

Registers - inside the processors

Stack - object reference, primary types (栈)

Heap – object themselves (堆)

Method Area – methods, static data

Constant Pool

Non-RAM

Streamed Object

Persistent Object

Page 32: Chapter 2: Java OOP I

32

Class and Object – Object

Destroying Object

Java GC (Garbage Collection)

finalize();

Try:

Page 33: Chapter 2: Java OOP I
Page 34: Chapter 2: Java OOP I

34

Class and Object – Object

All classes in Java inherits java.lang.Object

All objects in Java have following methods:

Page 35: Chapter 2: Java OOP I

35

toString()

1) What will happen?

2) How to print the name of

Tom?

Page 36: Chapter 2: Java OOP I

36

Lab Work 2

// output the name of tom

Page 37: Chapter 2: Java OOP I

37

Lab Work 3

Student

id //学号

name //姓名

Requirement

Create two students a and b with same id;

Use a.equals(b) to identify if a is the same student like b;

You need to overwrite .equals() method;

2009

Page 38: Chapter 2: Java OOP I

38

Comparing Two Strings

2009

Page 39: Chapter 2: Java OOP I

39

Lab Work 4

Try to print out .hashCode() of a certain object;

2009

Page 40: Chapter 2: Java OOP I

40

Reference

Inside The Java Virtual Machine (深入浅出Java虚

拟机)

Page 41: Chapter 2: Java OOP I

41

Construction and Initialization

How to describe the construction of an object in

a class?

Constructor

Default Constructor

Constructor with parameters

Initialization Block (self-study)

Page 42: Chapter 2: Java OOP I

42

Construction and Initialization

Constructor

Default

With Parameters

Page 43: Chapter 2: Java OOP I

43

About Construction

Is this compilable? Is this runnable? If yes, What is the result?

Page 44: Chapter 2: Java OOP I

44

Self-study

Initialization

Block

Attention: something is not correct in this code

Page 45: Chapter 2: Java OOP I

45

Self-study

Static

Initialization

BlockGet initial ID from database

Page 46: Chapter 2: Java OOP I

46

Self-study

Think:

Why use initialization blocks?

What is the difference between initialization blocks and

static initialization blocks?

Page 47: Chapter 2: Java OOP I

47

Class and Object – Access Control

Why Do We Need Access Control?

Encapsulation

Data Hiding

Without Access Control:

Debugging becomes difficult

Data and programs become unsafe

Page 48: Chapter 2: Java OOP I

48

Class and Object – Access Control

AC Modifier for Classes

default

public

AC Modifier for Members

default(package)

public

private

protected

Page 49: Chapter 2: Java OOP I

49

Class and Object – Access Control

Same class Same packageSubclass in different package

Non-Subclass in different package

public OK OK OK OK

protected OK OK OK NO

default(package) OK OK NO NO

private OK NO NO NO

Page 50: Chapter 2: Java OOP I

50

Class and Object – Access Control

Getter and Setter Methods

Page 51: Chapter 2: Java OOP I

51

Self-study

Data, Information and

Knowledge

Non-structural, semi-

structural and

structural data

XML

XML and XML Schema

XML vs. HTML

* Ant

Page 52: Chapter 2: Java OOP I

52

Lab Work 5 – Student Management System

Two classes: System and Student

System: input (create students/ modify students) and output

(show statistics)

Student: getter and setters of each student (name, age, score)

Modify Student Information using name;

Maximum 50 students

Statistics: Average Score/ Max / Min

Page 53: Chapter 2: Java OOP I
Page 54: Chapter 2: Java OOP I

54

Forecast

Abstraction

Abstract Class

Interface

Inheritance

Polymorphism