MELJUN CORTES Programming Languages Abstraction

download MELJUN CORTES Programming Languages Abstraction

of 8

Transcript of MELJUN CORTES Programming Languages Abstraction

  • 8/8/2019 MELJUN CORTES Programming Languages Abstraction

    1/18

    Abstraction

    Programming Languages

    * Property of STI 

    Page 1 of 18

     The Concept of

     Abstraction

     Abstraction is a view or representation ofan entity that includes only the most

    significant attributes.

     The concept of abstraction is fundamentalin programming (and computer science)

    Nearly all programming languages supportprocess abstraction

     with subprograms

    Nearly all programming languagesdesigned since 1980 have supported

    data

    abstraction

     with some kind of module

    http: / /www.cs.unb.ca/courses 

    http://www.cs.unb.ca/courseshttp://www.cs.unb.ca/courses

  • 8/8/2019 MELJUN CORTES Programming Languages Abstraction

    2/18

    Abstraction

    Programming Languages

    * Property of STI 

    Page 2 of 18

     Abstraction

     Abstraction is a weapon against thecomplexity of programming, it simplify the

    programming.

     Two fundamental kinds:

    Process bstraction

    Data bstraction

    Floating-point as an Abstract Data Type

    User-defined Abstract Data Type musthave the same characteristics as built-inabstract data types

    http: / /www.cs.unb.ca/courses 

    http://www.cs.unb.ca/courseshttp://www.cs.unb.ca/courses

  • 8/8/2019 MELJUN CORTES Programming Languages Abstraction

    3/18

    Abstraction

    Programming Languages

    * Property of STI 

    Page 3 of 18

    Introduction to Data

     Abstraction

     An abstract data type is a user-defineddata type that satisfies the following

    conditions:

     The representation of, and operations on,objects of the type are defined in a singlesyntactic unit; also, other units can createobjects of the type.

     The representation of objects of the type ishidden from the program units that usethese objects, so the only operationspossible are those provided in the type'sdefinition.

    http: / /www.cs.unb.ca/courses 

    http://www.cs.unb.ca/courseshttp://www.cs.unb.ca/courses

  • 8/8/2019 MELJUN CORTES Programming Languages Abstraction

    4/18

    Abstraction

    Programming Languages

    * Property of STI 

    Page 4 of 18

     Advantages of Data

     Abstraction

     Advantage of the first condition:

    Program organization, modifiability

    (everything associated with a data structureis together), and separate compilation.

     Advantage of the second condition:

    Reliability--by hiding the datarepresentations, user code cannot directly

    access objects of the type. User code cannotdepend on the representation, allowing therepresentation to be changed withoutaffecting user code.

    http: / /www.cs.unb.ca/courses 

    http://www.cs.unb.ca/courseshttp://www.cs.unb.ca/courses

  • 8/8/2019 MELJUN CORTES Programming Languages Abstraction

    5/18

    Abstraction

    Programming Languages

    * Property of STI 

    Page 5 of 18

    Classes  An example of a class – a class

    implementation of a complex number realplus the imaginary counterpart 

    class complex {

    double re, im;};

     public:

    complex& operator++ (complex a) {

    re+= a.re;

    im+= a.im;

    return *this;

    }

    complex& operator+= (double a) {

    re+= a;

    return *this;

    }

    complex operator+ (complex a, complex b) {

    complex r= a;

    complex r+= b;

    }

    complex operator+ (complex a, complex b) {

    complex r= a;

    return r+= b;

    }

    complex operator (double a, complex b) {

    complex r= b;

    return+= a;

    }

  • 8/8/2019 MELJUN CORTES Programming Languages Abstraction

    6/18

    Abstraction

    Programming Languages

    * Property of STI 

    Page 6 of 18

    Encapsulation

    Large programs have two special needs:

    Some means of organization, other than

    simply division into subprograms Some means of partial compilation

    (compilation units that are smaller than the whole program)

    Obvious solution : a grouping of

    subprograms that are logically related intoa unit that can be separately compiled

     These are called en psul tions

    http: / /www.cs.unb.ca/courses 

    http://www.cs.unb.ca/courseshttp://www.cs.unb.ca/courses

  • 8/8/2019 MELJUN CORTES Programming Languages Abstraction

    7/18

    Abstraction

    Programming Languages

    * Property of STI 

    Page 7 of 18

    Information Hiding

     The principle of information hiding is thehiding of design decisions in a computer

    program that are most likely to change,thus protecting other parts of the programfrom change if the design decision ischanged.

  • 8/8/2019 MELJUN CORTES Programming Languages Abstraction

    8/18

    Abstraction

    Programming Languages

    * Property of STI 

    Page 8 of 18

    Language Examples

     Abstract Data Type in Ada 

     The encapsulation constructs are called

    packages• Specification package (the interface)

    • Body package (implementation of the entitiesnamed in the specification)

    Information Hiding

    •  The specification package has two parts:public and private

    •  The name of the abstract type appears in thepublic part of the specification package

    •  The representation of type appears in a part ofthe specification called the private part 

     –  More restricted form with limited privatetypes 

    • Reasons for the public/private specification

    package: –   The compiler should be able to see the

    representation after seeing only thespecification package (it cannot see theprivate part)

     –  Clients should see the type name, butnot the representation (also cannot seethe private part)

    http: / /www.cs.unb.ca/courses 

    http://www.cs.unb.ca/courseshttp://www.cs.unb.ca/courses

  • 8/8/2019 MELJUN CORTES Programming Languages Abstraction

    9/18

    Abstraction

    Programming Languages

    * Property of STI 

    Page 9 of 18

    Language Examples

     Abstract Data Type in C++

    Based on C struct type and Simula 67

    classes  The class is the encapsulation device

     All of the class instances of a class share asingle copy of the member functions

    Each instance of a class has its own copy ofthe class data members

    Instances can be static, stack dynamic, orheap dynamic

    Information Hiding

    • Private clause for hidden entities

    • Public clause for interface entities

    • Protected clause for inheritance

    http: / /www.cs.unb.ca/courses 

    http://www.cs.unb.ca/courseshttp://www.cs.unb.ca/courses

  • 8/8/2019 MELJUN CORTES Programming Languages Abstraction

    10/18

    Abstraction

    Programming Languages

    * Property of STI 

    Page 10 of 18

    Language Examples

     Abstract Data Type in C++

    Constructors

    • Functions to initialize the data members ofinstances (they DO NOT create the objects)

    • May also allocate storage if part of the objectis heap-dynamic

    • Can include parameters to provideparameterization of the objects

    • Implicitly called when an instance is created

    • Can be explicitly called• Name is the same as the class name

    Destructors

    • Functions to cleanup after an instance isdestroyed; usually just to reclaim heap storage

    • Implicitly called when the object’s lifetime

    ends• Can be explicitly called

    • Name is the class name, preceded by a tilde (~)

    http: / /www.cs.unb.ca/courses 

    http://www.cs.unb.ca/courseshttp://www.cs.unb.ca/courses

  • 8/8/2019 MELJUN CORTES Programming Languages Abstraction

    11/18

    Abstraction

    Programming Languages

    * Property of STI 

    Page 11 of 18

    Language Examples

     Abstract Data Type in Java 

    Similar to C++, except:

    •  All user-defined types are classes•  All objects are allocated from the heap and

    accessed through reference variables

    • Individual entities in classes have accesscontrol modifiers (private or public), ratherthan clauses

    •  Java has a second scoping mechanism,

    package scope, which can be used in place offriends

     –   All entities in all classes in a packagethat do not have access control modifiersare visible throughout the package

    http: / /www.cs.unb.ca/courses 

    http://www.cs.unb.ca/courseshttp://www.cs.unb.ca/courses

  • 8/8/2019 MELJUN CORTES Programming Languages Abstraction

    12/18

    Abstraction

    Programming Languages

    * Property of STI 

    Page 12 of 18

    Language Examples

     Abstract Data Type in C#

    Based on C++ and Java 

     Adds two access modifiers: internal and protected internal

     All class instances are heap dynamic

    Default constructors are available for allclasses

    Garbage collection is used for most heap

    objects, so destructors are rarely used structs are lightweight classes that do not

    support inheritance

    Common solution to need for access to datamembers: accessor methods (getter andsetter)

    C# provides properties as a way ofimplementing getters and setters withoutrequiring explicit method calls

    http: / /www.cs.unb.ca/courses 

    http://www.cs.unb.ca/courseshttp://www.cs.unb.ca/courses

  • 8/8/2019 MELJUN CORTES Programming Languages Abstraction

    13/18

    Abstraction

    Programming Languages

    * Property of STI 

    Page 13 of 18

    Get /Set Methods

    Getters are methods that return the valueof the field. Prefix the name of any method

    that returns a value with ‘get’. If the getter is a Boolean field, prefix it with

    ‘is’.

    Example:

    • getFirstname();

    • GetAccountNumber();

    • isPersistent();

    • isAtEnd();

    Setters are method that modify the value ofa field.

    Prefix with ‘set’, regardless of the field type. Example:

    • setFirstName(String aName)

    • setAccountNumber(int anAccountNumber)

    • setReasonableGoals(Vector newGoals)

    • setPersistent(boolean isPersistent)

    • setAtEnd(boolean isAtEnd)

  • 8/8/2019 MELJUN CORTES Programming Languages Abstraction

    14/18

    Abstraction

    Programming Languages

    * Property of STI 

    Page 14 of 18

    Constructors

    Functions to initialize data members ofinstances (they DO NOT create object)

    May also allocate storage if part of theobject is heap-dynamic

    Can include parameters to provide

    parameterization of the objects

    Implicitly called when an instance iscreated

    Can be explicitly called

    Name is the same as the class name

    http: / /www.cs.unb.ca/courses 

    http://www.cs.unb.ca/courseshttp://www.cs.unb.ca/courses

  • 8/8/2019 MELJUN CORTES Programming Languages Abstraction

    15/18

    Abstraction

    Programming Languages

    * Property of STI 

    Page 15 of 18

    Destructors

    Functions to clean up after an instance isdestroyed; usually just to reclaim heap

    storage

    Implicitly called when the object’s lifetimeends

    Can be explicitly called

    Name is the class name, preceded by atilde (~)

    http: / /www.cs.unb.ca/courses 

    http://www.cs.unb.ca/courseshttp://www.cs.unb.ca/courses

  • 8/8/2019 MELJUN CORTES Programming Languages Abstraction

    16/18

    Abstraction

    Programming Languages

    * Property of STI 

    Page 16 of 18

    Modules

    C/C++ Header Files

    C and C++ provide a primitive means of

    module abstraction. Header files are separate group of files

     wherein certain subprogram definitions areobtained.

    • conio.h – console input and output library 

    • stdio.h – standard input/output library 

    Users may create their own header files

    Usually named after the use they areintended for such as:

    vga.h –  VGA graphics driver header 

     mouse.h – mouse driver routine header 

  • 8/8/2019 MELJUN CORTES Programming Languages Abstraction

    17/18

    Abstraction

    Programming Languages

    * Property of STI 

    Page 17 of 18

    Modules

    Packages in Ada 

    Grouping of subprograms

    Group together declarations of related variables, constants, and types

    Create encapsulated or abstract data typesfor which the name and the applicableoperations are declared

     Valuable for programming design and

    implementation of large systems software

    http: / /www.cs.unb.ca/courses 

    http://www.cs.unb.ca/courseshttp://www.cs.unb.ca/courses

  • 8/8/2019 MELJUN CORTES Programming Languages Abstraction

    18/18

    Abstraction

    Programming Languages

    * Property of STI 

    Page 18 of 18

    Modules

    Packages in Java 

     Java packages are mechanisms for

    organizing Java classes into namespaces.•  These can be stored in compressed files called

     JAR files

    In a Java source file, the package that thisfile's class or classes belong to is specifiedusing the package keyword, which is

    usually the first keyword in source file.

     To use the classes of a package inside a Java source file, import the classes from thepackage using the import keyword.