Ch. 9: Subprogram Linkage

Download Report

Transcript Ch. 9: Subprogram Linkage

ICS 313:
Programming Language Theory
Chapter 12: Object
Oriented Programming
(1)
Object Orientedness
Three essential features:
•ADTs
•Inheritance
•Polymorphism by Dynamic Binding
(2)
ADTs in OOP
Classes are Abstract Data Types:
•Syntactic encapsulation of data and
operations on the data
•Data accessible only through the defined
operations
Classes are a natural extension to the
concept of Abstract Data Types:
•Apply principle of abstraction to a
collection of related ADTs by factoring out
commonalities into superclasses
(3)
Inheritance
Motivated by software reuse:
•Need to modify the original ADT
•Need to model interrelationships and
hierarchy
Inheritance addresses these issues:
•Inherit functionality of an ADT with
selective modifications
•Inheritance hierarchy models the
application domain
(4)
Inheritance: Some Terms
Classes
Objects (class
instances)
Subclass or derived
class
Superclass or parent
class
Methods
Messages
(5)
Instance variables
Instance methods
Class variables
Class methods
Public
Private
Protected
Abstract Class
Polymorphism
Dynamic Binding of Messages to Method
Definitions
•Variables
of type
class can reference
objects of any
subclass of class
•Methods bound to
definition
dynamically
depending on the
referenced object
(6)
Class
method
ClassA
method
ClassB
method
ClassC
method
public Class var = new …;
var.method()
Computing in OOP
•Create
a hierarchical ontology of objects
in the world
•Simulate their communications and
processes
•Reuse the objects in other programs
(7)
Design Issues
(8)
Exclusivity of Objects
Purely object oriented
• All types are classes
• Everything is an object
• Elegant but slow
• SMALLTALK
Overlay an Object System
• C++: OOP added to imperative language
• CLOS: OOP added to functional language
• Large and complex languages
Primitives types are not objects
• Java
• Need wrapper classes
(9)
Wrapper Classes in Java
From a postfix evaluation application:
evalStack = new Stack();
•Putting
integers on the stack:
evalStack.push(new Integer(value));
•Retrieving
integers:
((Integer)evalStack.pop()).intValue();
(10)
What is inherited?
(Caution: confusion possible)
Interface Inheritance (ADT interface)
•Behaviors of methods (you can call them)
•Safer but less efficient
Implementation Inheritance
•Structure (you can access the data
structures directly)
Protocol Inheritance
•Only parameter profile and return type
•Java Interfaces
(11)
Single and Multiple Inheritance
Single inheritance: Smalltalk
Multiple inheritance: C++
•Goldfish extends Fish, Pet
Class
•Name collisions: which
implementation should be
used?
ClassB
•More complex dependenciesClassA
ClassC
(12)
Interfaces
Java avoids collisions with Interfaces
•Only one implementation inherited
public class Clock extends Applet
implements Runnable
•Requires Clock to implement methods of
Runnable
•Can lead to different complexities, e.g.
using ‘has-a’ as substitute for ‘is-a’
(13)
Allocation and Deallocation
Where allocated?
•Static
•Stack-dynamic
•Heap: new
•C++: all of these
•Smalltalk: Heap only
Implicit or Explicit Deallocation?
•C++: Explicit delete
•Smalltalk, Java: Implicit
(14)
Type Checking and Polymorphism
Binding of messages to methods must be
dynamic when polymorphic variables are
involved
When does type checking of this binding
happen?
•If you want strong typing, checking must
be static
-This restricts the language: overriding
methods must have matching protocol
•Dynamic type checking is more costly
(15)
Dynamic and Static Binding
Can the programmer specify that some
methods calls are statically bound?
•Java:
-Default is Dynamic
-For static, define method to be final
•C++:
-Default is Static
-Pointer of type base class may reference
derived class
-Declare function virtual for dynamic
binding
(16)
C++ Example: Static and Dynamic
class shape {
public:
virtual void draw() = 0; … } // pure virtual
class rectangle : public shape {
public:
virtual void draw() { … }
…}
class square : public rectangle {
public:
virtual void draw() { … } … }
square s;
rectangle r;
shape &refsq = s;
refsq.draw() // dynamically bound (ptr to base class)
r.draw();
// statically bound
(17)
Implementation Issues
Instance Data Storage
• Static storage: class instance records
-Instance variable addressing is constant offset
-C++
• Dynamic storage: association list or dictionary
-Python
Binding of Messages to Methods
• Static: more efficent
• Dynamic: requires pointers to methods
-Need only store once in Virtual Method Table or
class record (when classes are instances)
(18)
End of Module
(19)
Are Subclasses Subtypes
Variables of type subclass can appear
wherever variables of type parent are legal
No-brainer?
Not necessarily in C++!
(20)