0% found this document useful (0 votes)
2 views13 pages

04 Concept Object Oriented

The document outlines key concepts of object-oriented programming, including objects, classes, inheritance, and polymorphism. It explains the properties of objects such as identity, state, and behavior, as well as the importance of encapsulation and interaction between objects. Additionally, it discusses the advantages of inheritance for code reusability and flexibility in software development.

Uploaded by

yuyutong20060724
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)
2 views13 pages

04 Concept Object Oriented

The document outlines key concepts of object-oriented programming, including objects, classes, inheritance, and polymorphism. It explains the properties of objects such as identity, state, and behavior, as well as the importance of encapsulation and interaction between objects. Additionally, it discusses the advantages of inheritance for code reusability and flexibility in software development.

Uploaded by

yuyutong20060724
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/ 13

2025/3/27

Objectives

 Object

 Class

 Inheritance
 Single Inheritance
 Multiple Inheritance
 Polymorphism

Object and Class


 concept of an object
 interaction of objects
 concept of a class
 relationship between classes and objects

11

1
2025/3/27

The concept of an object


 What is an object
 Property
 Identity
 Behavior
 State
 Encapsulation of object data

12

What is an object?
 Anobject is a meaningful thing in the application
domain. It can be a physical entity or a concept.
 Physical Entity

 Concept

Chemical Process

13

2
2025/3/27

Properties of an object
 Identity
 Each object must have a name to distinguish it from
other objects
 “Identifiers” are used to accomplish this task in a program
 State
 Use the state (or attributes) to describe certain
characteristics of an object
 Use "variables" (which can be simple or complex) to
accomplish this task
 Behavior it might be another object

 It is composed of a set of operations, and each operation


determines a behavior of the object
 Use “function” to accomplish this task in a program

14

Identity
 Each object has a unique identifier to distinguish it from
other objects, even if its state may be the same as that
of other objects.

TV,No. 1, Row 1

TV,No. 2, Row 1

TV11
TV,No. 3, Row 1

TV12

TV13

15

3
2025/3/27

State
 It is all values possessed by each attribute at a
certain moment
 Student
 NO, set, age, grade, scores, etc.
 TV
 color, volume, brightness, channel, system, etc.
 The "grade" attribute of a student will change according
to whether the student is promoted to the next grade or
retained in the current grade
 The “channel” attribute of a TV will change according to
the choice of the person watching TV.

16

behavior
 When other objects send requests, the object responds
in a certain way.

17

4
2025/3/27

Encapsulation of object data


 Theidea of object encapsulation is to shield the
internal information of an object.
 People can only perform operations on the object
through the permitted operations on the object's
interface, and change the object's state.

adjustVolume

color
brightness
volume
channel
system

18

Interaction of objects
 An object that is completely isolated is of no use
 Thereshould be interactions among objects when the
system is running.
 How to accomplish the interactions of objects?
 message passing
 A message is the request information sent by the
sending object to the receiving object to invoke a
behavior of receiving object, and when necessary, it
includes the information of appropriate parameter
 Composition of the message
 Identity of receiving object
 Name of the invoked operation(function, behavior)
 arguments

19

5
2025/3/27

Interaction of objects
adjustVolume

color
Receiving
Object brightness
Name of
operation
volume
arguments
channel
system

TV12 adjustChannel Up

TV12

20

Properties of Message
 The same object can receive multiple messages in different
forms and make different responses.
 “TV11 adjustChannel down”
 “TV11 adjustVolume louder”
 “TV11 adjustColor intense”
 Messages in the same form can be sent to different objects,
and the responses they elicit can vary
 “TV11 adjustChannel up”(from 15  to 16)
 “TV12 adjustChannle up”(from 5to 6)
 The sending of a message does not need to take the specific
recipient into account. An object may or may not respond to a
message.
 message multicast

21

6
2025/3/27

Concept of a class
A class is a collection of objects that have the same data
composition and the same behavior.

data composition behavior

color adjustColor

volume adjustVolume

system adjustSystem

