[oop]chapter 01

39
METHODOLOGY OF THE OBJECT-ORIENTED PROGRAMMING Lecturer: TR N ĐỨC TÂM Day 02 Chapter 01

Upload: cuong-nguyen

Post on 04-Jun-2018

217 views

Category:

Documents


0 download

TRANSCRIPT

8/13/2019 [OOP]Chapter 01

http://slidepdf.com/reader/full/oopchapter-01 1/39

METHODOLOGY OF

THE OBJECT-ORIENTED PROGRAMMI

Lecturer: TRẦN ĐỨC TÂM

Day 02

Chapter 01

8/13/2019 [OOP]Chapter 01

http://slidepdf.com/reader/full/oopchapter-01 2/39

CONTENTS AT A GLANCE

2/13/2014 Designed by Tamars

No. Title

1SOME STYLES OF PROGRAMMING WHICH BE LEARNED

- CÁC PHƯƠNG PHÁP LẬP TRÌNH TRUYỀN THỐNG;

2OBJECT-ORIENTED PROGRAMMING (OOP) APPROACH

- TIẾP CẬN PHƯƠNG PHÁP LẬP TRÌNH HƯỚNG ĐỐI TƯỢNG;

3THE POWERFUL ADVANTAGES OF OOP

- CÁC TÍNH CHẤT QUAN TRỌNG TRONG LẬP TRÌNH HƯỚNG ĐỐI

8/13/2019 [OOP]Chapter 01

http://slidepdf.com/reader/full/oopchapter-01 3/39

SOME STYLES OF PROGRAMMINGWHICH BE LEARNED

Sequential ProgrammingStructured Programming

Procedural Programming

2/13/2014Designed by Tamars

1

8/13/2019 [OOP]Chapter 01

http://slidepdf.com/reader/full/oopchapter-01 4/39

SOME STYLES OF PROGRAMMING …

1.  Sequential Programming – Lập trình tuần tự  (hay Lập trình tuyếntính)

o   A program consisted of a sequence of instructions, where eachinstruction executed one after the other. It ran from start tofinish on a single processor.

Một chương trình sẽ là một tập hợp các câu lệnh có thứ tự chạy

nối tiếp nhau. Chương trình sẽ chạy từ  câu lệnh đầu tiên, và kếtthúc ở câu lệnh cuối cùng.

o   Advantages: very simple, for ease in performing solution

o   Disadvantages: not enough to solve the complex problem

2/13/2014 Designed by Tamars

8/13/2019 [OOP]Chapter 01

http://slidepdf.com/reader/full/oopchapter-01 5/39

SOME STYLES OF PROGRAMMING …

Example:

2/13/2014 Designed by Tamars

8/13/2019 [OOP]Chapter 01

http://slidepdf.com/reader/full/oopchapter-01 6/39

SOME STYLES OF PROGRAMMING …

2.   Structured Programming - Lập trình cấu trúc

o   Structured Programming, is a part of procedural programming, makesextensive use of subroutines, block structures, condition and loop statements,as a logical structure.

Sử dụng kết hợp các thủ tục, khối lệnh, câu lệnh điều kiện và vòng lặp.

o   Advantages: more clarity, more quality, more logical,

can solve moderately complex programs, …o   Disadvantages: less code reusability

2/13/2014 Designed by Tamars

8/13/2019 [OOP]Chapter 01

http://slidepdf.com/reader/full/oopchapter-01 7/39

SOME STYLES OF PROGRAMMING …

Example:

2/13/2014 Designed by Tamars

8/13/2019 [OOP]Chapter 01

http://slidepdf.com/reader/full/oopchapter-01 8/39

SOME STYLES OF PROGRAMMING …

3.   Procedural Programming – Lập trình thủ tục

o   Derived from Structured programming.

o   Based upon the concept of the procedure call, which also knownas routines, subroutines or functions.

  Chương trình chính được tạo nên từ tập hợpchương trình con bao gồm thủ tục và/hoặc hàm.

o   Advantages: more code reusability, more independent,more flexible, for ease in debugging

