isbn 0- 321-49362-1 object-oriented programming chapter 11.1-11.3 chapter 12.1-12.3

24

Click here to load reader

Upload: karin-mitchell

Post on 18-Jan-2016

229 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: ISBN 0- 321-49362-1 Object-Oriented Programming Chapter 11.1-11.3 Chapter 12.1-12.3

ISBN 0-321-49362-1

Object-Oriented Programming

Chapter 11.1-11.3Chapter 12.1-12.3

Page 2: ISBN 0- 321-49362-1 Object-Oriented Programming Chapter 11.1-11.3 Chapter 12.1-12.3

Copyright © 2007 Addison-Wesley. All rights reserved.1–2

The Concept of Abstraction

• Abstraction is representation of entity that includes only the most significant attributes– Abstraction is fundamental in programming

(and computer science)

• Two types of abstraction – process abstraction using subprograms– data abstraction using classes

Page 3: ISBN 0- 321-49362-1 Object-Oriented Programming Chapter 11.1-11.3 Chapter 12.1-12.3

Copyright © 2007 Addison-Wesley. All rights reserved.1–3

Introduction to Data Abstraction

• An abstract data type is a user-defined data type that satisfies the following two conditions:– The representation of, and operations on,

objects of the type are defined in a single syntactic unit

– The representation of objects of the type is hidden from the program units that use these objects

Page 4: ISBN 0- 321-49362-1 Object-Oriented Programming Chapter 11.1-11.3 Chapter 12.1-12.3

Copyright © 2007 Addison-Wesley. All rights reserved.1–4

Object-Oriented Terms

• ADTs for OOP are called classes• Class instances are called objects• Subprograms that define operations on

objects are called methods• Calls to methods are called messages

Page 5: ISBN 0- 321-49362-1 Object-Oriented Programming Chapter 11.1-11.3 Chapter 12.1-12.3

Copyright © 2007 Addison-Wesley. All rights reserved.1–5

Members of Classes

• Two categories of members in a class– Class members belong to the class as a whole– Instance members relate to a specific object

• There are two kinds of variables in a class:– Class variables - one/class– Instance variables - one/object

• There are two kinds of methods in a class:– Class methods – accept messages to the class– Instance methods – accept messages to

objects

Page 6: ISBN 0- 321-49362-1 Object-Oriented Programming Chapter 11.1-11.3 Chapter 12.1-12.3

Copyright © 2007 Addison-Wesley. All rights reserved.1–6

Information Access

• Possible choices– A class can hide entities from its subclasses– A class can hide entities from its clients– A class can also hide entities for its clients

while allowing its subclasses to see them

• Typical access modifiers– private for hidden entities– public for interface entities– protected for inheritance

Page 7: ISBN 0- 321-49362-1 Object-Oriented Programming Chapter 11.1-11.3 Chapter 12.1-12.3

Copyright © 2007 Addison-Wesley. All rights reserved.1–7

Approaches to Objects

• Everything is an object

• Add objects to a complete typing system

• Include an imperative-style typing system for primitives but make everything else objects

Page 8: ISBN 0- 321-49362-1 Object-Oriented Programming Chapter 11.1-11.3 Chapter 12.1-12.3

Copyright © 2007 Addison-Wesley. All rights reserved.1–8

Inheritance

• Productivity increases can come from reuse– ADTs are difficult to reuse without modification– All ADTs are independent and at the same

level

• Inheritance allows new classes to be defined in terms of existing ones– New classes inherit common parts– A class inherits all of the entities of its parent

Page 9: ISBN 0- 321-49362-1 Object-Oriented Programming Chapter 11.1-11.3 Chapter 12.1-12.3

Copyright © 2007 Addison-Wesley. All rights reserved.1–9

Inheritance Terms

• A class that inherits is a child class or a derived class or a subclass

• The class from which another class inherits is a parent class or superclass or sometimes a base class

• A class can modify an inherited method– The subclass definition overrides the inherited

one

Page 10: ISBN 0- 321-49362-1 Object-Oriented Programming Chapter 11.1-11.3 Chapter 12.1-12.3

Copyright © 2007 Addison-Wesley. All rights reserved.1–11

Dynamic Binding

• A polymorphic variable can reference objects of the class it belongs to and any of its subclasses

• For overridden methods called through a polymorphic variable, the binding to the correct method will occur at run time

• Polymorphism may require dynamic type checking of parameters return values

Page 11: ISBN 0- 321-49362-1 Object-Oriented Programming Chapter 11.1-11.3 Chapter 12.1-12.3

Copyright © 2007 Addison-Wesley. All rights reserved.1–12

Object Allocation and Deallocation

• Object allocation can be– Static, stack dynamic or heap dynamic– Explicit or automatic

• If all heap-dynamic, references can be uniform– Simplifies assignment - dereferencing can be

implicit

• If objects are stack dynamic, there is a problem with regard to subtypes

• Is deallocation explicit or implicit?– Garbage collector does implicit deallocation

Page 12: ISBN 0- 321-49362-1 Object-Oriented Programming Chapter 11.1-11.3 Chapter 12.1-12.3

Copyright © 2007 Addison-Wesley. All rights reserved.1–13

Support for OOP in Java

• Type system consists of primitives and objects

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

and accessed through reference variables– Objects are allocated with new

• A finalize method is called implicitly by garbage collector

