02_oop_intro

Upload: tuan-binh

Post on 07-Apr-2018

218 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/4/2019 02_OOP_intro

    1/19

    Concepts

    Object-Oriented Programming

  • 8/4/2019 02_OOP_intro

    2/19

    i h c Cng ngh . HQG H N i 2Basic concepts

    OutlineHistory of programming languagesProcedural programming vs. Object-oriented programming

    Basics concepts of OOP

    Readings:Core Java 2 , Section 4.1

  • 8/4/2019 02_OOP_intro

    3/19

    i h c Cng ngh . HQG H N i 3Basic concepts

    History of programming languagesEarly programming languages such as Basic, Fortran... are

    unstructured and allow spaghetti code. Programmers used gotoand gosub to jump to anywhere in the programs.

    10 k=120 gosub 10030 if y > 120 goto 6040 k = k + 150 goto 2060 print k, y70 stop100 y = 3*k*k + 7*k - 3110 return

    Hard to understand, error-prone, difficult to modify.

    Jump to an arbitrary position inthe program

  • 8/4/2019 02_OOP_intro

    4/19

    i h c Cng ngh . HQG H N i 4Basic concepts

    History of programming languagesNext came the new structural

    programming paradigm withlanguages like Algol, Pascal,C...

    int func(int j){

    return (3*j*j + 7*j-3;}

    int main(){

    int k = 1while (func(k) < 120)

    k++;printf("%d\t%d\n", k, func(k));return(0);

    }

    Characteristics of structural / procedural programming paradigm:

    Use of looping structures: for, while, repeat, do-whilePrograms as a series of functions/proceduresProgram source code focus on representing algorithms: how to do things.

  • 8/4/2019 02_OOP_intro

    5/19

    i h c Cng ngh . HQG H N i 5Basic concepts

    Limitation of procedural programmingstruct Date{

    int day;int month;int year;

    };

    Data are separated from

    processesPassive data, active processesNo guarantee of data

    consistancy and constrainstsHard to prevent client code frommodifying librarys data

    Difficulty in code maintenanceProcesses are scattered, butEach must understand the datastructures

    What if

    someDay, someMonth,someYearmake an invalid date?

    void someProcess(){

    ...date.day = someDay;date.month = someMonth;date.day = someYear;...

  • 8/4/2019 02_OOP_intro

    6/19

    i h c Cng ngh . HQG H N i 6Basic concepts

    Object-oriented programmingclass Date

    {public:void setDate(int newDay, int newMonth, int newYear);int getDay() { return day; }...

    private:int day;int month;int year;

    };

    void Date::setDate(int newDay, int newMonth, int newYear){

    //check validity of newDay, newMonth, newYear...//set new values...

    }

    OOP helps remove those limitations

    Data come together with related processing code

    Allow for guarantee of data consistency and constrainstEasier maintenance

  • 8/4/2019 02_OOP_intro

    7/19

    i h c Cng ngh . HQG H N i 7Basic concepts

    What is OOP?

    Object-Oriented ProgrammingMap your problem in the real worldDefine things (objects) which can either do something or have

    something done to themCreate a type (class) for these objects so that you dont have toredo all the work in defining an objects properties and behavior

    STUDENT

    * studentNo- name- birthday

    + enroll()...

    * *enrols in

    * courseID- lecturer- instructor

    + getStudents()+ setLecturer()...

    CLASS

  • 8/4/2019 02_OOP_intro

    8/19

    i h c Cng ngh . HQG H N i 8Basic concepts

    The goals of object-oriented designRobustness : software is capable of handling

    unexpected inputs that are not explicitly defined forits application.

    Nuclear plant control softwareAirplane control software

    Adaptability : software that can evolve over time inresponse to changing conditions in its environment.Web browsers and Internet search engines typicallyinvolve large programs that are used for many years.

    Reusability : the same code should be usable as acomponent of different systems in variousapplications.

    Save time and money

  • 8/4/2019 02_OOP_intro

    9/19

    i h c Cng ngh . HQG H N i 9Basic concepts

    Important OO concepts

    AbstractionObjects & Class

    Object state and behaviorObject identityMessages

    EncapsulationInformation/implementation hiding

    InheritancePolymorphism

    abstraction

    encapsulation

    inheritance polymorphism

    "P.I.Etriangle

  • 8/4/2019 02_OOP_intro

    10/19

    i h c Cng ngh . HQG H N i 10Basic concepts

    AbstractionAbstraction: to distill a complicated systemdown to its most fundamental parts anddescribe these parts in a simple, precise

    language.naming the partsexplaining their functionality

    Examples:Design of data abstract data types (ADT)

  • 8/4/2019 02_OOP_intro

    11/19

    i h c Cng ngh . HQG H N i 11Basic concepts

    AbstractionFive basic characteristics of Smalltalk, the first

    successful object-oriented languageEverything is an object.A program is a bunch of objects telling each other

    what to do by sending messages.Each object has its own memory made up of otherobjects.

    Every object has a type.All objects of a particular type can receive the samemessages.

  • 8/4/2019 02_OOP_intro

    12/19

    i h c Cng ngh . HQG H N i 12Basic concepts

    ObjectsThree components:

    State :Given by objects internal data

    Behaviour :Produced by objects methods

    Identity :Objects in any of its states are unique entities (with uniqueaddresses in memory)

    Different objects could have same values for data (same state)

    An object is manipulated via its handleIn C++: pointers and referencesIn Java: object references

    class Date{public:

    void setDate();int getDay();...

    private:int day;int month;int year;

    };

    Date today = new Date();

    today.setDate(2, 2, 2010);

  • 8/4/2019 02_OOP_intro

    13/19

    i h c Cng ngh . HQG H N i 13Basic concepts

    Messagestoday.setDate(2, 2, 2010);

    A means for object A to request object B to perform one method of Bs.A message consists of:

    Handle of the destination object host ( today )Name of the method to perform ( setDate )Other necessary information arguments ( 2, 2, 2010 )

    In fact, a message is a function call with the host object as the implicitargument (method invocation)However, the concept of messages has great significance to OOP

    Data become activeContrast to the old view of programming that

    All actions are centrally controlledData are passive, procedures manipulate data

  • 8/4/2019 02_OOP_intro

    14/19

    i h c Cng ngh . HQG H N i 14Basic concepts

    Object classes

    Classesare the templates to create objects (instantiate).Each object has the same structure and behaviour as the classfrom which it was created

    Data type Variable relationClasses are what we design and codeObjects are what are created (from a class) at run-time

    class Date{public:

    void setDate();...

    private:int day;...

    };

    Date today = new Date();

    today.setDate(2, 2, 2010);

  • 8/4/2019 02_OOP_intro

    15/19

    i h c Cng ngh . HQG H N i 15Basic concepts

    Encapsulation / Information hidingEncapsulation: to group related things together,so as to use one name to refer to thewhole group.

    Functions/procedures encapsulate instructionsObjects encapsulate data and related procedures

    Information hiding: encapsulate to hideinternal implementation details from outsiders

    Outsiders see only interfacesProgrammers have the freedom in implementing

    the details of a system.The only constraint on the programmer isto maintain the interfacepublic , private , and protected

    class Date{public :

    void setDate(...);int getDay()

    ...private:

    int day;int month;int year;

    };

    void Date::setDate(...){

    //check validity//set new values...

    }

  • 8/4/2019 02_OOP_intro

    16/19

    i h c Cng ngh . HQG H N i 16Basic concepts

    Inheritanceis-a relations

    The general classescan be specialized tomore specific classesReuse of interfaces & implementation

    Mechanism to allow derived classes to possessattributes and operations of base class, as if they weredefined at the derived classWe can design generic services before specialising them

  • 8/4/2019 02_OOP_intro

    17/19

    i h c Cng ngh . HQG H N i 17Basic concepts

    Polymophism

    Polymorphism:Ability to assumedifferent forms or shapes.To exist in more than one form

    Object polymorphism:Objects of different derived classes can be treated as if they areof the same class their common base classObjects of different classes understand the same message in

    different waysexample: on receiving message draw() , Rectangle and Triangleobjects perform different methods

  • 8/4/2019 02_OOP_intro

    18/19

    i h c Cng ngh . HQG H N i 18Basic concepts

    OOP languages

    Some OOP features can be implemented in C or otherprocedural programming languages, but not enforced bythese languagesOOP languages: OOP concepts are embeded in andenforced by the languages.OOP languages vary in degrees of object-oriented

    Pure: Smalltalk, Eiffel, Ruby, JADE..

    Original OO plus some procedural features: Python, Java (veryhigh), C++ (mixed), C#..OO features as extension: VB.NET, Fortran 2003, PHP, Perl..

  • 8/4/2019 02_OOP_intro

    19/19