Chapter 12 Categories of languages that support OOP:

Slides:



Advertisements
Similar presentations
Object-Oriented Programming Session 9 Course : T Programming Language Concept Year : February 2011.
Advertisements

Programming Languages and Paradigms
Software Productivity
Chapter 12 Support for Object-Oriented Programming.
Chapter 12: Support for Object-Oriented Programming
Object-Oriented Programming CS 3360 Spring 2012 Sec , Adapted from Addison Wesley’s lecture notes (Copyright © 2004 Pearson Addison.
ISBN Chapter 12 Support for Object-Oriented Programming.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Object-Oriented Programming
Chapter 10 Classes Continued
Abstract Data Types and Encapsulation Concepts
1 Chapter 12 © 2002 by Addison Wesley Longman, Inc Introduction - Categories of languages that support OOP: 1. OOP support is added to an existing.
ISBN Chapter 12 Support for Object- Oriented Programming.
CS 355 – PROGRAMMING LANGUAGES Dr. X. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 12 Topics Introduction Object-Oriented Programming.
Support for Object-Oriented Programming
Chapter 12 Support for Object-Oriented Programming.
CPS 506 Comparative Programming Languages
(11.1) COEN Data Abstraction and OOP  Data Abstraction – Problems with subprogram abstraction – Encapsulation – Data abstraction – Language issues.
1 Chapter 11 Abstract Data Types and Encapsulation Constructs.
ISBN Chapter 12 Support for Object-Oriented Programming.
OOPs Object oriented programming. Based on ADT principles  Representation of type and operations in a single unit  Available for other units to create.
CSC3315 (Spring 2009)1 CSC 3315 Programming Languages Hamid Harroud School of Science and Engineering, Akhawayn University
CS 403 – Programming Languages Class 25 November 28, 2000.
Introduction to Object Oriented Programming CMSC 331.
Chapter 12 Support for Object oriented Programming.
Chapter 12 Support for Object-Oriented Programming.
ISBN Chapter 12 Support for Object-Oriented Programming.
Chapter 12 Support for Object-Oriented Programming.
1 Abstract Data Types & Object Orientation Abstract Data Types (ADT) Concepts –Data Abstraction –ADT in PLs –Encapsulation Object Orientation –Principal.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Lecture 10 Concepts of Programming Languages Arne Kutzner Hanyang University / Seoul Korea.
Chapter 12 Support for Object-Oriented Programming.
Object-Oriented Programming Chapter Chapter
(1) ICS 313: Programming Language Theory Chapter 12: Object Oriented Programming.
OOPs Object oriented programming. Abstract data types  Representationof type and operations in a single unit  Available for other units to create variables.
1 Copyright © 1998 by Addison Wesley Longman, Inc. Chapter 11 Categories of languages that support OOP: 1. OOP support is added to an existing language.
ISBN Object-Oriented Programming Chapter Chapter
1 Chapter 11 © 1998 by Addison Wesley Longman, Inc The Concept of Abstraction - The concept of abstraction is fundamental in programming - Nearly.
1 CS Programming Languages Class 22 November 14, 2000.
1 Copyright © 1998 by Addison Wesley Longman, Inc. Chapter 10 Abstraction - The concept of abstraction is fundamental in programming - Nearly all programming.
C++ General Characteristics: - Mixed typing system - Constructors and destructors - Elaborate access controls to class entities.
ISBN Chapter 12 Support for Object-Oriented Programming.
Support for Object-Oriented Programming
Abstract Data Types and Encapsulation Concepts
12.1 Introduction - Categories of languages that support OOP:
Support for Object-Oriented Programming
Support for Object-Oriented Programming
Type Checking, and Scopes
Inheritance and Polymorphism
CMP 339/692 Programming Languages Day 24 Tuesday May 1, 2012
11.1 The Concept of Abstraction
Types of Programming Languages
Support for Object-Oriented Programming
Support for Object-Oriented Programming
Abstract Data Types and Encapsulation Concepts
Support for Object-Oriented Programming in Ada 95
Object-Oriented Programming
Support for Object-Oriented Programming
Support for Object-Oriented Programming
Object-Oriented Programming
CSE 452: Programming Languages
Support for Object-Oriented Programming
Support for Object-Oriented Programming
Lecture 10 Concepts of Programming Languages
11.1 The Concept of Abstraction
Chapter 11 Abstraction - The concept of abstraction is fundamental in
Presentation transcript:

Chapter 12 Categories of languages that support OOP: 1. OOP support is added to an existing language - C++ (also supports procedural and data- oriented programming) - Ada 95 (also supports procedural and data- - CLOS (also supports functional programming) - Scheme (also supports functional programming)

Chapter 11 Categories of languages that support OOP: 2. Support OOP, but have the same appearance and use the basic structure of earlier imperative languages - Eiffel (not based directly on any previous language) - Java (based on C++) 3. Pure OOP languages - Smalltalk

Paradigm Evolution 1. Procedural - 1950s-1970s (procedural abstraction) 2. Data-Oriented - early 1980s (data-oriented) 3. OOP - late 1980s (Inheritance and dynamic binding)

Origins of Inheritance Observations of the mid-late 1980s : - Productivity increases can come from reuse Unfortunately, - ADTs are difficult to reuse--never quite right - All ADTs are independent and at the same level Inheritance solves both--reuse ADTs after minor changes and define classes in a hierarchy

OOP Definitions: - ADTs are called classes - Class instances are called objects - A class that inherits is a derived class or a subclass - The class from which another class inherits is a parent class or superclass - Subprograms that define operations on objects are called methods

Continue OOP Definitions: - The entire collection of methods of an object is called its message protocol or message interface - Messages have two parts--a method name and the destination object - In the simplest case, a class inherits all of the entities of its parent

controls to encapsulated entities - Inheritance can be complicated by access controls to encapsulated entities - A class can hide entities from its subclasses ( protected in C++ for unhiding ) - A class can hide entities from its clients - Besides inheriting methods as is, a class can modify an inherited method - The new one overrides the inherited one - The method in the parent is overriden

- There are two kinds of variables in a class: 1. Class variables - one/class 2. Instance variables - one/object - There are two kinds of methods in a class: 1. Class methods - messages to the class 2. Instance methods - messages to objects - Single vs. Multiple Inheritance

- One disadvantage of inheritance for reuse: - Creates interdependencies among classes that complicate maintenance

Polymorphism in OOPLs - A polymorphic variable can be defined in a class that is able to reference (or point to) objects of the class and objects of any of its descendants - When a class hierarchy includes classes that override methods and such methods are called through a polymorphic variable, the binding to the correct method MUST be dynamic - This polymorphism simplifies the addition of new methods - A virtual method is one that does not include a definition (it only defines a protocol)

- A virtual class is one that includes at least one virtual method - A virtual class cannot be instantiated

Design Issues for OOPLs 1. The Exclusivity of Objects a. Everything is an object Advantage - elegance and purity Disadvantage - slow operations on simple objects (e.g., float) b. Add objects to a complete typing system Advantage - fast operations on simple objects Disadvantage - results in a confusing type system c. Include an imperative-style typing system for primitives but make everything else objects and a relatively small typing system Disadvantage - still some confusion because of the two type systems

Design Issues for OOPLs 2. Are Subclasses Subtypes? - Does an is-a relationship hold between a parent class object and an object of the subclass? An “is-a” relationship would guarantee that a variable of the parent class type was legal.

3. Implementation and Interface Inheritance - If only the interface of the parent class is visible to the subclass, it is interface inheritance Disadvantage - can result in inefficiencies - If both the interface and the implementation of the parent class is visible to the subclass, it is implementation inheritance Disadvantage - changes to the parent class require recompilation of subclasses, and sometimes even modification of subclasses

4. Type Checking and Polymorphism - Polymorphism may require dynamic type checking of parameters and the return value - Dynamic type checking is costly and delays error detection - If overriding methods are restricted to having the same parameter types and return type, the checking can be static

5. Single and Multiple Inheritance - Disadvantages of multiple inheritance: - Language and implementation complexity - Potential inefficiency - dynamic binding costs more with multiple inheritance (but not much) - Advantage: - Sometimes it is extremely convenient and valuable

6. Allocation and Deallocation of Objects - From where are objects allocated? - If they all live in the heap, references to them are uniform - Is deallocation explicit or implicit?

7. Dynamic and Static Binding - Should ALL binding of messages to methods be dynamic? - If none are, you lose the advantages of dynamic binding - If all are, it is inefficient

Overview of Smalltalk - Smalltalk is a pure OOP language - Everything is an object - All computation is through objects sending messages to objects - It adopts none of the appearance of imperative languages - The Smalltalk Environment - The first complete GUI system - A complete system for software development - All of the system source code is available to the user, who can modify it if he/she wants

Introduction to Smalltalk - Expressions - Four kinds: 1. Literals (numbers, strings, and keywords) 2. Variable names (all variables are references) 3. Message expressions (see below) 4. Block expressions (see below)

- Message expressions - Two parts: the receiver object and the message itself - The message part specifies the method and possibly some parameters - Replies to messages are objects

- Messages can be of three forms: 1. Unary (no parameters) e.g., myAngle sin (sends a message to the sin method of the myAngle object) 2. Binary (one parameter, an object) e.g., 12 + 17 (sends the message “+ 17” to the object 12; the object parameter is “17” and the method is “+”) 3. Keyword (use keywords to organize the parameters) e.g., myArray at: 1 put: 5 (sends the objects “1” and “5” to the at:put: method of the object myArray)

- Multiple messages to the same object can be strung together, separated by semicolons

Methods - General form: message_pattern [| temps |] statements - A message pattern is like the formal parameters of a subprogram - For a unary message, it is just the name - For others, it lists keywords and formal names - temps are just names--Smalltalk is typeless!

Assignments - Simplest Form: name1 <- name2 - It is simply a pointer assignment - RHS can be a message expression e.g., index <- index + 1

Blocks - A sequence of statements, separated by periods, delimited by brackets e.g., [index <- index + 1. sum <- sum + index] - A block specifies something, but doesn’t do it - To request the execution of a block, send it the unary message, value e.g., […] value - Blocks can have parameters, as in [:x :y | statements] - If a block contains a relational expression, it returns a Boolean object, true or false

Iteration - The objects true and false have methods for building control constructs - The method WhileTrue: from Block is used for pretest logical loops. It is defined for all blocks that return Boolean objects. e.g., [count <= 20] whileTrue [sum <- sum + count. count <- count + 1]

- timesRepeat: is defined for integers and can be used to build counting loops e.g., xCube <- 1.3 timesRepeat: [xCube <- xCube * x]

Selection - The Boolean objects have the method ifTrue:ifFalse: , which can be used to build selection e.g., total = 0 ifTrue: […] ifFalse: […]

Large-Scale Features of Smalltalk - Type Checking and Polymorphism - All bindings of messages to methods is dynamic - The process is to search the object to which the message is sent for the method; if not found, search the superclass, etc. - Because all variables are typeless, methods are all polymorphic

- Inheritance - All subclasses are subtypes (nothing can be hidden) - All inheritance is implementation inheritance - No multiple inheritance - Methods can be redefined, but the two are not related

C++ - General Characteristics: - Mixed typing system - Constructors and destructors - Elaborate access controls to class entities

- Inheritance - A class need not be subclasses of any class - Access controls for members are 1. Private (visible only in the class and friends) (disallows subclasses from being subtypes) 2. Public (visible in subclasses and clients)

b. Public derivation public and protected - Inheritance 3. Protected (visible in the class and in subclasses, but not clients) - In addition, the subclassing process can be declared with access controls (private or public), which define potential changes in access by subclasses a. Private derivation - inherited public and protected members are private in the subclasses b. Public derivation public and protected members are also public and protected in

Example (book, p. 468)

class base_class { private: int a; float x; protected: int b; float y; public: int c; float z; }; class subclass_1 : public base_class { … }; // - In this one, b and y are protected and // c and z are public class subclass_2 : private base_class { … }; // - In this one, b, y, c, and z are private, // and no derived class has access to any // member of base_class

- Reexportation A member that is not accessible in a subclass (because of private derivation) can be declared to be visible there using the scope resolution operator (::) e.g., class subclass_3 : private base_class { base_class :: c; … }

One motivation for using private derivation: - A class provides members that must be visible, so they are defined to be public members; a derived class adds some new members, but does not want its clients to see the members of the parent class, even though they had to be public in the parent class definition - Multiple inheritance is supported - If there are two inherited members with the same name, they can both be reference using the scope resolution operator

- Dynamic Binding - A method can be defined to be virtual, which means that they can be called through polymorphic variables and dynamically bound to messages - A pure virtual function has no definition at all - A class that has at least one pure virtual function is an abstract class

- Evaluation - C++ provides extensive access control (unlike Smalltalk) - C++ provides multiple inheritance - In C++, the programmer must decide at design time which methods will be statically bound and which must be dynamically bound - Static binding is faster! - Smalltalk type checking is dynamic (flexible, but somewhat unsafe)

Java - General Characteristics - All data are objects except the primitive types - All primitive types have wrapper classes that store one data value - All objects are heap-dynamic, are referenced through reference variables, and most are allocated with new

- Inheritance - Single inheritance only, but there is an abstract class category that provides some of the benefits of multiple inheritance (interface) - An interface can include only method declarations and named constants e.g., public class Clock extends Applet implements Runnable - Methods can be final (cannot be overriden)

- In Java, all messages are dynamically bound to - Dynamic Binding - In Java, all messages are dynamically bound to methods, unless the method is final (mean it cannot be overriden; therefore, dynamic binding serves no purpose)

- Encapsulation - Two constructs, classes and packages - Packages provide a container for classes that are related (can be named or unamed) - Entities defined without an scope (access) modifier have package scope, which makes them visible throughout the package in which they are defined - they go in the unnamed package - Every class in a package is a friend to the package scope entities elsewhere in the package - So, package scope is an alternative to the friends of C++

Ada 95 - General Characteristics - OOP was one of the most important extensions to Ada 83 - Encapsulation container is a package that defines a tagged type - A tagged type is one in which every object includes a tag to indicate during execution its type - Tagged types can be either private types or records - No constructors or destructors are implicitly called

- Inheritance - Subclasses are derived from tagged types - New entities in a subclass are added in a record

Example (of a tagged type)

Package PERSON_PKG is type PERSON is tagged private; procedure DISPLAY(P : in out PERSON); private type PERSON is tagged record NAME : STRING(1..30); ADDRESS : STRING(1..30); AGE : INTEGER; end record; end PERSON_PKG;

with PERSON_PKG; use PERSON_PKG; package STUDENT_PKG is type STUDENT is new PERSON with record GRADE_POINT_AVERAGE : FLOAT; GRADE_LEVEL : INTEGER; end record; procedure DISPLAY (ST: in STUDENT); end STUDENT_PKG; - DISPLAY is being overriden from PERSON_PKG

- Inheritance (more) - All subclasses are subtypes - Single inheritance only, except through generics - Dynamic Binding - Dynamic binding is done using polymorphic variables called classwide types e.g., for the tagged type PERSON, the classwide type is PERSON’class - Other bindings are static - Any method may be dynamically bound

Eiffel - General Characteristics - Has primitive types and objects - All objects get three operations, copy, clone, and equal - Methods are called routines - Instance variables are called attributes - The routines and attributes of a class are together called its features

Eiffel - Object creation is done with an operator (!!) - Constructors are defined in a creation clause, and are explicitly called in the statement in which an object is created - Inheritance - The parent of a class is specified with the inherit clause

- feature clauses specify access control to the entities defined in them - Without a modifier, the entities in a feature clause are visible to both subclasses and clients - With the name of the class as a modifier, entities are hidden from clients but are visible to subclasses - With the none modifier, entities are hidden from both clients and subclasses

- Inherited features can be hidden from subclasses with undefine - Abstract classes can be defined by including the deferred modifier on the class definition - Dynamic Binding - Nearly all message binding is dynamic - An overriding method must have parameters that are assignment compatible with those of the overriden method - All overriding features must be defined in a redefine clause - Access to overriden features is possible by putting their names in a rename clause

- Evaluation - Similar to Java in that procedural programming is not supported and nearly all message binding is dynamic - Elegant and clean design of support for OOP

Implementing OO Constructs - Class instance records (CIRs) store the state of an object - If a class has a parent, the subclass instance variables are added to the parent CIR - Virtual Method Tables (VMTs) are used for dynamic binding

Review Questions solutions Q1: What are the three characteristic features of object-oriented languages? 1- Abstract Data Types 2- Inheritance 3- Polymorphism and dynamic binding

Q2: What is the difference between a class variable and an instance variable ? Class variable belong to the class rather than to the instant. Instance variable is the instance of the class ( the state of the class instance variables )

Q3: What is an overriding method ? In inheritance, the base and the derived class may have the same method ( the same name). The new method is said to override the inherited version, which is then called an overriden method

Q4: Describe a situation where dynamic binding is a great advantage over its absence ? Suppose you have a number of objects of different classes but you want to put them all on a list and perform a particular operation on them using the same function call. For example, suppose a graphics program includes several different shapes: a triangle, a ball, a square, and so on. Each of these classes has a member function draw ( ) that causes the object to be drawn on the screen.

Q5: What is a virtual method ? It is a method that does not really exit but nevertheless appears real to some parts of the program. Or The method that exists in the base class without the body ( just the protocol ). Once it is declared a virtual, it remains virtual all the way down the inheritance hierarchy from that point even if it is not declared virtual when a class overrides it.

Q6: Describe briefly the seven design issues used in this chapter for object-oriented languages ? 1- The exclusivity of objects 2- Are subclasses Subtypes 3- Implementation and interference inheritance 4- Type checking and polymorphism 5- Single and multiple inheritance 6- Allocation and deallocation of objects 7- Dynamic and static binding

Q7: What is the message protocol for a method ? I think the author did an error here. He mean the message protocol for a class. This mean the entire collection of methods of an object is called. However, the question can be solved by saying: It is the method that exists in the base class without the body

Q8 : Why is that the classes of Smalltalk can respond to messages ? Because all classes in Smalltalk are themselves objects. This allow class to receive messages

Q9: Explain the actions of the Smalltalk statement results <--- first * second first is an object. This object is has the operation * applied on it. The passing parameter is second. The variable results is set to refer to the multiplication of objects: first and second.

Q10: What are the 4 parts of a smalltalk class definition ? 1- The class name 2- The superclass name 3- Declarations of the instance variables 4- Declarations of the instance and class methods

- The message has two parts: the receiver object Q11: Explain how smalltalk messages are bound to methods. When does this take place? - The message has two parts: the receiver object and the message itself - The message part specifies the method and possibly some parameters - Replies to messages are objects - Method general form is: message_pattern [| temps |] statements - A message pattern is like the formal parameters of a subprogram - temps are just names--Smalltalk is typeless! - All the computation are accomplished through messages. Binding of messages to methods is dynamic

It operate as follows: A message to an object causes the class to which the object belongs to be searched for a corresponding method. If the search fails, it is continued in the superclass of that class, and so forth, up to the system class, Object, which has no supercalss. Object is the root of the class derivation tree on which every class is a node. If no method is found anywhere in that chain, an error occurs

Q12: What type checking is done in smalltalk. When does it take place. Dynamic. It takes place when the message is sent. Smalltalk does not, under any circumstances, bind messages to methods statically.

Q13: What kind of inheritance does smalltalk support? Single inheritance, it does not allow multiple inheritance.

Q14: What are the two primary effects that smalltalk has had on computing ? 1- The integrated use of windows, mouse-pointing devices, and pop-up or pull-down menus. 2- The advancement of object-oriented programming

Q15: What purpose does the Smalltalk pseudovariable super serve ? It causes the method search to begin in the superclass rather than locally.

Q16: In essence, all Smalltalk variables are of a single type. What is that type ? All Smalltalk variables are references; they can only refer to objects or classes. They are typeless; any variable can point to any object.

Q17: How many parameters are there in a Smalltalk binary message ? One parameter. It is object.

Q18: Explain the precedence rules of Smalltalk expressions ? Unary operators, followed by binary expression, followed by keyword expressions. Both unary and binary expressions are associate left to right. Expressions can be parenthesized to force any order of operator evaluation.

To request the execution of a block, send it the unary message, value Q19: How one can force a Smalltalk block to be executed ? To request the execution of a block, send it the unary message, value e.g., [sum <- sume + index] value

Q20: What purpose does the Smalltalk pseudovariable self serve ? It is an object name that refers to the object in which it appears. Therefore, self is used for recursive messages, or messages to the object itself.

Q21: From where can C++ objects be allocated ? Either stack-dynamically or heap.

Q22: How are C++ heap-allocated objected deallocated ? By the destructor.

Q23: Are all C++ subclasses subtypes ? No, not necessary.

Q24: Under what circumstances is a C++ method call statically bound to a method ? If it is not a virtual method.

Q25: What drawback is there to allow designers to specify which method can be statically bound? The original design must include these decision, which may have to be changed later.

Q26: Explain the differences between the two uses of private in C++ ? Private is visible only in the class and friends. It disallows subclasses from being subtypes. The differences is between the subtypes and the derived types that are not subtypes. This is done by using private access. The complete example is in text page 470.

Q27: What is a friend function in C++ ? A function that is granted access to the private data.

Q28: How is the type system of Java different from that of C++ ? Java has no enumeration or record types, and its arrays are objects. Only values of primitive scalar types(Boolean, char, and numeric types) are not objects.

Q29: From where can Java objects be allocated ? From the heap.

Q30 : How Java objects are deallocated ? There is no explicit object deallocation operation. The garbage collector takes care of this.

Q31: Are all Java subclasses subtypes ?

Q32: Under what circumstances is a Java method call statically bound to a method ? When it is final.

Q33: Are all Ada 95 subclasses subtypes ? Yes

Dynamic binding is done using polymorphic Q34: How is a call to a subprogram in Ada 95 specified to be dynamically bound to a subprogram definition ? When is this decision made ? Dynamic binding is done using polymorphic variables called classwide types. This is done dynamically.

Q35 : What purpose does a creation clause have in Eiffel ? It is a constructor that is called when objects are created.

With the name of the class as a modifier. Look Q36: How is an Eiffel feature defined to be visible in subclasses but not clients With the name of the class as a modifier. Look for the 3 cases in the text page 482.

Q37: What does the { none } qualifier do when it appear on an Eiffel feature ? The details are hidden from both the clients and the subclasses.

Q38: How are Eiffel heap-allocated objects deallocated? There is no explicit deallocation operation. The garbage collection process reclaims its storage

Q39 : Under what circumstances is an Eiffel method call statically bound to a method ?

Q40: How can an Eiffel subclass be defined not to be a subtype ?