Download Java Programming Review: OOP, Interfaces, Inheritance, and I/O Streams in CMSC433, Spring and more Study Guides, Projects, Research Programming Languages in PDF only on Docsity!
CMSC433, Spring 2004
Programming Language Technology and
Paradigms
Java Review
Jeff Foster
January 29, 2004
2
Administrivia
• Project 1 will be posted later today
• Reading: (today) Liskov ch. 1, 2, (Tue) ch. 4
- Supplemental: Eckel ch. 1, 7, 8, 9 3
Java
• Descended from Simula67, SmallTalk, others
- Superficially similar to C, C++
• Fully specified, compiles to virtual machine
• Secure
- bytecode verification (“type-safe”)
- security manager 4
Object Orientation
• Combining data and behavior
- objects, not developers, decide how to carry out
operations
• Sharing via abstraction and inheritance
- similar operations and structures are implemented once
• Emphasis on object-structure rather than
procedure structure
- behavior more stable than implementation
- … but procedure structure still useful
Example
public 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); } }
Using Complex
public 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); }
7
The 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”
8
Project 1: Interfaces
public 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(...); 9
Interfaces
• 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 or
more interfaces
• Given some interface I, declaring I x = ... means
- x must refer to an instance of a class that implements I,
or else null
10
Interface Inheritance
• Interfaces can extend other interfaces
- Sometimes convenient form of reuse (see project 1)
• Given interfaces I1 and I2 where I2 extends I
- If C implements I2, then C implements I
• Since a class can implement multiple interfaces,
interface extensions are often not needed
Inheritance
• Each Java class extends or inherits code from
exactly one superclass
• Permits reusing classes to define new objects
- Can define the behavior of the new object in terms of
the old one
Example
class 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 be
19 InStream OutStream Process
I/O streams
• Raw communication takes place using streams
• Java also provides readers and writers
• Applies to files, network connections, strings, etc.
20
I/O Classes
• OutputStream – byte stream going out
• Writer – character stream going out
• InputStream – byte stream coming in
• Reader – character stream coming in
21
Some 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
- StringWriter 22
Applications and I/O
• Java “external interface” is a public class
- public static void main(String [] args)
• args[0] is first argument
• 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
Java Networking
• class Socket
• class ServerSocket
- Server-side “listen” socket
- Awaits and responds to connection requests
Example Client/Server
ServerSocket 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 code
server client
Client
code
25
Example Client/Server
ServerSocket 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 code
server client
Client
code
26
Example Client/Server
ServerSocket 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 code
server client
Client
code
27
Example Client/Server
ServerSocket 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 code
server client Note: The server can still accept other connection requests on port 5001
Client
code
28
Example Client/Server
ServerSocket 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 code
server client in out
Client
code
Example Client/Server
ServerSocket 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 code
server client in out in out
Client
code
Possible Failures
• Server-side
- ServerSocket port already in use
- Client dies on accept
• Client-side
- Server dead
- No one listening on port
• In all cases IOException thrown
- Must use appropriate throw/try/catch constructs