CSC321: Programming Languages13-1 Programming Languages Tucker and Noonan Chapter 13: Object-Oriented Programming 13.1 Prelude: Abstract Data Types 13.2.

Slides:



Advertisements
Similar presentations
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 13 Object-Oriented Programming I am surprised.
Advertisements

OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Chapter 1 Object-Oriented Concepts. A class consists of variables called fields together with functions called methods that act on those fields.
Abstract Data Types Data abstraction, or abstract data types, is a programming methodology where one defines not only the data structure to be used, but.
Inheritance Inheritance Reserved word protected Reserved word super
Chapter 12: Support for Object-Oriented Programming
Object Oriented Programming Chapter 7 Programming Languages by Ravi Sethi.
ITEC200 – Week03 Inheritance and Class Hierarchies.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
ISBN Chapter 12 Support for Object-Oriented Programming.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 13 Object-Oriented Programming I am surprised.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 13 Object-Oriented Programming I am surprised.
1 Inheritance and Polymorphism. 2 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common.
Abstract Data Types and Encapsulation Concepts
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
ISBN Chapter 12 Support for Object- Oriented Programming.
Object Oriented Programming
OOP Languages: Java vs C++
CPS 506 Comparative Programming Languages
Programming Languages and Paradigms 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.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
CS 403 – Programming Languages Class 25 November 28, 2000.
1 Chapter 10: Data Abstraction and Object Orientation Aaron Bloomfield CS 415 Fall 2005.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
Programming Languages and Paradigms Object-Oriented Programming (Part II)
Programming Languages and Paradigms Object-Oriented Programming.
Chapter 12 Support for Object oriented Programming.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Types in programming languages1 What are types, and why do we need them?
Summing Up Object Oriented Design. Four Major Components: Abstraction modeling real-life entities by essential information only Encapsulation clustering.
Chapter 14 Abstract Classes and Interfaces. Abstract Classes An abstract class extracts common features and functionality of a family of objects An abstract.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Chapter 10, Slide 1 ABSTRACT DATA TYPES Based on the fundamental concept of ABSTRACTION:  process abstraction  data abstraction Both provide:  information.
Lecture 10 Concepts of Programming Languages Arne Kutzner Hanyang University / Seoul Korea.
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
Session 24 Chapter 12: Polymorphism. Polymorphism polymorphism comes from the Greek root for “many forms” polymorphism is about how we can use different.
Chapter 12 Support for Object-Oriented Programming.
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.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
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
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Session 18 Lab 9 Re-cap, Chapter 12: Polymorphism & Using Sound in Java Applications.
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.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
ISBN Chapter 12 Support for Object-Oriented Programming.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Inheritance a subclass extends the functionality of a superclass a subclass inherits all the functionality of a superclass don't reinvent the wheel – "stand.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Lecture 12 Inheritance.
12.1 Introduction - Categories of languages that support OOP:
Support for Object-Oriented Programming
7.1 What Is An Object Object-oriented program - Description or simulation of application Object-oriented programming is done by adopting or extending an.
Inheritance and Polymorphism
Types of Programming Languages
Java Programming Language
Object-Oriented Programming
Support for Object-Oriented Programming
Support for Object-Oriented Programming
Lecture 10 Concepts of Programming Languages
Presentation transcript:

CSC321: Programming Languages13-1 Programming Languages Tucker and Noonan Chapter 13: Object-Oriented Programming 13.1 Prelude: Abstract Data Types 13.2 The Object Model 13.3 Smalltalk 13.4 Java 13.5 Python

CSC321: Programming Languages13-2 Procedure Abstraction Imperative programming paradigm –Algorithms + Data Structures = Programs [Wirth] –Produce a program by functional decomposition Start with function to be computed Systematically decompose function into more primitive functions Stop when all functions map to program statements Concerned mainly with interface –Function –What it computes –Ignore details of how –Example: sort(list, length);

CSC321: Programming Languages13-3 Data Abstraction ADT: Abstract Data Types Extend procedural abstraction to include data –Example: type float Extend imperative notion of type by: –Providing encapsulation of data/functions –Example: stack of int's –Separation of interface from implementation

CSC321: Programming Languages13-4 Encapsulation is a mechanism which allows logically related constants, types, variables, methods, and so on, to be grouped into a new entity. Examples: –Procedures –Packages –Classes Encapsulation

