Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "ISBN 0- 321-49362-1 Object-Oriented Programming Chapter 11.1-11.3 Chapter 12.1-12.3."— Presentation transcript:

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

2 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

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

4 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

5 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

6 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

7 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

8 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

9 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

10 Multiple Inheritance Multiple inheritance allows a new class to inherit from two or more classes Copyright © 2007 Addison-Wesley. All rights reserved. 1–10

11 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

12 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

13 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

14 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

15 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

16 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

17 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 (~)

18 Copyright © 2007 Addison-Wesley. All rights reserved. 1–18 Support for OOP in C# All class instances are heap dynamic struct s 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

19 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

20 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

21 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

22 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

23 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

24 CIR for single inheritance Copyright © 2007 Addison-Wesley. All rights reserved. 1–24

25 CIR for Multiple Inheritance Copyright © 2007 Addison-Wesley. All rights reserved. 1–25


Download ppt "ISBN 0- 321-49362-1 Object-Oriented Programming Chapter 11.1-11.3 Chapter 12.1-12.3."

Similar presentations


Ads by Google