Chapter SixteenModern Programming Languages1 Object Orientation.

Slides:



Advertisements
Similar presentations
Chapter 1 Object-Oriented Concepts. A class consists of variables called fields together with functions called methods that act on those fields.
Advertisements

Object Orientation Chapter SixteenModern Programming Languages, 2nd ed.1 Spring 2012.
Object Orientation Chapter SixteenModern Programming Languages, 2nd ed.1.
A Second Look At Java Chapter FifteenModern Programming Languages, 2nd ed.1.
Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 3 Advanced Object-Oriented Concepts.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Object-Oriented PHP (1)
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
Data Abstraction and Object- Oriented Programming CS351 – Programming Paradigms.
Chapter 10 Classes Continued
Abstract Data Types and Encapsulation Concepts
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Inheritance. Types of Inheritance Implementation inheritance means that a type derives from a base type, taking all the base type’s member fields and.
(c) University of Washington04-1 CSC 143 Java Inheritance Example (Review)
UFCEUS-20-2 : Web Programming Lecture 5 : Object Oriented PHP (1)
Programming Languages and Paradigms Object-Oriented Programming.
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
Chapter TwelveModern Programming Languages1 Memory Locations For Variables.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
1 Chapter 10: Data Abstraction and Object Orientation Aaron Bloomfield CS 415 Fall 2005.
Polymorphism, Inheritance Pt. 1 COMP 401, Fall 2014 Lecture 7 9/9/2014.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
CS200 Algorithms and Data StructuresColorado State University Part 4. Advanced Java Topics Instructor: Sangmi Pallickara
APCS Java AB 2004 Review of CS1 and CS2 Review for AP test #1 Sources: 2003 Workshop notes from Chris Nevison (Colgate University) AP Study Guide to go.
Chapter 2 Introducing Interfaces Summary prepared by Kirk Scott.
OOP and Dynamic Method Binding Chapter 9. Object Oriented Programming Skipping most of this chapter Focus on 9.4, Dynamic method binding – Polymorphism.
Chapter 12 Support for Object oriented Programming.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Programming in Java CSCI-2220 Object Oriented Programming.
Simple Classes. ADTs A specification for a real world data item –defines types and valid ranges –defines valid operations on the data. Specification is.
Summing Up Object Oriented Design. Four Major Components: Abstraction modeling real-life entities by essential information only Encapsulation clustering.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
Lecture 10 Concepts of Programming Languages Arne Kutzner Hanyang University / Seoul Korea.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
Object-Oriented Programming Chapter Chapter
 Objects versus Class  Three main concepts of OOP ◦ Encapsulation ◦ Inheritance ◦ Polymorphism  Method ◦ Parameterized ◦ Value-Returning.
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,
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
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.
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.
Classes, Interfaces and Packages
Object-Oriented Programming “The Rest of the Story”, CS 4450 – Chapter 16.
1 Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
OOP Basics Classes & Methods (c) IDMS/SQL News
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
CSC 243 – Java Programming, Spring, 2014 Week 4, Interfaces, Derived Classes, and Abstract Classes.
ISBN Chapter 12 Support for Object-Oriented Programming.
CSC 243 – Java Programming, Fall, 2008 Tuesday, September 30, end of week 5, Interfaces, Derived Classes, and Abstract Classes.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Modern Programming Tools And Techniques-I
Modern Programming Languages
CS 215 Final Review Ismail abumuhfouz Fall 2014.
Inheritance and Polymorphism
Types of Programming Languages
Object-Oriented PHP (1)
Lecture 10 Concepts of Programming Languages
Presentation transcript:

Chapter SixteenModern Programming Languages1 Object Orientation

Chapter SixteenModern Programming Languages2 Definitions n Give definitions for the following: – Object-oriented language – Object-oriented programming n Then again, why bother?

Chapter SixteenModern Programming Languages3 Observations n Object-oriented programming is not the same as programming in an object-oriented language n Object-oriented languages are not all like Java

Chapter SixteenModern Programming Languages4 Outline n 16.2 Object-oriented programming – OO in ML – Non-OO in Java n 16.3 Object-oriented language features – Classes – Prototypes – Inheritance – Encapsulation – Polymorphism