CSC321: Programming Languages13-5 A Simple Stack in C Figure 13.1

CSC321: Programming Languages13-6 A Stack Type in C Figure 13.2

CSC321: Programming Languages13-7 Implementation of Stack Type in C Figure 13.3

CSC321: Programming Languages13-8 Goal of Data Abstraction Package –Data type –Functions Into a module so that functions provide: –public interface –Type definition

CSC321: Programming Languages13-9 generic type element is private; package stack_pck is type stack is private; procedure push (in out s : stack; i : element); procedure pop (in out s : stack) return element; procedure isempty(in s : stack) return boolean; procedure top(in s : stack) return element; private type node; type stack is access node; type node is record val : element; next : stack; end record; end stack_pck; ADT in Ada

CSC321: Programming Languages13-10 package body stack_pck is procedure push (in out s : stack; i : element) is temp : stack; begin temp := new node; temp.all := (val => i, next => s); s := temp; end push; procedure isempty(in s : stack) return boolean is begin return s = null; end isempty; procedure pop (in out s : stack) return element is temp : stack; elem : element; begin elem := s.all.val; temp := s; s := temp.all.next; dispose(temp); return elem; end pop; procedure top(in s : stack) return element is begin return s.all.val; end top; end stack_pck; ADT in Ada

CSC321: Programming Languages13-11 The Object Model Problems remained: –Automatic initialization and finalization –No simple way to extend a data abstraction Concept of a class Object decomposition, rather than function decomposition

CSC321: Programming Languages13-12 A class is a type declaration which encapsulates constants, variables, and functions for manipulating these variables. A class is a mechanism for defining an ADT. –Constructor –Destructor –Client of a class –Class methods (Java static methods) –Instance methods Classes