channel adjustChannel

brightness adjustBrightness
open
close
TV

22

Relationship between classes and objects

 Every object is an instance of a certain class.


 At any given moment, each class has zero or more
instances (objects)
 Classes are static
 Their existence, semantics, and relationships are already
defined before execution.
 Objects are dynamic
 They can be created and deleted during the execution of
a program

23

7
2025/3/27

Abstraction and Encapsulation


 Data Abstraction

 Data Encapsulation

24

Data Abstraction
 An object is an abstraction of an entity in the real
world
 Abstraction is a kind of simplified description
 It emphasizes the characteristics relevant to the
given application.
 It suppresses the irrelevant characteristics.
 A class is an abstraction of a group of objects

25

8
2025/3/27

Encapsulation
 A class is divided into two parts: the interface and the implementation.
 For users, the interface is visible, while the implementation is invisible
 Only the operations provided by the class can modify the attribute
values.
 Hiding the implementation details of a class is called encapsulation.

implementation

interface balance
interestYTD
makeDeposit owner
User withdraw account_number
transfer makeDeposit
withDraw
transfer
an impenetrable wall

26

Advantages of encapsulation
 two types of protection
 Protect objects from being misused by users.
 Users have no right to access implementation, such as attribute
values and operation processes.
 Protect client - side code when the implementation of an
object changes.
 Changes in the object's implementation do not affect the client -
side code.

 The implementation may change, for example


 Fixing defects.
 Improving performance.
 Changing methods.
 Encapsulation can reduce the “Ripple Effect”.

27

9
2025/3/27

A Real Live Object

Here is a person, Jim.


Jim has a quantity of
money. How can you
determine how much
money Jim has?

Take Jim's wallet and start counting.

Ask Jim, "How much money do you have?"

28

Inheritance
 Superclass and Subclass
 Multiple Inheritance

29

10
2025/3/27

Superclass and Subclass


 A class definesa group of objects that have the same
structure and operations.
 Inheritance isan important feature of classes for
achieving reusability and extensibility.
A class can be defined as a special case of another more
general class.
 This general class is the superclass of the special class.
 The special class is the subclass of this general class.

Superclass Electronic

Subclass TV Telephone Computer

30

Superclass and Subclass


A subclass inherits all the attributes and operations of its superclass.
A subclass can also define its own unique attributes and operations.
Electronic
manufacturer Computer
model manufacturer
price model
color price
getModel monitortype
memorysize
getModel
Computer
getMonitorType
monitortype
memorysize
getMonitorType

31

11
2025/3/27

Superclass and Subclass


 The inheritance relationship is an "is a" relationship.
 If class B inherits from class A, then any object of class B
"is a" object of class A.
 A "television" "is an" electronic product.

 The "is a" relationship is transitive.


 Because of the "is a" relationship, classes form a
hierarchical association.
 The Square class inherits from the Rectangle class.
 Both the Rectangle class and the Triangle class inherit from the
Polygon class.

32

Superclass and Subclass


Polygon
Vertices
Translate
Rotate
Display
Perimeter

Rectangle Triangle
Length
Width
Diagonal

Square

33

12
2025/3/27

Superclass and Subclass


 Inheritance is a key feature for achieving code
reusability and extensibility.
 The reusability feature enables the handling of the
common parts of a group of objects with similar
structures, avoiding the repetition of code over and
over again.
 It avoids the waste of time and space.
 It also avoids inconsistencies.
 The extensibility feature enables the handling of
the diversity of classes in different situations.
 It allows for the expansion and specification of the
original code to meet different needs.

34

Advantages of Inheritance
 Increase the opportunities for software reuse.
 Reduce development and maintenance costs.
 Develop a model that is closer to reality.
 Make the system more flexible.
 All subclasses automatically inherit the changes of the
superclass.
 Deal with changes in requirements by adding a subclass.
 Ensure consistency among classes.
 The superclass can customize rules for all subclasses.
 Inherit the interface and implementation.
 Support polymorphism.

37

13

You might also like