C. Varela; Adapted from S. Haridi and P. Van Roy1 Object-Oriented Programming Objects, Classes and Inheritance (VRH 7.1-4) Polymorphism (VRH 6.4.3) Carlos.

Slides:



Advertisements
Similar presentations
Final and Abstract Classes
Advertisements

Object-Oriented Programming Python. OO Paradigm - Review Three Characteristics of OO Languages –Inheritance It isn’t necessary to build every class from.
1 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
C. Varela; Adapted w/permission from S. Haridi and P. Van Roy1 Declarative Computation Model From kernel to practical language (CTM 2.6) Exceptions (CTM.
Object Oriented Programming Chapter 7 Programming Languages by Ravi Sethi.
Copyright 2002 by S. Haridi and P. Van Roy1 Object-Oriented Programming Seif Haridi Peter Van Roy.
Object-Oriented PHP (1)
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 Chapter 12 More OOP, Interfaces, and Inner Classes.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
C. Varela; Adapted w/permission from S. Haridi and P. Van Roy1 Declarative Computation Model Single assignment store Kernel language syntax Carlos Varela.
Encapsulation, Inheritance & Interfaces CSE 115 Spring 2006 February 27, March 1 & 3, 2006.
C. Varela; Adapted from S. Haridi and P. Van Roy1 Object-Oriented Programming Objects, Classes and Inheritance (VRH 7.1-2) Polymorphism (VRH 6.4.3) Carlos.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
C. Varela; Adapted w/permission from S. Haridi and P. Van Roy1 Declarative Computation Model Kernel language semantics Carlos Varela RPI Adapted with permission.
1 Chapter 7 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
C. Varela; Adapted from S. Haridi and P. Van Roy1 Object-Oriented Programming Inheritance (VRH 7.3-4) Object System Implementation/Java(VRH 7.6-7) Carlos.
C. Varela; Adapted w/permission from S. Haridi and P. Van Roy1 Declarative Programming Techniques Non-declarative needs (VRH 3.8) Program design in the.
Data Abstraction and Object- Oriented Programming CS351 – Programming Paradigms.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Chapter 7 - Generalization/Specialization and Inheritance1 Chapter 7 Generalization/Specialization and Inheritance.
Programming Languages and Paradigms Object-Oriented Programming.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Chapter 12: Adding Functionality to Your Classes.
OOPs Object oriented programming. Based on ADT principles  Representation of type and operations in a single unit  Available for other units to create.
Chapter 8 More Object Concepts
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
An Object-Oriented Approach to Programming Logic and Design
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
11 Chapter 11 Object-Oriented Databases Database Systems: Design, Implementation, and Management 4th Edition Peter Rob & Carlos Coronel.
Programming Pillars Introduction to Object- Oriented Programming.
Copyright S. Haridi & P. Van Roy1 Object-Oriented Programming Seif Haridi Peter Van Roy.
Inheritance in the Java programming language J. W. Rider.
Data Structures Using C++ 2E1 Inheritance An “is-a” relationship –Example: “every employee is a person” Allows new class creation from existing classes.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
ITEC 3220A Using and Designing Database Systems Instructor: Gordon Turpin Course Website: Office: CSEB3020.
CS451 - Lecture 2 1 CS451 Lecture 2: Introduction to Object Orientation Yugi Lee STB #555 (816) * Acknowledgement:
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.
Object Oriented Programming
ISBN Object-Oriented Programming Chapter Chapter
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
1/21/2016COSC , Lecture 91 Programming Language Concepts, COSC Lecture 9 Stateful programming Object-Oriented Programming.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Inheritance and Polymorphism. Superclass and Subclass Inheritance defines a relationship between objects that share characteristics. It is a mechanism.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
1 n Object Oriented Programming. 2 Introduction n procedure-oriented programming consists of writing a list of instructions and organizing these instructions.
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
ISBN Chapter 12 Support for Object-Oriented Programming.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Java Programming Fifth Edition Chapter 9 Introduction to Inheritance.
Object-oriented programming (OOP) is a programming paradigm using "objects" – data structures consisting of data fields and methods together with their.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Object-Oriented Programming
The Movement To Objects
7.1 What Is An Object Object-oriented program - Description or simulation of application Object-oriented programming is done by adopting or extending an.
Final and Abstract Classes
State, Object-Oriented Programming Explicit State, Polymorphism (CTM 6
Declarative Computation Model Kernel language semantics (Non-)Suspendable statements (VRH ) Carlos Varela RPI October 11, 2007 Adapted with.
The Procedure Abstraction Part V: Run-time Structures for OOLs
Object-Oriented Programming Encapsulation Control/Visibility (VRH 7. 3
More Object-Oriented Programming
Java Programming, Second Edition
Declarative Computation Model Single assignment store (VRH 2
Final and Abstract Classes
State, Object-Oriented Programming Explicit State, Polymorphism (CTM 6
Presentation transcript:

C. Varela; Adapted from S. Haridi and P. Van Roy1 Object-Oriented Programming Objects, Classes and Inheritance (VRH 7.1-4) Polymorphism (VRH 6.4.3) Carlos Varela RPI Adapted with permission from: Seif Haridi KTH Peter Van Roy UCL

C. Varela; Adapted from S. Haridi and P. Van Roy2 Overview What is object-oriented programming? –Objects –Classes –Interfaces Inheritance Polymorphism Static and dynamic binding Multiple Inheritance Method dispatching, multi-methods Reflection Active Objects Relationship to Other Programming Models

C. Varela; Adapted from S. Haridi and P. Van Roy3 System building Abstraction is the best tool to build complex systems Complex systems are built by layers of abstractions Each layer has two parts: –Specification, and –Implementation Any layer uses the specification of the lower layer to implement its functionality

C. Varela; Adapted from S. Haridi and P. Van Roy4 Properties needed to support the principle of abstraction Encapsulation –Hide internals from the interface Compositionality –Combine parts to make new parts Instantiation/invocation –Create new instances of parts

C. Varela; Adapted from S. Haridi and P. Van Roy5 Component-based programming Supports –Encapsulation –Compositionality –Instantiation

C. Varela; Adapted from S. Haridi and P. Van Roy6 Object-oriented programming Supports –Encapsulation –Compositionality –Instantiation Plus –Inheritance

C. Varela; Adapted from S. Haridi and P. Van Roy7 Inheritance Programs can be built in hierarchical structure from ADT’s that depend on other ADT’s (Components) Object-oriented programming (inheritance) is based on the idea that ADTs have so much in common For example, sequences (stacks, lists, queues) Object oriented programming enables building ADTs incrementally, through inheritance An ADT can be defined to inherit from another abstract data type, substantially sharing functionality with that abstract data type Only the difference between an abstract datatype and its ancestor has to be specified

C. Varela; Adapted from S. Haridi and P. Van Roy8 What is object-oriented programming? OOP (Object-oriented programming) = encapsulated state + inheritance Object –An entity with unique identity that encapsulates state –State can be accessed in a controlled way from outside –The access is provided by means of methods (procedures that can directly access the internal state) Class –A specification of objects in an incremental way –Incrementality is achieved inheriting from other classes by specifying how its objects (instances) differ from the objects of the inherited classes

C. Varela; Adapted from S. Haridi and P. Van Roy9 Instances (objects) Interface (what methods are available) State (attributes) Procedures (methods)

C. Varela; Adapted from S. Haridi and P. Van Roy10 Classes (simplified syntax) A class is a statement class  ClassVariable  attr  AttrName1  :  AttrNameN  meth  Pattern1   Statement  end : meth  PatternN   Statement  end end

C. Varela; Adapted from S. Haridi and P. Van Roy11 Classes (simplified syntax) A class can also be a value that can be in an expression position class $ attr  AttrName1  :  AttrNamen  meth  Pattern   Statement  end : meth  Pattern   Statement  end end

C. Varela; Adapted from S. Haridi and P. Van Roy12 Classes in Oz The class Counter has the syntactic form class Counter attr val meth display end meth inc(Value) val + Value end meth init(Value) val := Value end end

C. Varela; Adapted from S. Haridi and P. Van Roy13 Attributes of Classes The class Counter has the syntactic form class Counter attr val meth display end meth inc(Value) val + Value end meth init(Value) val := Value end end val is an attribute: a modifiable cell that is accessed by the atom val

C. Varela; Adapted from S. Haridi and P. Van Roy14 Attributes of classes The class Counter has the syntactic form class Counter attr val meth display end meth inc(Value) val + Value end meth init(Value) val := Value end end the attribute val is accessed by the val

C. Varela; Adapted from S. Haridi and P. Van Roy15 Attributes of classes The class Counter has the syntactic form class Counter attr val meth display end meth inc(Value) val + Value end meth init(Value) val := Value end end the attribute val is assigned by the operator := as val :=...

C. Varela; Adapted from S. Haridi and P. Van Roy16 Methods of classes The class Counter has the syntactic form class Counter attr val meth display end meth inc(Value) val + Value end meth init(Value) val := Value end end methods are statements method head is a record (tuple) pattern

C. Varela; Adapted from S. Haridi and P. Van Roy17 Classes in Oz The class Counter has the syntactic form class Counter attr val meth display end meth inc(Value) val + Value end meth init(Value) val := Value end end

C. Varela; Adapted from S. Haridi and P. Van Roy18 Example An object is created from a class using the procedure New/3, whose first argument is the class, the second is the initial method, and the result is the object (such as in the functor and procedure implementation approaches) New/3 is a generic procedure for creating objects from classes. declare C = {New Counter init(0)} {C display} {C inc(1)} {C display}

C. Varela; Adapted from S. Haridi and P. Van Roy19 Example The following shows how an object is created from a class using the procedure New/3, whose first argument is the class, the second is the initial method, and the result is the object. New/3 is a generic procedure for creating objects from classes. declare C = {New Counter init(0)} {C display} {C inc(1)} {C display} Object interface is as a procedure of one argument, which expects a record representing a method

C. Varela; Adapted from S. Haridi and P. Van Roy20 A class X is defined by: –class X... end Attributes are defined using the attribute-declaration part before the method-declaration part: –attr A 1... A N Then follow the method declarations, each has the form: –meth E S end The expression E evaluates to a method head, which is a record whose label is the method name. Summary

C. Varela; Adapted from S. Haridi and P. Van Roy21 An attribute A is accessed An attribute is assigned a value using A := E A class can be defined as a value: X = class $... end Summary

C. Varela; Adapted from S. Haridi and P. Van Roy22 Attribute Initialization Stateful (may be updated by := ) Initialized at object creation time, all instances have the initial balance = 0 class Account attr balance:0 meth … end … end In general the initial value of an attribute could be any legal value (including classes and objects)

C. Varela; Adapted from S. Haridi and P. Van Roy23 Attribute Initialization Initialization by instance class Account attr balance meth init(X) balance := X end … end C1 = {New Account init(100)} C1 = {New Account init(50)}

C. Varela; Adapted from S. Haridi and P. Van Roy24 Attribute Initialization Initialization by brand declare L=linux class RedHat attr ostype:L meth get(X) X end end class SuSE attr ostype:L meth get(X) X end end class Debian attr ostype:L meth get(X) X end end

C. Varela; Adapted from S. Haridi and P. Van Roy25 Example class Queue attr front back count meth init Q in front := Q back := Q count := 0 end meth put(X) Q = X|Q back := Q count + 1 end... end

C. Varela; Adapted from S. Haridi and P. Van Roy26 Example class Queue attr front back count meth init Q in front := Q back := Q count := 0 end meth put(X) Q = X|Q back := Q count + 1 end... end front back Q0 front back a | Q1 put(a)

C. Varela; Adapted from S. Haridi and P. Van Roy27 Example class Queue attr front back count... meth get(?X) Q in X|Q front := Q count - 1 end meth count(X) X end... end front back a | Q1 X front back a | Q1 X

C. Varela; Adapted from S. Haridi and P. Van Roy28 Classes as incremental ADTs Object-oriented programming allows us to define a class by extending existing classes Three things have to be introduced –How to express inheritance, and what does it mean? –How to access particular methods in the new class and in preexisting classes –Visibility – what part of the program can see the attributes and methods of a class

C. Varela; Adapted from S. Haridi and P. Van Roy29 Inheritance Inheritance should be used as a way to specialize a class while retaining the relationship between methods In this way it is just an extension of an ADT The other view is inheritance is just a (lazy) way to construct new abstract data types! No relationships are preserved general class specialized class

C. Varela; Adapted from S. Haridi and P. Van Roy30 Inheritance class Account attr balance:0 meth transfer(Amount) balance end meth getBal(B) B end A={New Account transfer(100)}

C. Varela; Adapted from S. Haridi and P. Van Roy31 Inheritance II Conservative extension class VerboseAccount from Account meth verboseTransfer(Amount)... end The class VerboseAccount has the methods: transfer, getBal, and verboseTransfer

C. Varela; Adapted from S. Haridi and P. Van Roy32 Inheritance II Non-Conservative extension class AccountWithFee from VerboseAccount attr fee:5 meth transfer(Amount)... end The class AccountWithFee has the methods: transfer, getBal, and verboseTransfer The method transfer has been redefined (overridden) with another definition

C. Varela; Adapted from S. Haridi and P. Van Roy33 Inheritance II Non-Conservative extension class AccountWithFee from VerboseAccount attr fee:5 meth transfer(Amount)... end Account VerboseAccount AccountWithFee

C. Varela; Adapted from S. Haridi and P. Van Roy34 Polymorphismn The ability for operations to take objects (instances) of different types. For example, the transfer method can be invoked in account object instances of three different classes. The most specific behavior should be executed. Account VerboseAccount AccountWithFee

C. Varela; Adapted from S. Haridi and P. Van Roy35 Static and dynamic binding Dynamic binding Inside an object O we want to invoke a method M This is written as {self M}, and chooses the method visible in the current object (M of D) class C meth M class D a subclass of C meth M O an instance of D

C. Varela; Adapted from S. Haridi and P. Van Roy36 Static and dynamic binding Static binding Inside an object O we want to invoke a method M in a specific (super) class This is written as C, M and chooses the method visible in the super class C (M of C) class C meth M class D a subclass of C meth M O an instance of D

C. Varela; Adapted from S. Haridi and P. Van Roy37 Static method calls Given a class and a method head m(…), a static method-call has the following form: C, m(…) Invokes the method defined in the class argument. A static method call can only be used inside class definitions. The method call takes the current object denoted by self as implicit argument. The method m could be defined in the class C, or inherited from a super class.

C. Varela; Adapted from S. Haridi and P. Van Roy38 Inheritance class Account attr balance:0 meth transfer(Amount) balance end meth getBal(B) B end A={New Account transfer(100)}

C. Varela; Adapted from S. Haridi and P. Van Roy39 Inheritance II Conservative extension class VerboseAccount from Account meth verboseTransfer(Amount) { self transfer(Amount)} end The class VerboseAccount has the methods: transfer, getBal, and verboseTransfer

C. Varela; Adapted from S. Haridi and P. Van Roy40 Inheritance II Non-Conservative extension class AccountWithFee from VerboseAccount attr fee:5 meth transfer(Amount) VerboseAccount, transfer(Amount end The class AccountWithFee has the methods: transfer, getBal, and verboseTransfer The method transfer has been redefined (overridden) with another definition

C. Varela; Adapted from S. Haridi and P. Van Roy41 Inheritance II Non-Conservative extension class AccountWithFee from VerboseAccount attr fee:5 meth transfer(Amount) VerboseAccount, transfer(Amount end Non-Conservative inheritance is dangerous because it might change the relationship between methods and the invariants the programmer depends on Account getBalance(B); transfer(S); getBalance(B1) => B1 = B+S

C. Varela; Adapted from S. Haridi and P. Van Roy42 Inheritance II Non-Conservative extension class AccountWithFee from VerboseAccount attr fee:5 meth transfer(Amount) VerboseAccount, transfer(Amount end Non-Conservative inheritance is dangerous because it might change the relationship between methods and the invariants the programmer depends on AccountWithFee getBalance(B); transfer(S); getBalance(B1) => B1 =

C. Varela; Adapted from S. Haridi and P. Van Roy43 Inheritance III Classes may inherit from one or several classes appearing after the keyword: from. A class B is a superclass of a class A if: – B appears in the from declaration of A, or – B is a superclass of a class appearing in the from declaration of A. The methods (attributes and features) available in a class C (i.e. visible) are defined through a precedence relation on the methods that appear in the class hierarchy based on the overriding relation: –A method in a class C overrides any method, with the same label, in any super class of C.

C. Varela; Adapted from S. Haridi and P. Van Roy44 SuperClass relation C SuperClass relation is directed and acyclic.

C. Varela; Adapted from S. Haridi and P. Van Roy45 SuperClass relation C SuperClass relation is directed and acyclic. After striking out all overridden methods each remaining method should have a unique label and be defined only in one class in the hierarchy.

C. Varela; Adapted from S. Haridi and P. Van Roy46 Inheritance relation C m m m A (valid hierarchy) (invalid hierarchy) m

C. Varela; Adapted from S. Haridi and P. Van Roy47 Multiple Inheritance Example class Account attr balance:0 meth transfer(Amount) balance end meth getBal(?B) B end end class Customer attr name meth init(N) name  N end end class CustomerAccount from Customer Account end A={New CustomerAccount init}

C. Varela; Adapted from S. Haridi and P. Van Roy48 Illegal inheritance class Account attr balance meth init(Amount) balance  Amount end meth transfer(Amount) balance end meth getBal(B) B end end class Customer attr name meth init(N) name  N end end class CustomerAccount from Customer Account end There are two init methods visible for CustomerAccount This is illegal

C. Varela; Adapted from S. Haridi and P. Van Roy49 Legal inheritance class Account attr balance meth init(Amount) balance  Amount end meth transfer(Amount) balance end meth getBal(B) B end end class Customer attr name meth init(N) name  N end end class CustomerAccount from Customer Account meth init(N A) Customer, init(N) Account, init(A) end CustomerAccount has attributes balance and name methods init, transfer and getBalance This overriding is not harmful: it does not change relationships in super classes

C. Varela; Adapted from S. Haridi and P. Van Roy50 Controlling visibility Visibility is the control given to the user to limit access to members of a class (attributes, methods and properties) Each member is defined with a scope (part of program text that the member can be accessed by name) Programming languages use words like public, private and protected to define visibility Unfortunately different languages use these keywords to define different scopes

C. Varela; Adapted from S. Haridi and P. Van Roy51 Public and private scopes in ADTs A private member is one which is only visible in the object instance (it is used for implementing the ADT) The object instance can see all the private members in its class and its super classes A public member is visible anywhere in the program It is part of the interface of the ADT In Oz (and Smalltalk) attributes are private and methods are public (the default rule) In Java and C++ private has another meaning

C. Varela; Adapted from S. Haridi and P. Van Roy52 The meaning of Private C SubC SubSubC I1I4I2I3 Instances Class Hierarchy

C. Varela; Adapted from S. Haridi and P. Van Roy53 The meaning of Private C SubC SubSubC I1I4I2I3 Instances Class Hierarchy According to Smalltalk and Oz All private members in this region are visible to I3

C. Varela; Adapted from S. Haridi and P. Van Roy54 The meaning of Private C SubC SubSubC I1I4I2I3 Instances Class Hierarchy According to C++ and Java All private members in this region are visible to I3

C. Varela; Adapted from S. Haridi and P. Van Roy55 Public and private scopes in ADTs In Oz (and Smalltalk) attributes are private and methods are public It is possible in Oz to make a method private within a class Using a variable identifier as a method head will make the method local to the class The variable is automatically bound to a unique name class C meth A(...)... end.... end

C. Varela; Adapted from S. Haridi and P. Van Roy56 Public and private scopes in ADTs In Oz (and Smalltalk) attributes are private and methods are public It is possible in Oz to make a method private within a class Using a variable identifier as a method head will make the method local to the class The variable is automatically bound to a unique name ! is an escape character, !A means escape the class scope class C meth A(...)... end.... end A local A = {NewName} in class C !A(...) meth !A(...)... end.... end

C. Varela; Adapted from S. Haridi and P. Van Roy57 Exercises Do Java and C++ object abstractions completely encapsulate internal state? If so, how? If not, why? Do Java and C++ enable static access to methods defined in classes arbitrarily high in the inheritance hierarchy? If so, how? If not, why? Do Java and C++ allow multiple inheritance? If so, how? If not, why? Exercises VRH (pg 567) Exercise VRH (pg 568) Read VRH Sections 7.5, 7.7 and Multijava paper.