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 – Due February 11 • 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 – machine-independent • 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 5 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); } } 6 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 I1 – If C implements I2, then C implements I1 • Since a class can implement multiple interfaces, interface extensions are often not needed 11 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 12 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 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 29 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 30 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 31 Class Objects • For each class, there is an object of type Class • Describes the class as a whole – used extensively in Reflection package • Class.forName(“MyClass”) – returns class object for MyClass – will load MyClass if needed • Class.forName(“MyClass”).newInstance() – creates a new instance of MyClass • MyClass.class gives the Class object for MyClass