1CMSC433, Spring 2005 Programming Language Technologyand Paradigms Java ReviewJeff FosterJanuary 27, 20052Administrivia• Project 1 will be posted later today– Due February 9• Supplemental reading: Eckel ch. 1, 7, 8, 93Java• Descended from Simula67, SmallTalk, others– Superficially similar to C, C++• Fully specified, compiles to virtual machine– Machine-independent• Secure– Bytecode verification (“type-safe”)– Security manager4Object Orientation• Combining data and behavior– Objects, not developers, decide how to carry outoperations• Sharing via abstraction and inheritance– Similar operations and structures are implemented once• Emphasis on object-structure rather thanprocedure structure– Behavior more stable than implementation– … but procedure structure still useful25Examplepublic class Complex { private double r, i; public Complex(double r, double i) { this.r = r; this.i = i; } public String toString() { return “(“ + r + “, “ + i + “)”; } public Complex plus(Complex that) { return new Complex(r + that.r, i + that.i); }}6Using Complexpublic static void main(String[] args) { Complex a = new Complex(5.5,9.2); Complex b = new Complex(2.3,-5.1); Complex c; c = a.plus(b); System.out.println(“a = “ + a); System.out.println(“b = “ + b); System.out.println(“c = “ + c);}7The Class Hierarchy• Classes by themselves are a powerful tool– Support abstraction and encapsulation• Java also provides two other abilities– Interfaces allow different classes to be treated the same– Inheritance allows code reuse• Note: When you inherit from a class, you also“implement” the class’s “interface”8Project 1: Interfacespublic interface MiniServlet extends Runnable { void setArg(String arg); void setOutputStream(OutputStream out);}class HelloWorld implements MiniServlet { ... }class Print implements MiniServlet { ... }MiniServlet s = new HelloWorld();if (...) s = new Print();s.setArg(...);39Interfaces• An interface lists supported (public) methods– No constructors or implementations allowed– Can have final static variables• A class can implement (be a subtype of) zero ormore interfaces• Given some interface I, declaring I x = ... means– x must refer to an instance of a class that implements I,or else null10Interface Inheritance• Interfaces can extend other interfaces– Sometimes convenient form of reuse (see project 1)• Given interfaces I1 and I2 where I2 extends I1– If C implements I2, then C implements I1• Since a class can implement multiple interfaces,interface extensions are often not needed11Inheritance• Each Java class extends or inherits code fromexactly one superclass• Permits reusing classes to define new objects– Can define the behavior of the new object in terms ofthe old one12Exampleclass Point { int getX() { ... } int getY() { ... }}class ColorPoint extends Point { int getColor() { ... }}• ColorPoint reuses getX() and getY() from Point• ColorPoint “implements” the Point “interface”– They can be used anywhere a Point can be413Subtyping• Both inheritance and interfaces allow one class tobe used where another is specified– This is really the same idea: subtyping• We say that A is a subtype of B if– A extends B or a subtype of B, or– A implements B or a subtype of B14Java Design• Everything inherits from Object*– Even arrays– Allows sharing, generics, and more* Well, almost: there are primitive int, long, float, etc.ObjectThreadIntegerNumber…15No Multiple Inheritance• A class type can implement many interfaces• But can only extend one superclass• Not a big deal– Multiple inheritance rarely, if ever, necessary and oftenbadly used– And it’s complicated to implement well16Abstract Classes• Sometimes want a class with some code, but withsome methods unwritten– It can’t be an interface because it has code– It can’t be a regular class because it doesn’t have allthe code• You can’t instantiate such a class• Instead, we can mark such a class as abstract– And mark the unimplemented methods as abstract517Example from JDKpublic abstract class OutputStream { public abstract void write(int b) ...; public void write(byte b[], int off, int len) ... { ... write(b[off + i]);... } ...}• Subclasses of OutputStream need not override thesecond version of write(...)– But they do need to override the first one, since it’s abstract– (Note: They may want to override anyhow for efficiency)18Example from Project 1public abstract class ServletFilter extendsOutputStream implements MiniServlet{ public abstract void write(int b) ...; public abstract void close() ...; public abstract void flush() ...; public void setArg(String arg); public void setOutputStream(OutputStream out);}19InStreamOutStreamProcessI/O streams• Raw communication takes place using streams• Java also provides readers and writers– Character streams• Applies to files, network connections, strings, etc.20I/O Classes• OutputStream – byte stream going out• Writer – character stream going out• InputStream – byte stream coming in• Reader – character stream coming in621Some OutputStreams and Writers• Example classes– ByteArrayOutputStream – goes to byte []– FileOutputStream – goes to file• OutputStreamWriter– Wraps around OutputStream to get a Writer– Takes characters, converts to bytes– Can specify encoding used to convert• Other wrappers– PrintWriter – supports print, println– StringWriter22Applications and I/O• Java “external interface” is a public class– public static void main(String [] args)• args[0] is first argument– Unlike C/C++• System.out and System.err are PrintStreams– Should be PrintWriter, but would break 1.0 code– System.out.print(…) prints a string– System.out.println(…) prints a string with a newline• System.in is an InputStream– Not quite so easy to use23Java Networking• class Socket– Communication channel• class ServerSocket– Server-side “listen” socket– Awaits and responds to connection requests24Example Client/ServerServerSocket s = new ServerSocket(5001);Socket conn = s.accept();InputStream in = conn.getInputStream();OutputStream out = conn.getOutputStream();Socket conn = new Socket (“www.cs.umd.edu”, 5001);InputStream in = conn.getInputStream();OutputStream out = conn.getOutputStream();Server codeserver
View Full Document