Object-oriented Programming Language (JAVA) : 李建欣 (Jianxin Li)
Object-oriented Programming Language (JAVA) : 李建欣 (Jianxin Li)
Language (JAVA)
(面向对象程序设计)
李建欣(Jianxin Li)
[email protected]
School of Computer, Beihang University
© Beihang University
How about your tools?
Eclipse Luna
JDK 8
jdk-8u20-windows-x64
Some demos
2
© Beihang University
Chapter 1 Review
Java Features
Java Grammar
Java Running System
Java Application
Java Example
3
© Beihang University
Exercise-1
Topic 1:
Writing a Helloxxxx that can output:
Hello
Hello World
hello
world
4
© Beihang University
A simple Java program
keyword Name of class
public class HelloWorldApp
The content of this program
{ should be contained in a
public static void main (String[ ] args) file named
{ “HelloWorldApp.java”
System.out.println(“Hello World!”);
System.out.println(“Welcome to Java!!\n”);
Object-oriented Programming
6
© Beihang University
2.1 Program Designing of OOP
Real World
Entity
Abstract
World
Concept
Abstract DataType
Convert
Computer
Class
Instance Abstract
Object
7
© Beihang University
Program Designing of OOP
Object-oriented Programming is to build a model in
programs of real entity or abstracted entity in a real
world with object and class.
Some basic concepts of OOP need to be learnt
before you want to write any code.
This lesson will introduce to you about objects,
classes, inheritance, interfaces, etc..
It needs us to know on how these concepts relate to
the real world.
It also simultaneously provides an introduction to the
syntax of the Java programming language.
8
© Beihang University
Basic Concepts in OOP
What is Object
What is Message
What is Class
What is Inheritance
What is Interface
9
© Beihang University
2.2 What is an Object [物件,对象]?
In plain English, “object” is a “thing” in the
real world.
In programming, “object” is the “thing” (data +
functions or attributes + behaviors) that models the
real world object which we have to deal with.
While solving a problem, it is easier to group
all related data to form a data “object” to mirror
the real world object.
10
© Beihang University
Key Points of Object
11
© Beihang University
The two part of an objects
= +
12
12
© Beihang University
Object Example-Bicycle
13
© Beihang University
Object Instance
An instance is a specified object
14
© Beihang University
Encapsulation (封装)
Objects communicate with one another through
well-defined interfaces.
Objects are not supposed to know how other
objects are implemented.
Implementation details are hidden within the objects
themselves.
15
© Beihang University
What is Message?
Messages are used to communicate between
Objects
16
© Beihang University
Construction of Message
Three parts of Message
The object which receives the message
The name of method
The parameter of method
17
© Beihang University
An Example of Message
18
© Beihang University
Class Human
Bike bike = new Bike();
Class Bike
19
© Beihang University
The Benefits of Message Mechanism
20
© Beihang University
Classes[类] and Objects
A class is a blueprint, or prototype, that defines the
variables and the methods common to all objects of a
certain kind.
In our daily life, objects have general properties (attributes)
that characterize the group each object belonged to.
For example: “I” am a “teacher”.
“You” are a “student”.
or “John” is a “teacher”.
“Mary” is a “student”.
Both “I”, “You”, “John” & “Mary” are “objects”.
“I” & “John” belong to the class of “Teacher”.
“You” & “Mary” belong to the class of “Student”.
21
© Beihang University
What is a class?
Group of Objects with similar
properties (attributes).
behavior.
relationships to other objects.
semantics
Example
Parrot
Girl Boy
charlie
jullie kim dylan
22
22
© Beihang University
Object is an instance[例] of a class
In programming language,
“I” & “John” are instances of the “teacher” class.
“You” & “Mary” are instances of the “student” class.
In general, any object in a program is an instance of
a class.
23
© Beihang University
Object vs. Class
24
© Beihang University
“class” is a general form of “struct”
25
© Beihang University
Structure of Class
26
© Beihang University
Class Example- Bicycle
27
© Beihang University
Two Instances of Class (Bike)
28
© Beihang University
Object
Employ
Name,ID CalcSalary() OnEntrance
SetWorkingHours() OnLeave()
29
© Beihang University
Access Modifiers
Access Modifiers
The first (left-most) modifier used lets you control
what other classes have access to a member field.
For the moment, consider only public and private.
Other access modifiers will be discussed later.
public modifier—the field is accessible from all
classes.
private modifier—the field is accessible only within its
own class.
30
© Beihang University
Variables and Methods in class
Variable
instance variable
Including the state of specific object
class variable
Shared by all instance of class
Method
instance method
The method is accessed through instances
static method
The method is invoked through class
31
© Beihang University
Declaring Member Variables
Member variables in a class
---these are called fields.
Variables in a method or block of code
—these are called local variables.
Variables in method declarations
—these are called parameters.
Field declarations are composed of three
components, in order:
Zero or more modifiers, such as public or private.
The field's type.
The field's name.
32
© Beihang University
Class & Objects
A class can contain any of the following variable types.
Local variables. variables defined inside methods,
constructors or blocks are called local variables. The
variable will be declared and initialized within the method
and the variable will be destroyed when the method has
completed.
Instance variables. Instance variables are variables
within a class but outside any method. These variables are
instantiated when the class is loaded. Instance variables
can be accessed from inside any method, constructor or
blocks of that particular class.
Class variables. Class variables are variables declared
with in a class, outside any method, with the static
keyword.
33
© Beihang University
Bicycle Class Example
34
© Beihang University
Class{
Function a(){
}
}
35
© Beihang University
Methods
A method is basically a behavior. A class can
contain many methods. It is in methods where the
logics are written, data is manipulated and all the
actions are executed.
When you call the System.out.println() method, for
example, the system actually executes several
statements in order to display a message on the
console.
36
© Beihang University
Methods
37
© Beihang University
What is Inheritance?
38
© Beihang University
Inheritance
A class that is derived from another class is called a
subclass (also a derived class, extended class, or child
class).
The class from which the subclass is derived is called a
superclass (also a base class or a parent class).
The idea of inheritance is simple but powerful: When you
want to create a new class and there is already a class that
includes some of the code that you want, you can derive your
new class from the existing class. In doing this, you can reuse
the fields and methods of the existing class without having to
write (and debug!) them yourself.
39
© Beihang University
The Meaning of Inheritance
Class fields (variable)
The inherited fields can be used directly, just like any other
fields.
You can declare new fields in the subclass that are not in the
superclass.
Class Methods
The inherited methods can be used directly as they are.
You can declare new methods in the subclass that are not in the
superclass.
You can write a new instance method in the subclass that
has the same signature as the one in the superclass, thus
overriding it.
You can write a new static method in the subclass that has
the same signature as the one in the superclass, thus hiding it.
40
© Beihang University
Class Hierarchy
SuperClass
SubClass
Keyword: extends
SubClass
41
© Beihang University
Java Platform Class Hierarchy
42
© Beihang University
Example
43
© Beihang University
What is Interface
There are a number of situations in software
engineering when it is important for disparate groups
of programmers to agree to a "contract" that spells
out how their software interacts. Each group should
be able to write their code without any knowledge of
how the other group's code is written. Generally
speaking, interfaces are such contracts.
44
© Beihang University
Interface in Java
In the Java programming language, an interface is
a reference type, similar to a class, that can contain
only constants, method signatures, and nested types.
There are no method bodies.
Interfaces cannot be instantiated—they can only
be implemented by classes or extended by other
interfaces.
45
© Beihang University
Interface Definition
47
© Beihang University
The key points in this lesson
Object
Class
Inheritance
Interface
48
© Beihang University
Exercise-2
Discussion
Giving a similar object, class, interface, inheritance
example (including some Java demo codes), for
example, bicycle.
Interface: IA
Abstract: A Abstract: C
Class: B Class: D
49
© Beihang University
Following Lessons
Chapter3
Java Language Basics
Chapter4
Java OOP
Chapter5
High Level Java Language
50
© Beihang University