Presentation is loading. Please wait.

Presentation is loading. Please wait.

ICE1341 Programming Languages Spring 2005 Lecture #19 Lecture #19 In-Young Ko iko.AT. icu.ac.kr iko.AT. icu.ac.kr Information and Communications University.

Similar presentations


Presentation on theme: "ICE1341 Programming Languages Spring 2005 Lecture #19 Lecture #19 In-Young Ko iko.AT. icu.ac.kr iko.AT. icu.ac.kr Information and Communications University."— Presentation transcript:

1

2 ICE1341 Programming Languages Spring 2005 Lecture #19 Lecture #19 In-Young Ko iko.AT. icu.ac.kr iko.AT. icu.ac.kr Information and Communications University (ICU)

3 Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Announcements Project Presentation Sessions (May 26) Project Presentation Sessions (May 26) 7 minutes for each team (in English) 7 minutes for each team (in English) Explain design goals of your team Explain design goals of your team Present special features in your language Present special features in your language Show a sample program Show a sample program Explain pros and cons of your language Explain pros and cons of your language Discuss lessons learned Discuss lessons learned

4 Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Announcements Final Exam: Final Exam: Dater & Time: Monday May 30, :00PM-6:00PM Dater & Time: Monday May 30, :00PM-6:00PM Allowed to bring a hand-written ‘cheat sheet’ (A4 size, double-sided) Allowed to bring a hand-written ‘cheat sheet’ (A4 size, double-sided) Covering: Covering: Chapter 7 – 11 Chapter 7 – 11 Chapter 12 except 12.7, 12.8 and 12.9 Chapter 12 except 12.7, 12.8 and 12.9 Chapter 13 except 13.7 Chapter 13 except 13.7 Chapter 14 Chapter 14

5 Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Implementing Subprograms Implementing Subprograms Implementing Program Blocks Implementing Program Blocks Implementing Dynamic Scoping Implementing Dynamic Scoping Abstraction and Encapsulation Abstraction and Encapsulation Abstract Data Types Abstract Data Types Encapsulation Constructs (self-study) Encapsulation Constructs (self-study) Naming Encapsulations (self-study) Naming Encapsulations (self-study) Last Lecture

6 Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University This Lecture Object-oriented Programming Languages Object-oriented Programming Languages Software Reuse Software Reuse Overriden Methods Overriden Methods Abstract Classes and Methods Abstract Classes and Methods Design Issues for OOPLs Design Issues for OOPLs Object-oriented Processing of XML Data Object-oriented Processing of XML Data

7 Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Chapter 12 Object-oriented Programming Languages

8 Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Software Reuse How can we increase software productivity? How can we increase software productivity?  By improving software reusability How can we improve software reusability? How can we improve software reusability?  By supporting abstraction mechanisms  Abstract data types (ADTs) are the units to be reused What are the problems in using ADTs What are the problems in using ADTs Difficult to modify – requires modifications on both the data type and all client programs Difficult to modify – requires modifications on both the data type and all client programs Hard to relate similar ADTs that are generated from one ADT Hard to relate similar ADTs that are generated from one ADT Any solutions?  Inheritance mechanism Any solutions?  Inheritance mechanism

9 Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University An Inheritance Hierarchy Inheritance Relation Friend - nickname : String … + getNickname() : String … FamilyMember - relation : String … + getRelation() : String … Person - name : String - String … > Person(name: String) > Person(name: String) + getName() : String + setName(name: String) … SchoolFriend - schoolYear : int … + getSchoolYear() : int … HometownFriend - place : String … + getPlace() : String …

10 Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University An Inheritance Hierarchy List add(int, Object), get(int): Object remove(int), clear() size(): int, contains(Object): boolean Linked List add(int, Object), get(int): Object remove(int), clear() size(): int, contains(Object): boolean addFirst(Object), addLast(Object) getFirst(): Object, getLast(): Object removeFirst(), removeLast() Vector add(int, Object), get(int): Object remove(int), clear() size(): int, contains(Object): boolean capacity(): int, setSize(int) Stack empty(): boolean, top(): Object push(Object), pop(): Object