Chapter SixteenModern Programming Languages5 public class Node { private String data; private Node link; public Node(String theData, Node theLink) { data = theData; link = theLink; } public String getData() { return data; } public Node getLink() { return link; } } A previous Java example: a node used to build a stack of strings

Chapter SixteenModern Programming Languages6 Node Class Two fields, data and link One constructor that sets data and link Two methods: getData and getLink n In the abstract, an object takes a message (“get data”, “get link”) and produces a response (a String or another object) An object is a bit like a function of type message->response

Chapter SixteenModern Programming Languages7 datatype message = GetData | GetLink; datatype response = Data of string | Object of message -> response; fun node data link GetData = Data data | node data link GetLink = Object link; Same OO idea in ML. We have a type for messages and a type for responses. To construct a node we call node, passing the first two parameters. Result is a function of type message->response.

Chapter SixteenModern Programming Languages8 - val n1 = node "Hello" null; val n1 = fn : message -> response - val n2 = node "world" n1; val n2 = fn : message -> response - n1 GetData; val it = Data "Hello" : response - n2 GetData; val it = Data "world" : response Node Examples n Objects responding to messages null has to be something of the object type ( message->response ); we could use fun null _ = Data "null";

Chapter SixteenModern Programming Languages9 Stack Class One field, top Three methods: hasMore, add, remove Implemented using a linked list of node objects

Chapter SixteenModern Programming Languages10 datatype message = IsNull | Add of string | HasMore | Remove | GetData | GetLink; datatype response = Pred of bool | Data of string | Removed of (message -> response) * string | Object of message -> response; fun root _ = Pred false; Expanded vocabulary of messages and responses, for both node and stack Root class handles all messages by returning Pred false

Chapter SixteenModern Programming Languages11 fun null IsNull = Pred true | null message = root message; fun node data link GetData = Data data | node data link GetLink = Object link | node _ _ message = root message; fun stack top HasMore = let val Pred(p) = top IsNull in Pred(not p) end | stack top (Add data) = Object(stack (node data top)) | stack top Remove = let val Object(next) = top GetLink val Data(data) = top GetData in Removed(stack next, data) end | stack _ message = root message;

Chapter SixteenModern Programming Languages12 - val a = stack null; val a = fn : message -> response - val Object(b) = a (Add "the plow."); val b = fn : message -> response - val Object(c) = b (Add "forgives "); val c = fn : message -> response - val Object(d) = c (Add "The cut worm "); val d = fn : message -> response - val Removed(e,s1) = d Remove; val e = fn : message -> response val s1 = "The cut worm " : string - val Removed(f,s2) = e Remove; val f = fn : message -> response val s2 = "forgives " : string - val Removed(_,s3) = f Remove; val s3 = "the plow." : string - s1^s2^s3; val it = "The cut worm forgives the plow." : string

Chapter SixteenModern Programming Languages13 Inheritance, Sort Of Here is a peekableStack like the one in Java from Chapter Fifteen: n This style is rather like a Smalltalk system – Message passing – Messages not statically typed – Unhandled messages passed back to superclass fun peekableStack top Peek = top GetData | peekableStack top message = stack top message;

Chapter SixteenModern Programming Languages14 Thoughts n Obviously, not a good way to use ML – Messages and responses not properly typed – Skipped many “binding not exhaustive” messages from the previous example n (Objective CAML is a dialect that integrates OO features into ML) n The point is: it’s possible n OO programming is not the same as programming in an OO language

Chapter SixteenModern Programming Languages15 Outline n Object-oriented programming – OO in ML – Non-OO in Java n Object-oriented language features – Classes – Prototypes – Inheritance – Encapsulation – Polymorphism

Chapter SixteenModern Programming Languages16 Java n Java is better than ML at supporting an object-oriented style of programming n But using Java is no guarantee of object- orientation – Can use static methods – Can put all code in one big class – Can use classes as records—public fields and no methods, like C structures

