Object-Oriented Programming Chapter 11.1-11.3 Chapter 12.1-12.3.

Slides:



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

ISBN Chapter 12 Support for Object-Oriented Programming.
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.
CPS 506 Comparative Programming Languages Abstract Data Type and Encapsulation.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
ISBN Chapter 12 Support for Object-Oriented Programming.
Object-Oriented Programming
Lecture 9 Concepts of Programming Languages
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
Chapter 11 Abstract Data Types and Encapsulation Concepts.
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.
CS 403 – Programming Languages Class 25 November 28, 2000.
Object-Oriented Programming Session 9 Course : T Programming Language Concept Year : February 2011.
Object-oriented programming: C++ class A { private: …… // can be accessd by A protected: …… // can be accessed by A and // its derived classes public:
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
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.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
ISBN Chapter 12 Support for Object-Oriented Programming.
Lecture 10 Concepts of Programming Languages Arne Kutzner Hanyang University / Seoul Korea.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
Chapter 12 Support for Object-Oriented Programming.
(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.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
C++ General Characteristics: - Mixed typing system - Constructors and destructors - Elaborate access controls to class entities.
ISBN Chapter 12 Support for Object-Oriented Programming.
Object Oriented Programming CMSC 331. Concept of Abstraction “An abstraction is a view or representation of an entity that includes only the attributes.
Chapter 12: Support for Object- Oriented Programming Lecture # 18.
Support for Object-Oriented Programming
Abstract Data Types and Encapsulation Concepts
Support for Object-Oriented Programming
Support for Object-Oriented Programming
CMP 339/692 Programming Languages Day 24 Tuesday May 1, 2012
11.1 The Concept of Abstraction
Lecture 9 Concepts of Programming Languages
Abstract Data Types and Encapsulation Concepts
Support for Object-Oriented Programming
Abstract Data Types and Encapsulation Concepts
Object-Oriented Programming
Support for Object-Oriented Programming
Support for Object-Oriented Programming
Support for Object-Oriented Programming
Support for Object-Oriented Programming
Lecture 10 Concepts of Programming Languages
11.1 The Concept of Abstraction
Lecture 9 Concepts of Programming Languages
Chapter 11 Abstraction - The concept of abstraction is fundamental in
Presentation transcript:

Object-Oriented Programming Chapter Chapter

Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Object-Oriented Programming Abstract data types –Data abstraction Inheritance –Inheritance is the central theme in OOP and languages that support it Polymorphism

Copyright © 2007 Addison-Wesley. All rights reserved. 1–3 Design Issues for OOP Syntax for ADT What built-in operations Objects and primitives? Subclasses as Types Type Checking and Polymorphism Single or Multiple Inheritance Object Allocation and De-Allocation Dynamic and Static Binding Nested Classes

Copyright © 2007 Addison-Wesley. All rights reserved. 1–4 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

Copyright © 2007 Addison-Wesley. All rights reserved. 1–5 Introduction to Data Abstraction 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 Advantages: program organization, modifiability, separate compilation –The representation of objects of the type is hidden from the program units that use these objects Only available operations are provided in the type definition Reliability improved by hiding the data representations

Copyright © 2007 Addison-Wesley. All rights reserved. 1–6 Object-Oriented Concepts 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

Copyright © 2007 Addison-Wesley. All rights reserved. 1–7 Members of Classes 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

Copyright © 2007 Addison-Wesley. All rights reserved. 1–8 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 or inheritance

Copyright © 2007 Addison-Wesley. All rights reserved. 1–9 Exclusivity of Objects Everything is an object –Advantage - elegance and purity –Disadvantage - slow operations on simple objects Add objects to a complete typing system –Advantage - fast operations on simple objects –Disadvantage - results in a confusing type system (two kinds of entities) Include an imperative-style typing system for primitives but make everything else objects –Advantage - fast operations on simple objects and a relatively small typing system –Disadvantage - still some confusion because of the two type systems

Copyright © 2007 Addison-Wesley. All rights reserved. 1–10 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 One disadvantage of inheritance for reuse: –Creates interdependencies among classes that complicate maintenance

Copyright © 2007 Addison-Wesley. All rights reserved. 1–11 Inheritance Terms A class that inherits is a derived class or a subclass The class from which another class inherits is a parent class or superclass A class can modify an inherited method –The subclass definition overrides the inherited one

Copyright © 2007 Addison-Wesley. All rights reserved. 1–12 Single and Multiple Inheritance Multiple inheritance allows a new class to inherit from two or more classes Disadvantages of multiple inheritance: –Language and implementation complexity (in part due to name collisions) –Potential inefficiency - dynamic binding costs more with multiple inheritance (but not much) Advantage: –Sometimes it is extremely convenient and valuable

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

Copyright © 2007 Addison-Wesley. All rights reserved. 1–14 Dynamic Binding Polymorphic variable can reference objects of the class it belongs to and any of its descendants 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 –Dynamic type checking is costly and delays error detection –If overridden methods are restricted to having the same parameter and return types, checking can be static

Copyright © 2007 Addison-Wesley. All rights reserved. 1–15 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 Allow the user to specify

Copyright © 2007 Addison-Wesley. All rights reserved. 1–16 Are Subclasses Subtypes? Does an “is-a” relationship hold between a parent class object and an object of the subclass? –If a derived class is-a parent class, then objects of the derived class must behave the same as the parent class object A derived class is a subtype if it has an is-a relationship with its parent class –Subclass can only add variables and methods and override inherited methods in “compatible” ways

Copyright © 2007 Addison-Wesley. All rights reserved. 1–17 Object Allocation and Deallocation Object Allocation –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

Copyright © 2007 Addison-Wesley. All rights reserved. 1–18 Nested Classes If a new class is needed by only one class, there is no reason to define so it can be seen by other classes –Can the new class be nested inside the class that uses it? –In some cases, the new class is nested inside a subprogram rather than directly in another class Other issues: –Which facilities of the nesting class should be visible to the nested class and vice versa

Copyright © 2007 Addison-Wesley. All rights reserved. 1–19 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

Copyright © 2007 Addison-Wesley. All rights reserved. 1–20 Encapsulation in C++ Use class is 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 class data members Instances can be static, stack dynamic, or heap dynamic

Copyright © 2007 Addison-Wesley. All rights reserved. 1–21 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 –Can be implicitly or explicitly called –Name is the same as the class name Destructors –Cleanup after an instance is destroyed (reclaim heap storage) –Can be explicitly or implicitly called –Name is the class name, preceded by a tilde (~)

Copyright © 2007 Addison-Wesley. All rights reserved. 1–22 Support for OOP in Java All user-defined types are classes All data are objects except the primitive types All objects are allocated from the heap and accessed through reference variables –Objects are allocated with new A finalize method is implicitly by garbage collector Inheritance –Single inheritance only –Interfaces provide some of the benefits of multiple inheritance –Methods can be final (cannot be overriden)

Copyright © 2007 Addison-Wesley. All rights reserved. 1–23 Support for OOP in Java 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 Several varieties of nested classes

Copyright © 2007 Addison-Wesley. All rights reserved. 1–24 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

Copyright © 2007 Addison-Wesley. All rights reserved. 1–25 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

Copyright © 2007 Addison-Wesley. All rights reserved. 1–26 C# Properties Common solution to need for access to data members: accessor methods (getter and setter) C# provides properties as a way of implementing getters and setters without requiring explicit method calls

Copyright © 2007 Addison-Wesley. All rights reserved. 1–27 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

Copyright © 2007 Addison-Wesley. All rights reserved. 1–28 Implementing OO Constructs Two interesting and challenging parts –Storage structures for instance variables –Dynamic binding of messages to methods

Copyright © 2007 Addison-Wesley. All rights reserved. 1–29 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

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

Copyright © 2007 Addison-Wesley. All rights reserved. 1–31 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

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