CSC321: Programming Languages13-13 class MyStack { class Node { Object val; Node next; Node(Object v, Node n) { val = v; next = n; } } Node theStack; MyStack( ) { theStack = null; } boolean empty( ) { return theStack == null; } Object pop( ) { Object result = theStack.val; theStack = theStack.next; return result; } Object top( ) { return theStack.val; } void push(Object v) { theStack = new Node(v, theStack); } Java Class

CSC321: Programming Languages13-14 OO program: collection of objects which communicate by sending messages Generally, only 1 object is executing at a time Object-based language (vs. OO language) Classes –Determine type of an object –Permit full type checking Visibility –public –protected –private OO Program

CSC321: Programming Languages13-15 Inheritance Class hierarchy –Subclass, parent or super class is-a relationship –A stack is-a kind of a list –So are: queue, deque, priority queue has-a relationship –Identifies a class as a client of another class –Aggregation –A class is an aggregation if it contains other class objects

CSC321: Programming Languages13-16 Single Inheritance In single inheritance, the class hierarchy forms a tree. Rooted in a most general class: Object Inheritance supports code reuse Remark: in Java a Stack extends a Vector –Good or bad idea? –Why? Single inheritance languages: Smalltalk, Java

CSC321: Programming Languages13-17 Multiple Inheritance Allows a class to be a subclass of zero, one, or more classes. Class hierarchy is a directed graph Advantage: facilitates code reuse Disadvantage: more complicated semantics C++, Effiel No inheritance – final in Java –A class can not be inherited, meaning no subclasses –Java uses final –Java final methods can not overridden –Java final variables can not be changed Super 1Super 2 Sub

CSC321: Programming Languages13-18 Problems with Multiple Inheritance 1.Two superclasses define a same method with the same signature, which is inherited? Consider the following example. A is super of B and C, D is sub of B and C. Assume both B and C have a method myMethod, the question is which method D will inherit, B.myMethod or C.myMethod? Similarly, if A has a method aMethod which is overridden by B and C, which method body will be inherited by D, A.aMethod, B.aMethod, or C.aMethod?

CSC321: Programming Languages13-19 Problems with Multiple Inheritance 2.Two superclasses declare a same variable name with different types, how many variables should be inherited and which type? Consider another situation. Assume B declares a variable x of Class1 and C declares a variable x of Class2, both of which can be inherited by subclasses. The question is how many x’s D should have? If D inherits only one x, which type should x be of, Class1 or Class2? If A declares a variable, say y, which is inherited by both B and C, how many y’s D should have?

CSC321: Programming Languages13-20 Solutions Eiffel: rename –D redefines the inherited methods: e.g. myMethodB for B.myMethod, and myMethodC for C.myMethod –D redeclares the inherited variables: e.g. xB for B.x and xC for C.x C++: use :: to specify which method or variable is inherited Java –Does not allow multiple inheritance of classes –However, does allow multiple inheritance of interfaces, because Interfaces do not declare variables Interfaces do not have method bodies –Also allows a subclass to inherit one class and implement multiple interfaces

CSC321: Programming Languages13-21 A language is object-oriented if it supports –an encapsulation mechanism with information hiding for defining abstract data types, –virtual methods, and –inheritance OO Language

CSC321: Programming Languages13-22 Polymorphism Polymorphic - having many forms In OO languages polymorphism refers to the late binding of a call to one of several different implementations of a method in an inheritance hierarchy. Consider the call: obj.m( ); obj of type T All subtypes must implement method m( ) In a statically typed language, verified at compile time Actual method called can vary at run time depending on actual type of obj

CSC321: Programming Languages13-23 A subclass method is substitutable for a parent class method if the subclass’s method performs the same general function. Consider for (Drawable obj : myList) obj.paint( ); // paint method invoked varies // each graphical object paints itself // essence of OOP Thus, the paint method of each graphical object must be transparent to the caller. The code to paint each graphical object depends on the principle of substitutability. Essence: same call evokes a different method depending on class of object Ex: obj.paint(g); –Button –Panel –Choice Box Substitutability Principle

CSC321: Programming Languages13-24 C++ solution: C++ provides both static binding and dynamic binding –Static binding: default Use the name-declaration (type) binding: The method call is bound to the method body of the class to which the variable is declared –Dynamic binding: virtual functions Use the keyword virtual to declare virtual functions. For the virtual functions, the function call is bound to the method body of the class of which the value of the variable is. Delphi solution: Similar to C++, but for dynamic binding, the method in the superclass should be declared as virtual and the method in the subclass should be declared as override Static vs. Dynamic Binding

CSC321: Programming Languages13-25 Java solution: –Only final methods are statically bound, in which case they can not be overridden. –All other methods are dynamically bound. This is default. Ada solution: –Static binding – default –Dynamic binding – use classwide type. For a tagged type T, the classwide type is specified with T’class. A variable of type T’class can be of type T or any type derived from T. E.g. Person’class can be of Person, Student or Professor Static vs. Dynamic Binding (cont.)

CSC321: Programming Languages13-26 Templates or Generics A kind of class generator Can restrict a Collections class to holding a particular kind of object A template defines a family of classes parameterized by one or more types. Prior to Java 1.5, clients had to downcast an object retrieved from a Collection class. ArrayList list = new ArrayList ();... for (Drawable d : list) d.paint(g);

CSC321: Programming Languages13-27 Abstract Classes An abstract class is one that is either declared to be abstract or has one or more abstract methods. An abstract method is a method that contains no code beyond its signature. Any subclass of an abstract class that does not provide an implementation of an inherited abstract method is itself abstract. Because abstract classes have methods that cannot be executed, client programs cannot initialize an object that is a member an abstract class. This restriction ensures that a call will not be made to an abstract (unimplemented) method.

CSC321: Programming Languages13-28 abstract class Expression {... } class Variable extends Expression {... } abstract class Value extends Expression {... } class IntValue extends Value {... } class BoolValue extends Value {... } class FloatValue extends Value {... } class CharValue extends Value {... } class Binary extends Expression {... } class Unary extends Expression {... }

CSC321: Programming Languages13-29 Interfaces An interface encapsulates a collection of constants and abstract method signatures. An interface may not include either variables, constructors, or non-abstract methods. public interface Map { public abstract boolean containsKey(Object key); public abstract boolean containsValue(Object value); public abstract boolean equals(Object o); public abstract Object get(Object key); public abstract Object remove(Object key);... }

CSC321: Programming Languages13-30 Because it is not a class, an interface does not have a constructor, but an abstract class does. Some like to think of an interface as an alternative to multiple inheritance. Strictly speaking, however, an interface is not quite the same since it doesn't provide a means of reusing code; i.e., all of its methods must be abstract. An interface is similar to multiple inheritance in the sense that an interface is a type. A class that implements multiple interfaces appears to be many different types, one for each interface. Interfaces

CSC321: Programming Languages13-31 Virtual Method Table (VMT) How is the appropriate virtual method is called at run time. At compile time the actual run time class of any object may be unknown. MyList myList;... System.out.println(myList.toString( )); Each class has its own VMT, with each instance of the class having a reference (or pointer) to the VMT. A simple implementation of the VMT would be a hash table, using the method name (or signature, in the case of overloading) as the key and the run time address of the method invoked as the value. For statically typed languages, the VMT is kept as an array. The method being invoked is converted to an index into the VMT at compile time.

CSC321: Programming Languages13-32 Example class A { Obj a; void am1( ) {... } void am2( ) {... } } class B extends A { Obj b; void bm1( ) {... } void bm2( ) {... } void am2( ) {... } }

CSC321: Programming Languages13-33 Run Time Type Identification Run time type identification (RTTI) is the ability of the language to identify at run time the actual type or class of an object. All dynamically typed languages have this ability, whereas most statically typed imperative languages, such as C, lack this ability. At the machine level, recall that data is basically untyped. In Java, for example, given any object reference, we can determine its class via: Class c = obj.getClass( );

CSC321: Programming Languages13-34 Reflection Reflection is a mechanism whereby a program can discover and use the methods of any of its objects and classes. Reflection is essential for programming tools that allow plugins (such as Eclipse -- and for JavaBeans components. In Java the Class class provides the following information about an object: The superclass or parent class. The names and types of all fields. The names and signatures of all methods. The signatures of all constructors. The interfaces that the class implements.

CSC321: Programming Languages13-35 Class class = obj.getClass( ); Constructor[ ] cons = class.getDeclaredConstructors( ); for (int i=0; i < cons.length; i++) { System.out.print(class.getName( ) + "(" ); Class[ ] param = cons[i].getParameterTypes( ); for (int j=0; j < param.length; j++) { if (j > 0) System.out.print(", "); System.out.print(param[j].getName( ); } System.out.println( ")" ); } Reflection Example in Java

CSC321: Programming Languages13-36 Smalltalk the original object-oriented language developed in 1970s at Xerox PARC Xerox Alto –Smalltalk system –OS –IDE –mouse based GUI –Steve Jobs visit Simple language Most of the class libraries written in Smalltalk Everything is an object, even control structures Excluding lexical productions, grammar has 21 productions (3 pages)

CSC321: Programming Languages13-37 The value of every variable is an object; every object is an instance of some class. A method is triggered by sending a message to an object. –The object responds by evaluating the method of the same name, if it has one. –Otherwise the message is sent to the parent object. –The process continues until the method is found; otherwise an error is raised. All methods return a value (object). Precedence –Unary messages, as in: x negated –Binary messages, as in: x + y –Keyword messages, as in: Turtle go: length In the absence of parentheses, code is evaluated from left to right.

CSC321: Programming Languages13-38 Java 1.0 released in (or Java 5) in 2004 major language changes: 1.1, 1.5 steady growth in SE libraries mixed language –primitive types: int, double, boolean –objects statically typed single inheritance Direct support for: –inner classes –visibility modifiers –abstract classes –interfaces –generics –run time type identification –reflection

CSC321: Programming Languages13-39 Python Multiparadigm –Imperative –Object-orientd –Functional Scripting language Dynamically typed Builtin types: int, float, infinite precision integers, complex numbers, strings,... Data structure –Lists: [1, [2, "allen"], "bob", ] –Hashes: ["Tucker": "Bowdoin", "Noonan": "W&M"] –Tuples: (238111, "allen", "bob") Strings: viewed as list of characters –List operators work on strings

CSC321: Programming Languages13-40 Python "less forgiving" than Perl –Cannot do string operations on numbers –Or vice versa –Cannot index past end of list –Must append to end of a list Statements fairly conventional Indentation used for compound statements No $var as in Perl (bareword error) Reference semantics used for assignment Multiple inheritance All methods and instance variables are public. Variables must be set before being referenced. Run time type identification Reflection