11 Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Object-Oriented Programming (OOP) A programming paradigm that centers around “objects,” which are similar to real-world objects A programming paradigm that centers around “objects,” which are similar to real-world objects Programmers see the execution of a program as a collection of dialoguing objects [Wikipedia] Programmers see the execution of a program as a collection of dialoguing objects [Wikipedia] OOP languages (OOPL) began in 1960s with the Simular project, which incorporated the notions of objects and inheritance OOP languages (OOPL) began in 1960s with the Simular project, which incorporated the notions of objects and inheritance OOPLs: Smalltalk, Ada 95, CLOS, Scheme, Eiffel, C++, Java OOPLs: Smalltalk, Ada 95, CLOS, Scheme, Eiffel, C++, Java

12 Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University OOP uses an abstracted real world object OOP uses an abstracted real world object A set of data A set of data A set of methods (functions) to manipulate the data A set of methods (functions) to manipulate the data An analogy of objects: eggs that exchange messages An analogy of objects: eggs that exchange messages Data Methods Message Object Object-Oriented Programming (OOP)

13 Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University OOP Definitions Classes: Abstract data types Classes: Abstract data types Objects: Instances of a class Objects: Instances of a class Subclass (Derived Class): A class that inherits another class Subclass (Derived Class): A class that inherits another class Superclass (Parent Class): The class from which another classes inherit Superclass (Parent Class): The class from which another classes inherit Methods: Subprograms that define operations on objects Methods: Subprograms that define operations on objects Messages: Calls to methods (Destination object + Method name) Messages: Calls to methods (Destination object + Method name) Message Protocol (Message Interface): The entire collection of methods of an object Message Protocol (Message Interface): The entire collection of methods of an object

14 Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Access Control and Overriden Methods Inheritance can be complicated by access controls to encapsulated entities Inheritance can be complicated by access controls to encapsulated entities A class can hide entities from its subclasses (Private) A class can hide entities from its subclasses (Private) A class can hide entities from its clients A class can hide entities from its clients A class can also hide entities from its clients while allowing its subclasses to see them (Protected) A class can also hide entities from its clients while allowing its subclasses to see them (Protected) Besides inheriting methods as is, a class can modify an inherited method Besides inheriting methods as is, a class can modify an inherited method The new one overrides the inherited one The new one overrides the inherited one The method in the parent is overriden The method in the parent is overriden * AW Lecture Notes

15 Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Examples of Overriden Methods List add(int, Object), get(int): Object remove(int), clear() size(): int, contains(Object): boolean Linked List add(int, Object), get(int): Object remove(int), clear() size(): int, contains(Object): boolean addFirst(Object), addLast(Object) getFirst(): Object, getLast(): Object removeFirst(), removeLast() Vector add(int, Object), get(int): Object remove(int), clear() size(): int, contains(Object): boolean capacity(): int, setSize(int) Stack empty(): boolean, top(): Object push(Object), pop(): Object Methods that override the ones in List

16 Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Kinds of Methods and Variables There are two kinds of variables in a class: There are two kinds of variables in a class: Class variables – belong to a class (e.g., static variables in Java) Class variables – belong to a class (e.g., static variables in Java) Instance variables – belong to an instance Instance variables – belong to an instance There are two kinds of methods in a class: There are two kinds of methods in a class: Class methods – accept messages to the class (e.g., static methods in Java) Class methods – accept messages to the class (e.g., static methods in Java) Instance methods – accept messages to objects Instance methods – accept messages to objects * AW Lecture Notes