o   Disadvantages: uncontroll data accessing,less data security, unpredictable bug appearing

2/13/2014 Designed by Tamars

8/13/2019 [OOP]Chapter 01

http://slidepdf.com/reader/full/oopchapter-01 9/39

SOME STYLES OF PROGRAMMING …

Example:

2/13/2014 Designed by Tamars

8/13/2019 [OOP]Chapter 01

http://slidepdf.com/reader/full/oopchapter-01 10/39

OBJECT-ORIENTED PROGRAMMIN APPROACH

What is Object?

Procedural vs. OO ProgrammingMoving from Procedural to OO Development

What exactly is a Class?

2/13/2014Designed by Tamars

2

8/13/2019 [OOP]Chapter 01

http://slidepdf.com/reader/full/oopchapter-01 11/39

What is Object?

2/14/2014 Designed by Tamars

8/13/2019 [OOP]Chapter 01

http://slidepdf.com/reader/full/oopchapter-01 12/39

What is Object?

o   Attributes:

  name

  eyeColor

  skin

  height

o   Behaviors:

  Talking

  Writing

  Running

2/14/2014 Designed by Tamars

8/13/2019 [OOP]Chapter 01

http://slidepdf.com/reader/full/oopchapter-01 13/39

Procedural vs. OO Programming

1.  In Design

2/13/2014 Designed by Tamars

Procedural Programming OO Programming

struct {data members;

};

function();

procedure();

data behaviors

class myObject {attributes;methods;

}

8/13/2019 [OOP]Chapter 01

http://slidepdf.com/reader/full/oopchapter-01 14/39

Procedural vs. OO Programming

2.  In communication

2/13/2014 Designed by Tamars

Procedural Programming OO Programming

8/13/2019 [OOP]Chapter 01

http://slidepdf.com/reader/full/oopchapter-01 15/39

Procedural vs. OO Programming

3.   Difference between Procedural and OO Programming

2/13/2014 Designed by Tamars

Procedural Programming OO Programming

The attrubites and behaviors areseparated

The attributes and behaviors are cwithin a sin

The program is represented as a logicalstructure

The program is written as a coobjects which communicate

Can not transport internal data, usingglobal data Uncontrol data accessing

Prevent data from outside Control access to attributes of

DATAHIDING

Data mem  Attribute

Behavior  Method RESTRICTING ACC

8/13/2019 [OOP]Chapter 01

http://slidepdf.com/reader/full/oopchapter-01 16/39

Moving from Procedural to OO Developme

2/13/2014 Designed by Tamars

What happens when another object want

access to data or use method of your obje

AnotherObject

Data

Method

Method

Method

Method

DataMethod

Method

Request message

Response message

8/13/2019 [OOP]Chapter 01

http://slidepdf.com/reader/full/oopchapter-01 17/39

Moving from Procedural to OO Developme

In OOP, each of attributes has corresponding methods, such as getAttr() ais called Getters and Setters

2/13/2014 Designed by Tamars

Getter

is also called

accessor*

me

Setter mutator*

* Accessor  is a method returns the value of the private attribut*Mutator  is a method used to control changes to the private attribut

8/13/2019 [OOP]Chapter 01

http://slidepdf.com/reader/full/oopchapter-01 18/39

Moving from Procedural to OO Developme

 Another object only see the interface of the methods, not the implementation

2/13/2014 Designed by Tamars

INTERFACE

OF THE

METHOD

The name of the method

The parameters passed to the method

The return type of the method

8/13/2019 [OOP]Chapter 01

http://slidepdf.com/reader/full/oopchapter-01 19/39

UML Class Diagrams

2/13/2014 Designed by Tamars

Class Di

8/13/2019 [OOP]Chapter 01

http://slidepdf.com/reader/full/oopchapter-01 20/39

What exactly is a Class?

In concept:

Class vs. Object     Chicken vs. Egg

In OO Software:

Class vs. Object ≠   Chicken vs. Egg

In database table

Class vs. Object ≡   The definition of the table vs. Each row of the ta