Page 13: ISBN 0- 321-49362-1 Object-Oriented Programming Chapter 11.1-11.3 Chapter 12.1-12.3

Copyright © 2007 Addison-Wesley. All rights reserved.1–14

Support for OOP in Java

• Inheritance– Single inheritance only– Interfaces provide some of the benefits of

multiple inheritance

• Dynamic Binding– All messages are dynamically bound to

methods unless • the method is final• the methods is static or private both of which

disallow overriding

Page 14: ISBN 0- 321-49362-1 Object-Oriented Programming Chapter 11.1-11.3 Chapter 12.1-12.3

Copyright © 2007 Addison-Wesley. All rights reserved.1–15

Support for OOP in C++

• Mixed typing system• Constructors and destructors• Elaborate access controls to class entities• Inheritance

– A class need not be the subclass of any class– Multiple inheritance allowed– Public or private derivation

• A virtual method can be called through polymorphic variables and dynamically bound to messages

Page 15: ISBN 0- 321-49362-1 Object-Oriented Programming Chapter 11.1-11.3 Chapter 12.1-12.3

Copyright © 2007 Addison-Wesley. All rights reserved.1–16

Encapsulation in C++

• Use classes for encapsulation• All of the instances of a class share a

single copy of the member functions• Each instance of a class has its own copy

of the data members• Instances can be static, stack dynamic, or

heap dynamic

Page 16: ISBN 0- 321-49362-1 Object-Oriented Programming Chapter 11.1-11.3 Chapter 12.1-12.3

Copyright © 2007 Addison-Wesley. All rights reserved.1–17

C++ Allocation/Deallocation

• Constructors initialize the data members of instances (they do not create the objects)– May also allocate storage if part of the

object is heap-dynamic– Name is the same as the class name

• Destructors– Cleanup after an instance is destroyed

(reclaim heap storage)– Name is the class name, preceded by a tilde

(~)

Page 17: ISBN 0- 321-49362-1 Object-Oriented Programming Chapter 11.1-11.3 Chapter 12.1-12.3

Copyright © 2007 Addison-Wesley. All rights reserved.1–18

Support for OOP in C#

• All class instances are heap dynamic• structs are lightweight classes that do

not support inheritance• Default constructors are available for all

classes• Garbage collection is used for most

heap objects, so destructors are rarely used

• Inheritance– A method inherited from parent class can be

replaced in the derived class by marking its definition with new

Page 18: ISBN 0- 321-49362-1 Object-Oriented Programming Chapter 11.1-11.3 Chapter 12.1-12.3

Copyright © 2007 Addison-Wesley. All rights reserved.1–19

Support for OOP in C#

• Dynamic binding– To allow dynamic binding of method calls to

methods:• The base class method is marked virtual• The corresponding methods in derived classes are

marked override– Abstract methods are marked abstract and

must be implemented in all subclasses

• Nested Classes– A C# class that is directly nested in a nesting

class behaves like a Java static nested class

Page 19: ISBN 0- 321-49362-1 Object-Oriented Programming Chapter 11.1-11.3 Chapter 12.1-12.3

Copyright © 2007 Addison-Wesley. All rights reserved.1–20

Support for OOP in Ruby

• Everything is an object• Ruby class is an object of type class

– contain flags, instance variables, methods– default visibility for methods is public – all instance data is private, attributes provide

access

• All variables are references to objects• All variables are polymorphic• Classes are executable and dynamic

– they can be modified while program is running

• Single inheritance plus mixin modules

Page 20: ISBN 0- 321-49362-1 Object-Oriented Programming Chapter 11.1-11.3 Chapter 12.1-12.3

Copyright © 2007 Addison-Wesley. All rights reserved.1–21

Implementing OO Constructs

• Two interesting and challenging parts– Storage structures for instance variables– Dynamic binding of messages to methods

Page 21: ISBN 0- 321-49362-1 Object-Oriented Programming Chapter 11.1-11.3 Chapter 12.1-12.3

Copyright © 2007 Addison-Wesley. All rights reserved.1–22

Instance Data Storage

• Class instance records (CIRs) store the state of an object– Static (built at compile time)

• If a class has a parent, the subclass instance variables are added to the parent CIR

• Because CIR is static, access to all instance variables is done as it is in records– Efficient

Page 22: ISBN 0- 321-49362-1 Object-Oriented Programming Chapter 11.1-11.3 Chapter 12.1-12.3

Copyright © 2007 Addison-Wesley. All rights reserved.1–23

Dynamic Binding of Methods Calls

• Methods in a class that are statically bound need not be involved in the CIR; methods that will be dynamically bound must have entries in the CIR– Calls to dynamically bound methods can be

connected to the corresponding code thru a pointer in the CIR

– The storage structure is sometimes called virtual method tables (vtable)

– Method calls can be represented as offsets from the beginning of the vtable

Page 23: ISBN 0- 321-49362-1 Object-Oriented Programming Chapter 11.1-11.3 Chapter 12.1-12.3

CIR for single inheritance

Copyright © 2007 Addison-Wesley. All rights reserved.1–24

Page 24: ISBN 0- 321-49362-1 Object-Oriented Programming Chapter 11.1-11.3 Chapter 12.1-12.3

CIR for Multiple Inheritance

Copyright © 2007 Addison-Wesley. All rights reserved.1–25