Lecture 4 Java Interfaces (review of inheritance and abstract classes) The XML DOM Java Examples Homework 3.

Slides:



Advertisements
Similar presentations
SUMMARY: abstract classes and interfaces 1 Make a class abstract so instances of it cannot be created. Make a method abstract so it must be overridden.
Advertisements

OOP: Inheritance By: Lamiaa Said.
Inheritance Inheritance Reserved word protected Reserved word super
ACM/JETT Workshop - August 4-5, :Inheritance and Interfaces.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Summer A-2000, Project Course-- Carnegie Mellon University 1 Financial Engineering Project Course.
14-Jun-15 DOM. SAX and DOM SAX and DOM are standards for XML parsers-- program APIs to read and interpret XML files DOM is a W3C standard SAX is an ad-hoc.
Financial Engineering Project Course. Lecture 3 Object Oriented Design Inheritance Abstract Base Classes Polymorphism Using XML to represent the swap.
Summer A-2000, Project Course-- Carnegie Mellon University 1 Financial Engineering Project Course.
Inheritance and Polymorphism Recitation – 10/(16,17)/2008 CS 180 Department of Computer Science, Purdue University.
CS 106 Introduction to Computer Science I 04 / 21 / 2008 Instructor: Michael Eckmann.
Processing XML Processing XML using XSLT Processing XML documents with Java (DOM) Next week -- Processing XML documents with Java (SAX)
Chapter 10 Classes Continued
CS 106 Introduction to Computer Science I 04 / 28 / 2010 Instructor: Michael Eckmann.
Apache DOM Parser©zwzOctober 24, 2002 Wenzhong Zhao Department of Computer Science The University of Kentucky.
Abstract Classes b b An abstract class is a placeholder in a class hierarchy that represents a generic concept b b An abstract class cannot be instantiated.
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
Inheritance and Subclasses in Java CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo de Manila University.
CSE 6331 © Leonidas Fegaras XML Tools1 XML Tools Leonidas Fegaras.
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
CMSC 202 Interfaces. 11/20102 Classes and Methods When a class defines its methods as public, it describes how the class user interacts with the method.
MIT AITI 2002 Abstract Classes, Interfaces. Abstract Classes What is an abstract class? An abstract class is a class in which one or more methods is declared,
CSE 6331 © Leonidas Fegaras XML Tools1 XML Tools.
1 Java Inheritance. 2 Inheritance On the surface, inheritance is a code re-use issue. –we can extend code that is already written in a manageable manner.
Programming With Java ICS201 University Of Ha’il1 Chapter 8 Polymorphism and Abstract Classes.
DOM Robin Burke ECT 360. Outline XHTML in Schema JavaScript DOM (MSXML) Loading/Parsing Transforming parameter passing DOM operations extracting data.
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.
Programming With Java ICS Chapter 8 Polymorphism.
Chapter 9 Object-Oriented Software Development F Software Development Process F Analyze Relationships Among Objects F Class Development F Class Design.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
Outline §Review of the last class l class variables and methods l method overloading and overriding §Inheritance and polymorphism l polymorphism l abstract.
Chapter 8 Inheritance Part 2. © 2004 Pearson Addison-Wesley. All rights reserved2/23 Outline Creating Subclasses Overriding Methods Class Hierarchies.
CS 106 Introduction to Computer Science I 04 / 23 / 2010 Instructor: Michael Eckmann.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Topic 4 Inheritance.
Programming in Java CSCI-2220 Object Oriented Programming.
SDPLNotes 3.2: DOM1 3.2 Document Object Model (DOM) n How to provide uniform access to structured documents in diverse applications (parsers, browsers,
Apache DOM Parser©zwzOctober 24, 2002 Wenzhong Zhao Department of Computer Science The University of Kentucky.
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
CS 61B Data Structures and Programming Methodology July 2, 2008 David Sun.
1 Abstract Classes and Interfaces. 2 The abstract Modifier  The abstract class –Cannot be instantiated –Should be extended and implemented in subclasses.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
CS 106 Introduction to Computer Science I 04 / 18 / 2008 Instructor: Michael Eckmann.
Interfaces F What is an Interface? F Creating an Interface F Implementing an Interface F What is Marker Interface?
Inheritance and Subclasses CS 21a. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L16:
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Peyman Dodangeh Sharif University of Technology Fall 2014.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
OOP Basics Classes & Methods (c) IDMS/SQL News
Object-Oriented Programming: Polymorphism Chapter 10.
13-Mar-16 DOM. 2 Difference between SAX and DOM DOM reads the entire XML document into memory and stores it as a tree data structure SAX reads the XML.
USING ANDROID WITH THE DOM. Slide 2 Lecture Summary DOM concepts SAX vs DOM parsers Parsing HTTP results The Android DOM implementation.
21-Jun-16 Document Object Model DOM. SAX and DOM SAX and DOM are standards for XML parsers-- program APIs to read and interpret XML files DOM is a W3C.
XML. Contents  Parsing an XML Document  Validating XML Documents.
DOM Robin Burke ECT 360.
Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is new code that reuses old code. Polymorphism.
Lecture 14 - Abstract Classes
DOM Document Object Model.
Inheritance, Polymorphism, and Interfaces
Object Oriented Programming (OOP) LAB # 8
Inheritance, Polymorphism, and Interfaces. Oh My
Java Programming Language
DOM 8-Dec-18.
Java Inheritance.
DOM 24-Feb-19.
Final and Abstract Classes
WaysInJavaToParseXML
Presentation transcript:

Lecture 4 Java Interfaces (review of inheritance and abstract classes) The XML DOM Java Examples Homework 3

Inheritance Review IS-A versus IS-LIKE-A relationship Shape draw() erase() Circle draw() erase() Shape draw() erase() Circle draw() erase() getRadius()

void doStuff(Shape s){Shape s.erase(); …Circle Square Some class s.draw(); not yet written } Circle c = new Circle(); doStuff (c) Polymorphism via Dynamic Binding The decision is made at run-time as to which function to call.

BankAccount CheckingAccount SavingsAccountSomeOtherAccount Consider

public class BankAccount { public void deductFees() { …body… } : } public class SavingsAccount extends BankAccount{ } Writing the BankAccount class But what should we write??

Make the class Abstract Better public abstract class BankAccount { public abstract void deductFees(); : } public class SavingsAccount extends BankAccount{ public abstract void deductFees() {…} } No body..It’s up to the derived class to complete. We can write this!

Abstract Classes When you extend an existing class, you have a choice whether or not to redefine the methods of the superclass. If you don’t redefine the method, it will appear in the derived class as it appears in the superclass. Sometimes it is desirable to force programmers to redefine a method There may be no good (superclass) default for the method Only the subclass programmer can know how to implement the method

Abstract Classes An abstract method has no implementation You can’t construct objects of classes with abstract methods You can only create objects of concrete classes The class must be declared with the keyword abstract It’s fine to have handles that reference abstract classes Abstract classes, unlike interfaces, may have concrete methods and instance variables

Another abstract base class public abstract class Shape{ public abstract double area(); public abstract double circumference(); } Require computations only the derived class programmer knows.

class Circle extends Shape{ … public double area(){return PI*r*r;} } class Rectangle extends Shape{ } … Shape s = new Rectangle(); System.out.println(s.area()); Another abstract base class

Interfaces The interface keyword takes the concept of an abstract class one step further by preventing any function definitions at all!

Interfaces 1.The core reason for interfaces is to be able to upcast to more than one base type. C++ multiple inheritance is not supported. 2.Usually better than abstract classes. 3.Allows us to inherit a contract rather than an implementation. 4.When I implement an interface I make a promise that I will provide the required methods.

INTERFACES 5. Have no instance variables 6. Have only abstract methods (all parameters but no bodies) 7. Have only public methods 8. Are not classes…you can’t create interface objects 9. May be referenced 10. May contain constants 11. Are implemented not extended as in inheritance

INTERFACES Example public interface Comparable { int compareTo(Object other); } public class Student implements Comparable { // this class MUST define compareTo() } Automatically public Is there one correct way to write this?

INTERFACES public class Car implements Comparable { // this class MUST define compareTo() }

INTERFACES Now, suppose we have a function sort void sort(Comparable x[]) { } Can we pass an array of Student objects to this function? How about an array of Car?

INTERFACES void sort(Comparable x[]) { } Can we pass an array of Student objects to this function? Only if Student implements Comparable. How about an array of Car? Only if Car implements Comparable.

XML DOM The World Wide Web Consortium’s Document Object Model Provides a common vocabulary to use in manipulating XML documents. May be used from C, Java, Perl, Python, or VB Things may be quite different “under the hood”. The interface to the document will be the same.

I am The Cat in The Hat I am Little Cat A I am Little Cat B I am Little Cat C The XML File “cats.xml”

Little cat A Little cat B I am little cat B topcat I am the cat in the hat Little cat D Little Cat C I am little cat C I am little cat A document XML doc XML decdoctypeelement textelement text element DOM

Agreement.xml

document XML doc XML decdoctype FixedFloatSwap Notional FixedRate NumYearsNumPayments All of these nodes implement the Node interface

Some DOM Documentation from JavaSoft

The Node Interface The Node interface is the primary datatype for the entire Document Object Model. It represents a single node in the document tree. While all objects implementing the Node interface expose methods for dealing with children, not all objects implementing the Node interface may have children. For example, Text nodes may not have children.

Properties All Nodes have properties. Not all properties are need by all types of nodes. The attribute property is an important part of the Element node but is null for the Text nodes. We access the properties through methods…

Some Methods of Node Example Methods are: String getNodeName() – depends on the Node type if Element node return tag name if Text node return #text

Some Methods of Node Example Methods are: short getNodeType() Might return a constant like ELEMENT_NODE or TEXT_NODE or …

Some Methods of Node Example Methods are: String getNodeValue() if the Node is an Element Node then return ‘null’ if the Node is a Text Node then return a String representing that text.

Some Methods of Node Example Methods are: Node getParentNode() returns a reference to the parent

Some Methods of Node Example Methods are: public Node getFirstChild() Returns the value of the firstChild property.

Some Methods of Node Example Methods are: public NodeList getChildNodes() returns a NodeList object A NodeList object does not implement Node. Why not?

The NodeList Interface The NodeList interface provides the abstraction of an ordered collection of nodes, without defining or constraining how this collection is implemented. The items in the NodeList are accessible via an integral index, starting from 0.

There are only two methods of the NodeList Interface public Node item(int index) Returns the indexth item in the collection. If index is greater than or equal to the number of nodes in the list, this returns null.

There are only two methods of the NodeList Interface public int getLength() Returns the value of the length property.

The Element Interface public interface Element extends Node By far the vast majority of objects (apart from text) that authors encounter when traversing a document are Element nodes. Inheritance Nothing prevents us from extending one interface in order to create another.

The Element Interface public interface Element extends Node Some methods in the Element interface String getAttribute(String name) Retrieves an attribute value by name.

The Element Interface public interface Element extends Node Some methods in the Element interface public String getTagName() Returns the value of the tagName property.

The Element Interface public interface Element extends Node Some methods in the Element interface public NodeList getElementsByTagName(String name) Returns a NodeList of all descendant elements with a given tag name, in the order in which they would be encountered in a preorder traversal of the Element tree..

The CharacterData Interface public interface CharacterData extends Node The CharacterData interface extends Node with a set of attributes and methods for accessing character data in the DOM. For clarity this set is defined here rather than on each object that uses these attributes and methods. No DOM objects correspond directly to CharacterData, though Text and others do inherit the interface from it. All offsets in this interface start from 0.

The CharacterData Interface public interface CharacterData extends Node An example method: public String getData() Returns the value of the the character data of the node that implements this interface. The Text interface extends CharacterData.

The Document Interface public interface Document extends Node The Document interface represents the entire HTML or XML document. Conceptually, it is the root of the document tree, and provides the primary access to the document's data.

The Document Interface public interface Document extends Node Some methods: public Element getDocumentElement() Returns the value of the documentElement property. This is a convenience attribute that allows direct access to the child node that is the root element of the document. For HTML documents, this is the element with the tagName "HTML".

The Document Interface Some methods: public NodeList getElementsByTagName(String tagname) Returns a NodeList of all the Elements with a given tag name in the order in which the would be encountered in a preorder traversal of the Document tree. Parameters: tagname - The name of the tag to match on. The special value "*" matches all tags. Returns: A new NodeList object containing all the matched Elements.

An Example import java.io.File; import org.w3c.dom.*; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.DocumentBuilder; import org.xml.sax.SAXException; import org.xml.sax.SAXParseException;

public class Simulator3 { public static void main(String argv[]) { Document doc; if(argv.length != 1 ) { System.err.println("usage: java Simulator3 documentname"); System.exit(1); } try { DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();

doc = docBuilder.parse(new File(argv[0])); Element top = doc.getDocumentElement(); top.normalize(); NodeList elementList = top.getElementsByTagName("*"); int listLength = elementList.getLength(); for(int i = 0; i < listLength; i++) { Element e = (Element)elementList.item(i); System.out.print(e.getNodeName()); Text t = (Text)e.getFirstChild(); System.out.println(t.getNodeValue()); }

catch(SAXParseException err) { System.out.println("Parsing error" + ", line " + err.getLineNumber() + ", URI " + err.getSystemId()); System.out.println(" " + err.getMessage()); } catch(SAXException e) { Exception x = e.getException(); ((x == null) ? e : x).printStackTrace(); } catch (Throwable t) { t.printStackTrace(); } System.exit(0); }

Agreement.xml

Output Notional100 Fixed_Rate5 NumYears3 NumPayments6

Homework 3 Rewrite the simulator (or modify the one that I wrote) for project 2 so that it reads the SwapAgreement.xml file using Java and the XML DOM