0% found this document useful (0 votes)
19 views50 pages

Object-oriented Programming Language (JAVA) : 李建欣 (Jianxin Li)

Uploaded by

creation portal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views50 pages

Object-oriented Programming Language (JAVA) : 李建欣 (Jianxin Li)

Uploaded by

creation portal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 50

Object-oriented Programming

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

Use System.out.print(…) and


System.out.println(…)

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”);

System.out.println(“Test of a simple calculation:”);


int ans;
ans = 2 + 2;
System.out.println(“2 plus 2 is “ + ans + “ !!!”);
}
}
5
© Beihang University
Chapter-2

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

An object contains:


A set of fields or variables (which models the
attributes/characteristics of the real world object) and
A set of functions/methods (which models the
behaviors of the real world object).
The data and methods of an object are closely tied
together.

An object is not just an instance of data structure

11
© Beihang University
The two part of an objects

Object = Data + Methods


or to say the same differently:

An object has the responsibility to know and


the responsibility to do.

= +
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.

We can drive a car without knowing how the engine,


transmission and other parts work internally.

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

Public static void (){


Human alice = new Human();
Bike redbike = new Bike();
Alice.bike = redbike;}

19
© Beihang University
The Benefits of Message Mechanism

The behaviors of Object are expressed based on it,


so message transition supports all the interactions
between objects.
The interacted objects can locate in different
processes, even different machines.

MSN, Skype, OICQ…

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.

The relationship between object and class is like that


between variable and type in C.

23
© Beihang University
Object vs. Class

Now map this Object to a generalized form such


that it looks like

24
© Beihang University
“class” is a general form of “struct”

“struct” in C/C++ is a data structure contains


data only.

“class” in C++/Java is a structure contains both


data and functions/methods which are used to
process data or produce an action.

25
© Beihang University
Structure of Class

26
© Beihang University
Class Example- Bicycle

27
© Beihang University
Two Instances of Class (Bike)

28
© Beihang University
Object

Attributes Behaviors Messages


Class instant Variables Methods Method Calls
Properties Functions Events
Signals
Car
Color IncreaseSpeed() OnGearChange()
Speed_Limits WindowUp() OnNearMaxSpeed()
Number_Of_Gears Break() OnProximity()
Auto_or_Manual OnBadRoadDetect()

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

Keyword: extends Keyword: extends

SubClass

Keyword: extends

SubClass

41
© Beihang University
Java Platform Class Hierarchy

The Object class, defined in the java.lang package,


defines and implements behavior common to all
classes—including the ones that you write. In the
Java platform, many classes derive directly from
Object, other classes derive from some of those
classes, and so on, forming a hierarchy of classes.

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

Note that the method signatures have no braces


and are terminated with a semicolon.
46
© Beihang University
Implemented Class

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

You might also like