DOC PREVIEW
UMD CMSC 433 - Java Review

This preview shows page 1-2-3-18-19-37-38-39 out of 39 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 39 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 39 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 39 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 39 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 39 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 39 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 39 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 39 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 39 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CMSC433 Spring 2009 Programming Language Technology and Paradigms Java Review Adam Porter 2 5 09 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 3 1 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 4 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 5 2 The Class Hierarchy Classes by themselves are a powerful tool Support abstraction and encapsulation Java also provides two other abilities Interfaces allow type reuse Inheritance allows code reuse Note When you inherit from a class you also implement the class s interface 6 Project 1 Interfaces public interface Servlet public void doGet String path String options OutputStream out throws ServletException ShutdownException class HelloWorld implements Servlet Servlet s ServletFactory getInstance getServlet HelloWorld s doGet 7 3 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 8 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 9 4 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 10 Example class int int class int Point getX getY ColorPoint extends Point getColor ColorPoint reuses getX and getY from Point ColorPoint implements the Point interface They can be used anywhere a Point can be 11 5 Subtyping Both inheritance and interfaces allow one class to be 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 B 12 Java Design Everything inherits from Object Even arrays Allows sharing generics and more Object Number Integer Thread Well almost there are primitive int long float etc 13 6 No Multiple Inheritance A class type can implement many interfaces But can only extend one superclass Not a big deal There are ways to get around it And it s complicated to implement well Never ending debate on this point 14 Abstract Classes Sometimes want a class with some code but with some methods unwritten It can t be an interface because it has code It can t be a regular class because it doesn t have all the code You can t instantiate such a class Instead we can mark such a class as abstract And mark the unimplemented methods as abstract 15 7 Example from JDK public 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 the second 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 16 I O streams Raw communication takes place using streams InStream OutStream Process Java also provides readers and writers character streams Applies to files network connections strings etc 17 8 I O Classes OutputStream byte stream going out Writer character stream going out InputStream byte stream coming in Reader character stream coming in 18 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 19 9 Applications 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 use 20 Java Networking class Socket Communication channel class ServerSocket Server side listen socket Awaits and responds to connection requests 21 10 Example Client Server ServerSocket s new ServerSocket 5001 Socket conn s accept InputStream in conn getInputStream OutputStream out conn getOutputStream server Server code client Socket conn new Socket www cs umd edu 5001 InputStream in conn getInputStream OutputStream out conn getOutputStream Client code 22 Example Client Server ServerSocket s new ServerSocket 5001 Socket conn s accept InputStream in conn getInputStream OutputStream out conn getOutputStream server Server code client Socket conn new Socket www cs umd edu 5001 InputStream in conn getInputStream OutputStream out conn getOutputStream Client code 23 11 Example Client Server ServerSocket s new ServerSocket 5001 Socket conn s accept InputStream in conn getInputStream OutputStream out conn getOutputStream Server code server client Socket conn new Socket www cs umd edu 5001 InputStream in conn getInputStream OutputStream out conn getOutputStream Client code 24 Example Client Server ServerSocket s new ServerSocket 5001 Socket conn s accept InputStream in conn getInputStream OutputStream out conn getOutputStream server client Server code Note The server can still accept other connection requests on port 5001 Socket conn new Socket www cs umd edu 5001 InputStream in conn getInputStream OutputStream out conn getOutputStream Client code 25 12 Example Client Server ServerSocket s new ServerSocket 5001 Socket conn s accept InputStream in conn getInputStream OutputStream out conn getOutputStream server in Server code client out Socket conn new Socket www cs umd edu 5001 InputStream in conn getInputStream OutputStream out conn getOutputStream Client code 26 Example


View Full Document

UMD CMSC 433 - Java Review

Documents in this Course
Trace 1

Trace 1

62 pages

Reflection

Reflection

137 pages

Testing

Testing

25 pages

Paradigms

Paradigms

10 pages

Testing

Testing

17 pages

Java RMI

Java RMI

17 pages

Java RMI

Java RMI

17 pages

Java RMI

Java RMI

17 pages

Trace 1

Trace 1

46 pages

Jini

Jini

4 pages

Final

Final

15 pages

Java RMI

Java RMI

13 pages

Testing

Testing

16 pages

Load more
Download Java Review
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Java Review and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Java Review 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?