2/14/2014 Designed by Tamars

 A class is an abstractcommon features

group

8/13/2019 [OOP]Chapter 01

http://slidepdf.com/reader/full/oopchapter-01 21/39

What exactly is a Class?

 A class must be designed before you can create an objectby using a predefined class

CookieCutter Cookie1;

CookieCutter Cookie2;

2/14/2014 Designed by Tamars

8/13/2019 [OOP]Chapter 01

http://slidepdf.com/reader/full/oopchapter-01 22/39

What exactly is a Class?

For example: public class Minions {

 //Attributes

private String _name;

private Int32 _height;

 //Methods

public String getName() { return _name; }

public void setName(String n) { _name = n; }public Int32 getHeight() { return _height; }

public void setHeight(Int32 height) { _height =

public void Talking (String msg) {

System.Console.WriteLine(msg);

}

}

2/14/2014 Designed by Tamars

Defenitionof a

MinionsClass

8/13/2019 [OOP]Chapter 01

http://slidepdf.com/reader/full/oopchapter-01 23/39

The parts of the Class

1.  Attribute:o   The attribute will store the state of each object instantiated from the class

2. Method:

o   Methods implement the required behavior of a class

o   Methods are called from other objects (messages)

o   Methods provide the fundamental, internal behavior (private methods) of th

o   Some methods allow other objects to inspect and change the valueattributes.

3. Messages:

o   Messages are the communication mechanism between objects.

o   Messages can be the return value of the public methods, which is invoked b

2/13/2014 Designed by Tamars

8/13/2019 [OOP]Chapter 01

http://slidepdf.com/reader/full/oopchapter-01 24/39

The parts of the Class

2/13/2014 Designed by Tamars

Access Designations

(+) public Other objects can directly access public members

() private Only that specific object can access private mem

(#) protected Allow access to protected members by related ob

8/13/2019 [OOP]Chapter 01

http://slidepdf.com/reader/full/oopchapter-01 25/39

THE POWERFUL ADVANTAGES OFTHE OBJECT-ORIENTED PROGRAMMI

 Abstraction

Encapsulation & Data Hiding

Inheritance

Polymorphism

Composition2/13/2014Designed by Tamars

3

8/13/2019 [OOP]Chapter 01

http://slidepdf.com/reader/full/oopchapter-01 26/39

 ABSTRACTION

o   Abstraction is an emphasis on the idea,

qualities and properties rather than theparticulars.

o   The importance of abstraction is derivedfrom its ability to hide irrelevant details

o   Generally, the abstraction is somethingmore general.

o   Advantages:

  Helps to manager the complexity of alarge system.

  Most modifications can be localizedto just a few modules

2/13/2014 Designed by Tamars

8/13/2019 [OOP]Chapter 01

http://slidepdf.com/reader/full/oopchapter-01 27/39

Encapsulation and Data Hiding

o   Encapsulation is defined by the fact that objects contain both the attributes and b

o   Data Hiding is a major part of encapsulation

2/13/2014 Designed by Tamars

8/13/2019 [OOP]Chapter 01

http://slidepdf.com/reader/full/oopchapter-01 28/39

Encapsulation and Data Hiding

o   Each class design specifies the interfaces for theproper instantiation and operation of objects

o   The interfaces should completely describe howusers of the class interact with the class

2/13/2014 Designed by Tamars Interfaces of the

8/13/2019 [OOP]Chapter 01

http://slidepdf.com/reader/full/oopchapter-01 29/39

Encapsulation and Data Hiding

HOW TO INTERACT WITH THE ATTRIBUTES OF THE CLASSES? 

o   If user need access to an attribute, we have getter

o   If user need change to data of attribute, we have setter

  Easily secure, test and maintenance

2/13/2014 Designed by Tamars

8/13/2019 [OOP]Chapter 01

http://slidepdf.com/reader/full/oopchapter-01 30/39

Inheritance

o   Inheritance allows a class to inherit tha attributes and methods of another class

  Abstracting out common attributes and behaviors

2/13/2014 Designed by Tamars

The Superclass (parent class)

contains all the attributes and

behaviors that are common to

classes that inherit from it

The Subcla

inherits all and behasuperclass. subclass mattributes apertain solely

8/13/2019 [OOP]Chapter 01

http://slidepdf.com/reader/full/oopchapter-01 31/39

Inheritance

o   Inhertitance relationship is often referred to as an “Is – a”  relationship

Example:

Cirle, Square, Star inherit from Shape

because Cirle is a Shape,

Square is a Shape,Star is a Shape, too …

2/13/2014 Designed by Tamars

Circle

Star

8/13/2019 [OOP]Chapter 01

http://slidepdf.com/reader/full/oopchapter-01 32/39

In Greek word:

Polymorphism means “ Many Shapes”Polymorphism

o   Polymorphism is tightly coupled to inheritance;

o   Polymorphism means the ability to take on many forms, the term is applied boand to operations.

2/13/2014 Designed by Tamars

when do you need to use

polymorphism feature

8/13/2019 [OOP]Chapter 01

http://slidepdf.com/reader/full/oopchapter-01 33/39

Polymorphism

2/14/2014 Designed by Tamars

Shape s

s1.Draw

Draw();error

s1 = ne

s1.Draw

s1 = ne

s1.Draw

s1 = ne

s1.Draw

8/13/2019 [OOP]Chapter 01

http://slidepdf.com/reader/full/oopchapter-01 34/39

Polymorphism

How to use the Polymorphism feature

Polymorphicobject

Polymorphic operation:“One Operation, Many Methods”

One Interf Many Impleme

There is a object, and

it has many differentsubtype objects

There is a object, and it has manydifferent subtype objects. All thedifferent subtype objects have to

be able to excute the sameoperation in many ways

There is the Intewhich has a t

method. Any cla

up to use the which has to imp

template m

2/14/2014 Designed by Tamars

In OOP, the Polymorphism   is achieved by using many different techniqmethod overloading, operator overloading, and method overriding.

Overriding  means replacing an implementation of a parent with one from a child

8/13/2019 [OOP]Chapter 01

http://slidepdf.com/reader/full/oopchapter-01 35/39

Composition

o   For example, the computer can contains

the video card, sound card, memory,

processor, …

o   In this way, objects are often built, or

composed, from other objects.

2/14/2014 Designed by Tamars

8/13/2019 [OOP]Chapter 01

http://slidepdf.com/reader/full/oopchapter-01 36/39

Composition

2/14/2014 Designed by Tamars

There are only two ways to build classes from other classes

Inheritance(Is-a Relationship)

RelationshipComposition

(Has-a Relationsh

Dogs and Cats are both Mammalsbecause Dog is-a Mammal and Cat

is-a Mammal

ForExample

The Computer is built by cprocessor, memory, driv

supply, …

UML Design

8/13/2019 [OOP]Chapter 01

http://slidepdf.com/reader/full/oopchapter-01 37/39

CONCLUSION

 You should have a good understanding of the following topics …

2/14/2014Designed by Tamars

4

8/13/2019 [OOP]Chapter 01

http://slidepdf.com/reader/full/oopchapter-01 38/39

Conclusion

2/14/2014 Designed by Tamars

The knowledge … … should be kep

Styles of Programming Sequential, Structured, Procedural, Ob

Object Object is an entity that contains both data

Class A class is an abstraction describing the common

objects in a group of

Abstraction Abstraction is an emphasis on the idea, qualities and pr

than

Three primary OOconcepts

Encapsulation: a single object contains both its data &

can hide what it wants fromInheritance: a class can inherit from another

advantage of the attributes and methods defined by

Polymorphism means that similar objects can responmessage in

Composition Composition means that an object is built from

8/13/2019 [OOP]Chapter 01

http://slidepdf.com/reader/full/oopchapter-01 39/39

QUESTION & ANSWER 

 You have 15 minutes to ask me any question is related the today’s lecture

2/14/2014Designed by Tamars

The end