Chapter SixteenModern Programming Languages17 public class Node { public String data; // Each node has a String... public Node link; //...and a link to the next Node } public class Stack{ public Node top; // The top node in the stack } Classes Used As Records

Chapter SixteenModern Programming Languages18 public class Main { private static void add(Stack s, String data) { Node n = new Node(); n.data = data; n.link = s.top; s.top = n; } private static boolean hasMore(Stack s) { return (s.top!=null); } private static String remove(Stack s) { Node n = s.top; s.top = n.link; return n.data; } … } A Non-OO Stack Note direct references to public fields—no methods required, data and code completely separate

Chapter SixteenModern Programming Languages19 Polymorphism In Chapter Fifteen: Worklist interface implemented by Stack, Queue, etc. n There is a common trick to support this kind of thing in non-OO solutions Each record starts with an element of an enumeration, identifying what kind of Worklist it is…

Chapter SixteenModern Programming Languages20 public class Worklist { public static final int STACK = 0; public static final int QUEUE = 1; public static final int PRIORITYQUEUE = 2; public int type; // one of the above Worklist types public Node front; // front Node in the list public Node rear; // unused when type==STACK public int length; // unused when type==STACK } A Non-OO Worklist The type field says what kind of Worklist it is. Meanings of other fields depend on type. Methods that manipulate Worklist records must branch on type …

Chapter SixteenModern Programming Languages21 private static void add(Worklist w, String data) { if (w.type==Worklist.STACK) { Node n = new Node(); n.data = data; n.link = w.front; w.front = n; } else if (w.type==Worklist.QUEUE) { the implementation of add for queues } else if (w.type==Worklist.PRIORITYQUEUE) { the implementation of add for priority queues } } Branch On Type Every method that operates on a Worklist will have to repeat this branching pattern

Chapter SixteenModern Programming Languages22 Drawbacks n Repeating the branching code is tedious and error-prone n Depending on the language, there may be no way to avoid wasting space if different kinds of records require different fields n Some common maintenance tasks are hard—like adding a new kind of record

Chapter SixteenModern Programming Languages23 OO Advantages n When you call an interface method, language system automatically dispatches to the right implementation for the object n Different implementations of an interface do not have to share fields n Adding a new class that implements an interface is easy—no need to modify existing code

Chapter SixteenModern Programming Languages24 Thoughts n OO programming is not the same as programming in an OO language – Can be done in a non-OO language – Can be avoided in an OO language n Usually, an OO language and an OO programming style do and should go together – You usually get a worse ML design by using an OO style (hint: avoid “binding not exhaustive”) – You usually get a better Java design by using an OO style (hint: avoid enumerations)

Chapter SixteenModern Programming Languages25 Outline n 16.2 Object-oriented programming – OO in ML – Non-OO in Java n 16.3 Object-oriented language features – Classes – Prototypes – Inheritance – Encapsulation – Polymorphism

Chapter SixteenModern Programming Languages26 Classes n Most OO languages, including Java, have some kind of class construct n Classes serve a variety of purposes, depending on the language: – Group fields and methods together – Are instantiable: the running program can create as many objects of a class as it needs – Serve as the unit of inheritance: derived class inherits from base class or classes

Chapter SixteenModern Programming Languages27 Classes n More purposes: – Serve as a type: objects (or references to them) can have a class or superclass name as their static type – House static fields and methods: one per class, not one per instance – Serve as a labeled namespace; control the visibility of contents outside the class definition

Chapter SixteenModern Programming Languages28 Without Classes n Imagine an OO language with no classes n With classes, you create objects by instantiating a class n Without classes, you could create an object from scratch by listing all its methods and fields on the spot n Or, you could clone an existing prototype object and then modify parts of it

Chapter SixteenModern Programming Languages29 x = new Stack(); x = { private Node top = null; public boolean hasMore() { return (top!=null); } public String remove() { Node n = top; top = n.getLink(); return n.getData(); } … } x = y.clone(); x.top = null; With classes: instantiation Without classes: raw object creation Without classes: prototype cloning

Chapter SixteenModern Programming Languages30 Prototypes n A prototype is an object that is copied to make similar objects n When making copies, a program can modify the values of fields, and can add or remove fields and methods n Prototype-based languages (like Self) use this concept instead of classes

Chapter SixteenModern Programming Languages31 Without Classes n Instantiation is only one use of classes n Other things prototype-based languages must do without: – Classes as types: most prototype-based languages are dynamically typed – Inheritance: prototype-based languages use a related dynamic technique called delegation

Chapter SixteenModern Programming Languages32 Inheritance n Simple enough in outline – Set up a relationship between two classes: a derived class and a base class – Derived class gets things from the base class n But what a derived class gets from the base class (or classes) depends on the language…

Chapter SixteenModern Programming Languages33 Inheritance Questions n More than one base class allowed? – Single inheritance: Smalltalk, Java – Multiple inheritance: C++, CLOS, Eiffel n Forced to inherit everything? – Java: derived class inherits all methods, fields – Sather: derived class can rename inherited methods (useful for multiple inheritance), or just undefine them

Chapter SixteenModern Programming Languages34 Inheritance Questions n Universal base class? – A class from which all inherit: Java’s Object – No such class: C++ n Specification inherited? – Method obligations, as in Java – More specification: invariants, as in Eiffel n Types inherited? – Java: all types of the base class

Chapter SixteenModern Programming Languages35 Inheritance Questions n Overriding, hiding, etc.? – Java, roughly (skipping many details): Constructors can access base-class constructors with super ; implicit call of no-arg super constructor New instance method of the same name and type overrides inherited one; overridden one can be called using super New field or static method hides inherited ones; still accessible using super or base class static types n Languages differ considerably

Chapter SixteenModern Programming Languages36 Encapsulation n Found in virtually all modern programming languages, not just OO ones n Encapsulated program parts: – Present a controlled interface – Hide everything else n In OO languages, objects are encapsulated n Different languages do it differently

Chapter SixteenModern Programming Languages37 Visibility Of Fields And Methods n Java: four levels of visibility – private : only within class – Default access: throughout package – protected : package + derived classes – public : everywhere n Some OO languages (Smalltalk, LOOPS, Self) have less control: everything public n Others have more: in Eiffel, features can be exposed to a specific set of client classes

Chapter SixteenModern Programming Languages38 Polymorphism n Found in many languages, not just OO ones n Special variation in many OO languages: – When different classes have methods of the same name and type, like a stack class and a queue class that both have an add method – When language permits a call of that method in contexts where the class of the object is not known statically

Chapter SixteenModern Programming Languages39 Example: Java Here, Drawable is an interface Class of object referred to by d is not known at compile time public static void flashoff(Drawable d, int k) { for (int i = 0; i < k; i++) { d.show(0,0); d.hide(); } }

Chapter SixteenModern Programming Languages40 Dynamic Dispatch n In Java, static type of the reference may be a superclass or interface of the actual class n At runtime, the language system must find the right method for the actual class n That’s dynamic dispatch: the hidden, implicit branch-on-class to implement method calls n Optional in C++; always used in Java and most other OO languages

Chapter SixteenModern Programming Languages41 Implementation And Type n In Java, two mechanisms: – A class inherits both types and implementation from its base class – A class gets additional types (but no implementation) by implementing interfaces n Partially separates inheritance of implementation and inheritance of type n Other OO languages differ in how much they separate these two

Chapter SixteenModern Programming Languages42 Implementation And Type n In C++, no separation: – One mechanism for general inheritance – For inheriting type only, you can use an abstract base class with no implementations n In Sather, complete separation: – A class can declare that it includes another class, inheriting implementation but not type – A class can declare that it is a subclass of an abstract class, inheriting type but not implementation (like Java interfaces)

Chapter SixteenModern Programming Languages43 About Dynamic Typing n Some OO languages use dynamic typing: Smalltalk, Self n An object may or may not be able to respond to a particular message—no compile-time check (like our ML trick) n Total freedom: program can try using any method for any object n Polymorphism is not relevant here

Chapter SixteenModern Programming Languages44 Conclusion n Today, a cosmopolitan perspective: – Object-oriented programming is not the same as programming in an object-oriented language – Object-oriented languages are not all like Java n There is no single OO programming style or set of OO language features: they are often debated and they are evolving n Be skeptical of definitions!