17 Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Abstract Classes and Methods Abstract method: one that does not include a definition (it only defines a protocol) Abstract method: one that does not include a definition (it only defines a protocol) Abstract class is one that includes at least one virtual method Abstract class is one that includes at least one virtual method An abstract class cannot be instantiated An abstract class cannot be instantiated public interface List { public boolean add(Object); public boolean remove(Object); public int size(); public void clear(); …} public abstract class AbstractList implements List { … // Partial implementation of List …} public class Vector extends AbstractList { …} Java

18 Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Design Issues for OOPL (1) The Exclusivity of Objects The Exclusivity of Objects Everything is an object - Elegance and uniformity, but slow operations on simple objects (e.g., float) Everything is an object - Elegance and uniformity, but slow operations on simple objects (e.g., float) Add objects to a complete typing system – Fast operations on simple objects, but results in a confusing type system (two kinds of entities, e.g., Integer and int in Java) Add objects to a complete typing system – Fast operations on simple objects, but results in a confusing type system (two kinds of entities, e.g., Integer and int in Java) Are Subclasses Subtypes? Are Subclasses Subtypes? Does an “is-a” relationship hold between a parent class object and an object of the subclass? Does an “is-a” relationship hold between a parent class object and an object of the subclass? e.g., Ada – subtype Small_Int is Integer range ; e.g., Ada – subtype Small_Int is Integer range ; * AW Lecture Notes

19 Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Design Issues for OOPL (2) Type Checking and Polymorphism Polymorphism may require dynamic type checking of parameters and the return value Polymorphism may require dynamic type checking of parameters and the return value Dynamic type checking is costly and delays error detection Dynamic type checking is costly and delays error detection If overriding methods are restricted to having the same parameter types and return type, the checking can be static If overriding methods are restricted to having the same parameter types and return type, the checking can be static e.g., IntVector v = new IntVector(); v.add(new Integer(20)); class Vector { void add(Object item) { … } … } class IntegerVector extends Vector { void add(Integer item) { … } … } class IntVector extends IntegerVector { void add(int item) { … } … } * AW Lecture Notes

20 Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Design Issues for OOPL (3) Single vs. Multiple Inheritance Single vs. Multiple Inheritance Disadvantages of multiple inheritance: Disadvantages of multiple inheritance: Language and implementation complexity (in part due to name collisions) Language and implementation complexity (in part due to name collisions) Potential inefficiency - dynamic binding costs more with multiple inheritance Potential inefficiency - dynamic binding costs more with multiple inheritance Allocation and Deallocation of Objects Allocation and Deallocation of Objects From where are objects allocated? – In the heap or stack area? From where are objects allocated? – In the heap or stack area? Is deallocation explicit or implicit? – e.g., Garbage collectors Is deallocation explicit or implicit? – e.g., Garbage collectors * AW Lecture Notes

21 Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Object-Oriented Processing of XML Data

22 Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University A Sample XML Document Prog. Lang. Prog. Lang. ICE1341 ICE1341 K.D. Ko K.D. Ko C.S. Lee C.S. Lee

23 Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University XML Processors XML Document Databases XML Parser DTD/ XMLSchema XSL Description XSL Processor XML Grammar (Structure) Validation DOM Objects HTML Presentation Parsing Events DOM API SAX API

24 Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University XML APIs SAX (Simple API for XML) – XML-DEV SAX (Simple API for XML) – XML-DEV Stream-based Access Interface (Sequential Access) Stream-based Access Interface (Sequential Access) Notifies an application of a stream of parsing events Notifies an application of a stream of parsing events Needs a Content Handler to handle the parsing events (e.g., start and end of an element) Needs a Content Handler to handle the parsing events (e.g., start and end of an element) Appropriate to handle a large XML document Appropriate to handle a large XML document DOM (Document Object Model) – W3C DOM (Document Object Model) – W3C Object-oriented Access Interface (Random Access) Object-oriented Access Interface (Random Access) Builds a tree of nodes based on the structure and information in an XML document Builds a tree of nodes based on the structure and information in an XML document Types of nodes: Document, Element, Attr, … Types of nodes: Document, Element, Attr, …

25 Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University DOM Representation Prog. Lang. Prog. Lang. ICE1341 ICE1341 Y.K. Ko Y.K. Ko D.W. Kim D.W. Kim XML Document DOM Representation Document (Root Node) Elements (Child Nodes) Node Values (Text Nodes)

26 Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Java API Hierarchy for DOM Node getChildNodes(): NodeList getAttributes(): NamedNodeMap getNodeName(): String getNodeValue(): String appendChild(Node) removeChild(Node) setNodeValue(String) Attr getName(): String getValue(): String setValue(String) CharacterData getData(): String getLength(): int setData(String) Document createAttribute(String): Attr createElement(String): Element createTextNode(String): Text getDocumentElement(): Element getElementByTagName(String): NodeList Element getAttribute(String): String getTagName(): String Text splitText(int): Text Comment

27 Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University An Example of Creating DOM Objects from an XML File try { DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); Document doc = Document doc = docBuilder.parse(new File("sample.xml")); Element rootEle = doc.getDocumentElement(); Element rootEle = doc.getDocumentElement(); NodeList children = rootEle.getChildNodes(); NodeList children = rootEle.getChildNodes(); for (int i = 0; i < children.getLength(); i++) { for (int i = 0; i < children.getLength(); i++) { Node subEle = children.item(i); Node subEle = children.item(i); … } } catch(Exception e) { e.printStackTrace(); }

28 Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Related Materials W3C Document Object Model (www.w3.org/DOM/) W3C Document Object Model (www.w3.org/DOM/)www.w3.org/DOM/ A simple way to read an XML file in Java (www.developerfusion.com/show/2064/) A simple way to read an XML file in Java (www.developerfusion.com/show/2064/)www.developerfusion.com/show/2064/ Working with XML (java.sun.com/xml/jaxp/dist/1.0.1/docs/tutorial/index.html) Working with XML (java.sun.com/xml/jaxp/dist/1.0.1/docs/tutorial/index.html)java.sun.com/xml/jaxp/dist/1.0.1/docs/tutorial/index.html Java Technology and XML FAQs (java.sun.com/xml/faq.html) Java Technology and XML FAQs (java.sun.com/xml/faq.html)java.sun.com/xml/faq.html Java API Manual (java.sun.com/j2se/1.4.2/docs/api/) Java API Manual (java.sun.com/j2se/1.4.2/docs/api/)java.sun.com/j2se/1.4.2/docs/api/ See org.w3c.dom and javax.xml.parsers See org.w3c.dom and javax.xml.parsers XML.org (www.xml.org) XML.org (www.xml.org)www.xml.org

29 Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Smalltalk – Characteristics The 1 st language to include complete support for the object-oriented programming paradigm The 1 st language to include complete support for the object-oriented programming paradigm Everything is an object Everything is an object All binding of messages to methods is dynamic All binding of messages to methods is dynamic Because all variables are typeless, methods are all polymorphic Because all variables are typeless, methods are all polymorphic All subclasses are subtypes All subclasses are subtypes No multiple inheritance No multiple inheritance

30 Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Smalltalk – Evaluation The syntax of the language is simple and regular The syntax of the language is simple and regular Slow compared with conventional compiled imperative languages Slow compared with conventional compiled imperative languages Dynamic binding allows type errors to go undetected until run time Dynamic binding allows type errors to go undetected until run time Greatest impact: advancement of OOP Greatest impact: advancement of OOP

31 Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University C++ – Characteristics A hybrid language: procedural + OOP A hybrid language: procedural + OOP Access controls for members: Access controls for members: Private: visible only in the class and friends Private: visible only in the class and friends Protected: visible in the class and in subclasses, but not clients Protected: visible in the class and in subclasses, but not clients Public: visible in subclasses and clients Public: visible in subclasses and clients Disallows subclasses from being subtypes Disallows subclasses from being subtypes Inheritance Inheritance Private derivation – inherited public and protected members are private in the subclasses Private derivation – inherited public and protected members are private in the subclasses Public derivation – public and protected members are also public and protected in subclasses Public derivation – public and protected members are also public and protected in subclasses

32 Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University C++ – Private & Public Derivations class base_class { private: int a;float x; private: int a;float x; protected:int b;float y; protected:int b;float y; public:int c;float z; public:int c;float z;}; class subclass_1 : public base_class { // In this, b and y are protected and c and z are public // In this, b and y are protected and c and z are public}; class subclass_2 : private base_class { // In this, b, y, c, and z are private, and no derived class // In this, b, y, c, and z are private, and no derived class // has access to any member of base_class // has access to any member of base_class}; * AW Lecture Notes

33 Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University C++ – Reexportation A member that is not accessible in a subclass (because of private derivation) can be declared to be visible there using the scope resolution operator ( :: ) A member that is not accessible in a subclass (because of private derivation) can be declared to be visible there using the scope resolution operator ( :: ) e.g., class subclass_3 : private base_class { base_class :: c; base_class :: c; … … } } Motivation for using private derivation: a derived class adds some new members, but does not want its clients to see the members of the parent class, even though they had to be public in the parent class definition Motivation for using private derivation: a derived class adds some new members, but does not want its clients to see the members of the parent class, even though they had to be public in the parent class definition * AW Lecture Notes

34 Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University C++ – Multiple Inheritance If there are two inherited members with the same name, they can both be reference using the scope resolution operator If there are two inherited members with the same name, they can both be reference using the scope resolution operator class A { public: int n; float f; public: int n; float f;}; class B { public: int n; char c; public: int n; char c;}; class C : public A, public B { … f = 10.23; c = ‘$’; f = 10.23; c = ‘$’; A::n = 10; B::n = 20; A::n = 10; B::n = 20;};

35 Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University C++ – Dynamic Binding Virtual method: a method that can be called through polymorphic variables and dynamically bound to messages public class shape { public: virtual void draw() = 0; … public: virtual void draw() = 0; …} public class circle : public shape { public: virtual void draw() { … } … public: virtual void draw() { … } …} public class rectangle : public shape { public: virtual void draw() { … } … public: virtual void draw() { … } …} public class square : public rectangle { public: virtual void draw() { … } … public: virtual void draw() { … } …}… square s; rectangle r; shape &ref_shape = s; ref_shape.draw(); r.draw(); Dynamically bound to draw in square Dynamically bound to draw in rectangle Pure virtual function

36 Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Java – Characteristics All data are objects except the primitive types All data are objects except the primitive types All primitive types have wrapper classes that store one data value (e.g., new Integer(10)) All primitive types have wrapper classes that store one data value (e.g., new Integer(10)) All objects are heap-dynamic, are referenced through reference variables, and most are allocated with new All objects are heap-dynamic, are referenced through reference variables, and most are allocated with new Single inheritance only, but interfaces provide a version of multiple inheritance Single inheritance only, but interfaces provide a version of multiple inheritance Methods can be final (cannot be overriden) Methods can be final (cannot be overriden) All messages are dynamically bound to methods, unless the method is final All messages are dynamically bound to methods, unless the method is final

37 Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Implementing OO Constructs Class Instance Record (CIR): A storage structure for the instance variables of class instances Class Instance Record (CIR): A storage structure for the instance variables of class instances Virtual Method Table (VMT): A storage structure for the list of dynamically bound methods that can be called from an instance of a class Virtual Method Table (VMT): A storage structure for the list of dynamically bound methods that can be called from an instance of a class public class A { public int a, b; public int a, b; public void draw() { … } public void draw() { … } public int area() { … } public int area() { … }} Public class B extends A { public int c, d; public int c, d; public void draw() { … } public void draw() { … } public void sift() { … } public void sift() { … }} vtable pointer a b c d CIR for B VMT for B A’s area B’s draw B’s sift


Download ppt "ICE1341 Programming Languages Spring 2005 Lecture #19 Lecture #19 In-Young Ko iko.AT. icu.ac.kr iko.AT. icu.ac.kr Information and Communications University."

Similar presentations


Ads by Google