![Page 1: Lecture 4 Java Interfaces (review of inheritance and abstract classes) The XML DOM Java Examples Homework 3](https://reader035.vdocument.in/reader035/viewer/2022062421/56649d375503460f94a0ff33/html5/thumbnails/1.jpg)
Lecture 4
• Java Interfaces (review of inheritance and abstract classes)
• The XML DOM
• Java Examples
• Homework 3
![Page 2: Lecture 4 Java Interfaces (review of inheritance and abstract classes) The XML DOM Java Examples Homework 3](https://reader035.vdocument.in/reader035/viewer/2022062421/56649d375503460f94a0ff33/html5/thumbnails/2.jpg)
Inheritance Review
IS-A versus IS-LIKE-A relationship
Shape
draw()
erase()
Circle
draw()
erase()
Shape
draw()
erase()
Circle
draw()
erase()
getRadius()
![Page 3: Lecture 4 Java Interfaces (review of inheritance and abstract classes) The XML DOM Java Examples Homework 3](https://reader035.vdocument.in/reader035/viewer/2022062421/56649d375503460f94a0ff33/html5/thumbnails/3.jpg)
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.
![Page 4: Lecture 4 Java Interfaces (review of inheritance and abstract classes) The XML DOM Java Examples Homework 3](https://reader035.vdocument.in/reader035/viewer/2022062421/56649d375503460f94a0ff33/html5/thumbnails/4.jpg)
BankAccount
CheckingAccount SavingsAccount SomeOtherAccount
Consider
![Page 5: Lecture 4 Java Interfaces (review of inheritance and abstract classes) The XML DOM Java Examples Homework 3](https://reader035.vdocument.in/reader035/viewer/2022062421/56649d375503460f94a0ff33/html5/thumbnails/5.jpg)
public class BankAccount {
public void deductFees() { …body… }
:
:
}
public class SavingsAccount extends BankAccount{
}
Writing the BankAccount class
But what should wewrite??
![Page 6: Lecture 4 Java Interfaces (review of inheritance and abstract classes) The XML DOM Java Examples Homework 3](https://reader035.vdocument.in/reader035/viewer/2022062421/56649d375503460f94a0ff33/html5/thumbnails/6.jpg)
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 thederived class to complete.
We can write this!
![Page 7: Lecture 4 Java Interfaces (review of inheritance and abstract classes) The XML DOM Java Examples Homework 3](https://reader035.vdocument.in/reader035/viewer/2022062421/56649d375503460f94a0ff33/html5/thumbnails/7.jpg)
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
![Page 8: Lecture 4 Java Interfaces (review of inheritance and abstract classes) The XML DOM Java Examples Homework 3](https://reader035.vdocument.in/reader035/viewer/2022062421/56649d375503460f94a0ff33/html5/thumbnails/8.jpg)
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
![Page 9: Lecture 4 Java Interfaces (review of inheritance and abstract classes) The XML DOM Java Examples Homework 3](https://reader035.vdocument.in/reader035/viewer/2022062421/56649d375503460f94a0ff33/html5/thumbnails/9.jpg)
Another abstract base class
public abstract class Shape{
public abstract double area();public abstract double circumference();
}
Require computations only the derivedclass programmer knows.
![Page 10: Lecture 4 Java Interfaces (review of inheritance and abstract classes) The XML DOM Java Examples Homework 3](https://reader035.vdocument.in/reader035/viewer/2022062421/56649d375503460f94a0ff33/html5/thumbnails/10.jpg)
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
![Page 11: Lecture 4 Java Interfaces (review of inheritance and abstract classes) The XML DOM Java Examples Homework 3](https://reader035.vdocument.in/reader035/viewer/2022062421/56649d375503460f94a0ff33/html5/thumbnails/11.jpg)
Interfaces
The interface keyword takes the concept of an abstract class one step further by preventing any function definitions at all!
![Page 12: Lecture 4 Java Interfaces (review of inheritance and abstract classes) The XML DOM Java Examples Homework 3](https://reader035.vdocument.in/reader035/viewer/2022062421/56649d375503460f94a0ff33/html5/thumbnails/12.jpg)
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.
![Page 13: Lecture 4 Java Interfaces (review of inheritance and abstract classes) The XML DOM Java Examples Homework 3](https://reader035.vdocument.in/reader035/viewer/2022062421/56649d375503460f94a0ff33/html5/thumbnails/13.jpg)
INTERFACES
5. Have no instance variables6. Have only abstract methods (all parameters but no bodies)7. Have only public methods8. Are not classes…you can’t create interface objects9. May be referenced10. May contain constants11. Are implemented not extended as in inheritance
![Page 14: Lecture 4 Java Interfaces (review of inheritance and abstract classes) The XML DOM Java Examples Homework 3](https://reader035.vdocument.in/reader035/viewer/2022062421/56649d375503460f94a0ff33/html5/thumbnails/14.jpg)
INTERFACES
• Example
public interface Comparable { int compareTo(Object other); }
public class Student implements Comparable { // this class MUST define compareTo() }
Automatically public
Is there one correctway to write this?
![Page 15: Lecture 4 Java Interfaces (review of inheritance and abstract classes) The XML DOM Java Examples Homework 3](https://reader035.vdocument.in/reader035/viewer/2022062421/56649d375503460f94a0ff33/html5/thumbnails/15.jpg)
INTERFACES
public class Car implements Comparable { // this class MUST define compareTo() }
![Page 16: Lecture 4 Java Interfaces (review of inheritance and abstract classes) The XML DOM Java Examples Homework 3](https://reader035.vdocument.in/reader035/viewer/2022062421/56649d375503460f94a0ff33/html5/thumbnails/16.jpg)
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?
![Page 17: Lecture 4 Java Interfaces (review of inheritance and abstract classes) The XML DOM Java Examples Homework 3](https://reader035.vdocument.in/reader035/viewer/2022062421/56649d375503460f94a0ff33/html5/thumbnails/17.jpg)
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.
![Page 18: Lecture 4 Java Interfaces (review of inheritance and abstract classes) The XML DOM Java Examples Homework 3](https://reader035.vdocument.in/reader035/viewer/2022062421/56649d375503460f94a0ff33/html5/thumbnails/18.jpg)
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.
![Page 19: Lecture 4 Java Interfaces (review of inheritance and abstract classes) The XML DOM Java Examples Homework 3](https://reader035.vdocument.in/reader035/viewer/2022062421/56649d375503460f94a0ff33/html5/thumbnails/19.jpg)
<?xml version = "1.0" ?><!DOCTYPE TopCat SYSTEM "cats.dtd"> <TopCat> I am The Cat in The Hat <LittleCatA> I am Little Cat A </LittleCatA> <LittleCatB> I am Little Cat B <LittleCatC> I am Little Cat C </LittleCatC> </LittleCatB> <LittleCatD/></TopCat>
The XML File “cats.xml”
![Page 20: Lecture 4 Java Interfaces (review of inheritance and abstract classes) The XML DOM Java Examples Homework 3](https://reader035.vdocument.in/reader035/viewer/2022062421/56649d375503460f94a0ff33/html5/thumbnails/20.jpg)
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 littlecat C
I am littlecat A
document
XML doc
XML dec doctype element
text element element element
text text
text
element
DOM
![Page 21: Lecture 4 Java Interfaces (review of inheritance and abstract classes) The XML DOM Java Examples Homework 3](https://reader035.vdocument.in/reader035/viewer/2022062421/56649d375503460f94a0ff33/html5/thumbnails/21.jpg)
Agreement.xml
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE FixedFloatSwap SYSTEM "FixedFloatSwap.dtd"><FixedFloatSwap> <Notional>100</Notional> <Fixed_Rate>5</Fixed_Rate> <NumYears>3</NumYears> <NumPayments>6</NumPayments></FixedFloatSwap>
![Page 22: Lecture 4 Java Interfaces (review of inheritance and abstract classes) The XML DOM Java Examples Homework 3](https://reader035.vdocument.in/reader035/viewer/2022062421/56649d375503460f94a0ff33/html5/thumbnails/22.jpg)
document
XML doc
XML dec doctype
FixedFloatSwap
Notional
FixedRate NumYears NumPayments
All of these nodes implement the Node interface
![Page 23: Lecture 4 Java Interfaces (review of inheritance and abstract classes) The XML DOM Java Examples Homework 3](https://reader035.vdocument.in/reader035/viewer/2022062421/56649d375503460f94a0ff33/html5/thumbnails/23.jpg)
Some DOM Documentation from JavaSoft
![Page 24: Lecture 4 Java Interfaces (review of inheritance and abstract classes) The XML DOM Java Examples Homework 3](https://reader035.vdocument.in/reader035/viewer/2022062421/56649d375503460f94a0ff33/html5/thumbnails/24.jpg)
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.
![Page 25: Lecture 4 Java Interfaces (review of inheritance and abstract classes) The XML DOM Java Examples Homework 3](https://reader035.vdocument.in/reader035/viewer/2022062421/56649d375503460f94a0ff33/html5/thumbnails/25.jpg)
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…
![Page 26: Lecture 4 Java Interfaces (review of inheritance and abstract classes) The XML DOM Java Examples Homework 3](https://reader035.vdocument.in/reader035/viewer/2022062421/56649d375503460f94a0ff33/html5/thumbnails/26.jpg)
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
![Page 27: Lecture 4 Java Interfaces (review of inheritance and abstract classes) The XML DOM Java Examples Homework 3](https://reader035.vdocument.in/reader035/viewer/2022062421/56649d375503460f94a0ff33/html5/thumbnails/27.jpg)
Some Methods of Node
Example Methods are:
short getNodeType()
Might return a constant like ELEMENT_NODE or TEXT_NODE or …
![Page 28: Lecture 4 Java Interfaces (review of inheritance and abstract classes) The XML DOM Java Examples Homework 3](https://reader035.vdocument.in/reader035/viewer/2022062421/56649d375503460f94a0ff33/html5/thumbnails/28.jpg)
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.
![Page 29: Lecture 4 Java Interfaces (review of inheritance and abstract classes) The XML DOM Java Examples Homework 3](https://reader035.vdocument.in/reader035/viewer/2022062421/56649d375503460f94a0ff33/html5/thumbnails/29.jpg)
Some Methods of Node
Example Methods are:
Node getParentNode()
returns a reference to the parent
![Page 30: Lecture 4 Java Interfaces (review of inheritance and abstract classes) The XML DOM Java Examples Homework 3](https://reader035.vdocument.in/reader035/viewer/2022062421/56649d375503460f94a0ff33/html5/thumbnails/30.jpg)
Some Methods of Node
Example Methods are:
public Node getFirstChild()
Returns the value of the firstChild property.
![Page 31: Lecture 4 Java Interfaces (review of inheritance and abstract classes) The XML DOM Java Examples Homework 3](https://reader035.vdocument.in/reader035/viewer/2022062421/56649d375503460f94a0ff33/html5/thumbnails/31.jpg)
Some Methods of Node
Example Methods are:
public NodeList getChildNodes()
returns a NodeList object
A NodeList object does not implement Node. Why not?
![Page 32: Lecture 4 Java Interfaces (review of inheritance and abstract classes) The XML DOM Java Examples Homework 3](https://reader035.vdocument.in/reader035/viewer/2022062421/56649d375503460f94a0ff33/html5/thumbnails/32.jpg)
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.
![Page 33: Lecture 4 Java Interfaces (review of inheritance and abstract classes) The XML DOM Java Examples Homework 3](https://reader035.vdocument.in/reader035/viewer/2022062421/56649d375503460f94a0ff33/html5/thumbnails/33.jpg)
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.
![Page 34: Lecture 4 Java Interfaces (review of inheritance and abstract classes) The XML DOM Java Examples Homework 3](https://reader035.vdocument.in/reader035/viewer/2022062421/56649d375503460f94a0ff33/html5/thumbnails/34.jpg)
There are only two methods of the NodeList Interface
public int getLength()
Returns the value of the length property.
![Page 35: Lecture 4 Java Interfaces (review of inheritance and abstract classes) The XML DOM Java Examples Homework 3](https://reader035.vdocument.in/reader035/viewer/2022062421/56649d375503460f94a0ff33/html5/thumbnails/35.jpg)
The Element Interface
public interface Elementextends 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 fromextending one interface inorder to create another.
![Page 36: Lecture 4 Java Interfaces (review of inheritance and abstract classes) The XML DOM Java Examples Homework 3](https://reader035.vdocument.in/reader035/viewer/2022062421/56649d375503460f94a0ff33/html5/thumbnails/36.jpg)
The Element Interface
public interface Elementextends Node
• Some methods in the Element interface
String getAttribute(String name)
Retrieves an attribute value by name.
![Page 37: Lecture 4 Java Interfaces (review of inheritance and abstract classes) The XML DOM Java Examples Homework 3](https://reader035.vdocument.in/reader035/viewer/2022062421/56649d375503460f94a0ff33/html5/thumbnails/37.jpg)
The Element Interface
public interface Elementextends Node
• Some methods in the Element interface
public String getTagName()
Returns the value of the tagName property.
![Page 38: Lecture 4 Java Interfaces (review of inheritance and abstract classes) The XML DOM Java Examples Homework 3](https://reader035.vdocument.in/reader035/viewer/2022062421/56649d375503460f94a0ff33/html5/thumbnails/38.jpg)
The Element Interface
public interface Elementextends 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..
![Page 39: Lecture 4 Java Interfaces (review of inheritance and abstract classes) The XML DOM Java Examples Homework 3](https://reader035.vdocument.in/reader035/viewer/2022062421/56649d375503460f94a0ff33/html5/thumbnails/39.jpg)
The CharacterData Interface
public interface CharacterDataextends 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 usesthese 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.
![Page 40: Lecture 4 Java Interfaces (review of inheritance and abstract classes) The XML DOM Java Examples Homework 3](https://reader035.vdocument.in/reader035/viewer/2022062421/56649d375503460f94a0ff33/html5/thumbnails/40.jpg)
The CharacterData Interface
public interface CharacterDataextends 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.
![Page 41: Lecture 4 Java Interfaces (review of inheritance and abstract classes) The XML DOM Java Examples Homework 3](https://reader035.vdocument.in/reader035/viewer/2022062421/56649d375503460f94a0ff33/html5/thumbnails/41.jpg)
The Document Interface
public interface Documentextends 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.
![Page 42: Lecture 4 Java Interfaces (review of inheritance and abstract classes) The XML DOM Java Examples Homework 3](https://reader035.vdocument.in/reader035/viewer/2022062421/56649d375503460f94a0ff33/html5/thumbnails/42.jpg)
The Document Interface
public interface Documentextends 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".
![Page 43: Lecture 4 Java Interfaces (review of inheritance and abstract classes) The XML DOM Java Examples Homework 3](https://reader035.vdocument.in/reader035/viewer/2022062421/56649d375503460f94a0ff33/html5/thumbnails/43.jpg)
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.
![Page 44: Lecture 4 Java Interfaces (review of inheritance and abstract classes) The XML DOM Java Examples Homework 3](https://reader035.vdocument.in/reader035/viewer/2022062421/56649d375503460f94a0ff33/html5/thumbnails/44.jpg)
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;
![Page 45: Lecture 4 Java Interfaces (review of inheritance and abstract classes) The XML DOM Java Examples Homework 3](https://reader035.vdocument.in/reader035/viewer/2022062421/56649d375503460f94a0ff33/html5/thumbnails/45.jpg)
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();
![Page 46: Lecture 4 Java Interfaces (review of inheritance and abstract classes) The XML DOM Java Examples Homework 3](https://reader035.vdocument.in/reader035/viewer/2022062421/56649d375503460f94a0ff33/html5/thumbnails/46.jpg)
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());
}
![Page 47: Lecture 4 Java Interfaces (review of inheritance and abstract classes) The XML DOM Java Examples Homework 3](https://reader035.vdocument.in/reader035/viewer/2022062421/56649d375503460f94a0ff33/html5/thumbnails/47.jpg)
} 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); }}
![Page 48: Lecture 4 Java Interfaces (review of inheritance and abstract classes) The XML DOM Java Examples Homework 3](https://reader035.vdocument.in/reader035/viewer/2022062421/56649d375503460f94a0ff33/html5/thumbnails/48.jpg)
Agreement.xml
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE FixedFloatSwap SYSTEM "FixedFloatSwap.dtd"><FixedFloatSwap> <Notional>100</Notional> <Fixed_Rate>5</Fixed_Rate> <NumYears>3</NumYears> <NumPayments>6</NumPayments></FixedFloatSwap>
![Page 49: Lecture 4 Java Interfaces (review of inheritance and abstract classes) The XML DOM Java Examples Homework 3](https://reader035.vdocument.in/reader035/viewer/2022062421/56649d375503460f94a0ff33/html5/thumbnails/49.jpg)
Output
Notional100Fixed_Rate5NumYears3NumPayments6
![Page 50: Lecture 4 Java Interfaces (review of inheritance and abstract classes) The XML DOM Java Examples Homework 3](https://reader035.vdocument.in/reader035/viewer/2022062421/56649d375503460f94a0ff33/html5/thumbnails/50.jpg)
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