DOC PREVIEW
UT Arlington CSE 3302 - Object-Oriented Java Programming

This preview shows page 1-2 out of 7 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 7 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 7 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 7 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

2008-2-251Object-Oriented Java ProgrammingCSE3302 Programming Languages Object-Oriented Java Programming Spring 2008 ©Weimin He1Why Java?• Simple• Platform Independent•SafeCSE3302 Programming Languages Object-Oriented Java Programming Spring 2008 ©Weimin He2• Multi-Threaded• Garbage CollectedObjects, Classes, and Methods• ObjectSomething that occupies memory and has a state• ClassPattern (type) of objectCSE3302 Programming Languages Object-Oriented Java Programming Spring 2008 ©Weimin He3• MethodFunction and procedure to access object stateConstructors Overloadingpublic class Point{ … }public class Rectangle{ private int width = 0;private int height = 0;private Point origin;public Rectangle() { origin = new Point(0, 0); } CSE3302 Programming Languages Object-Oriented Java Programming Spring 2008 ©Weimin He4{g (,);}public Rectangle(Point p, int w, int h){ origin = p;width = w;height = h; } …} Access Control• Public:• Private:• Protected:CSE3302 Programming Languages Object-Oriented Java Programming Spring 2008 ©Weimin He5• Package:Example of Access ControlBase.javapackage p1; public class Base{ private int u;protected int v;public int w;private void g ( ){ System.out.println(v); } protected void f ( ){ System.out.println(u);g(); Test.javapackage p2;import p1.Base;public class Test {public static void main(String[] args) { Base b = new Base();b.u = 1; // Compilation errorb. v = 2;// Compilation errorCSE3302 Programming Languages Object-Oriented Java Programming Spring 2008 ©Weimin He6g()}}Derived.javapackage p2;import p1.Base;public class Derived extends Base{ void h (){ f(); }void k (){ g(); }}pb.w = 3;// OKb.g();// Compilation errorb.f();// Compilation errorDerived d = new Derived();d.h();// OKd.k();// Compilation error}}2008-2-252InheritanceShapeTwoDimensionalShape ThreeDimensionalShapeObjectCSE3302 Programming Languages Object-Oriented Java Programming Spring 2008 ©Weimin He7public class B extends A {…}public class A {…}Circle Square Triangle Sphere CubeClass Objectclass Object {…public boolean equals ( Object obj ) { … }public String toString() { …}CSE3302 Programming Languages Object-Oriented Java Programming Spring 2008 ©Weimin He8pgg() { }…} Method equals ( )• ==: identical references to objectsString s = “Hello”;String t = new String(“Hello”);if ( s == t ) System.out.println( “the same”); // condition is false and print nothing!if ( s.equals(t) ) System.out.println( “the same”);• equals: equality of objects– Already implemented in class Object–Need to define for your class :CSE3302 Programming Languages Object-Oriented Java Programming Spring 2008 ©Weimin He9Need to define for your class :Point p1 = new Point (10, 10);Point p2 = new Point (5, 5);Rectangle rec1 = new Rectangle (p1, 5,10);Rectangle rec2 = new Rectnagle (p2, 5, 10);if ( rec1 == rec2 ) System.out.println(“the same”);// condition is false and print nothing!if ( rec1.equals(rec2) ) System.out.println(“the same”);Overide equals ( ) public class Rectangle{ …public boolean equals ( Object obj ){ Rectangle rec = (Rectangle) obj;CSE3302 Programming Languages Object-Oriented Java Programming Spring 2008 ©Weimin He10return (w == rec.width()) && (h == rec.height());}} Rectangle rec1 = new Rectangle (p1, 5, 10); Rectangle rec2 = new Rectangle (p2, 5, 10);if ( rec1.equals(rec2) ) System.out.println(“the same!”);// condition is true and print “the same”!Method toString( )Rectangle rec = new Rectangle (p1, 5, 10);System.out.println( rec );// prints something like Rectangle@73d6at(class name +@+ hash code of object)CSE3302 Programming Languages Object-Oriented Java Programming Spring 2008 ©Weimin He11Overide toString( )public class Rectangle{ …public String toString() { return “width = ” + w + “, height = ” + h;}}CSE3302 Programming Languages Object-Oriented Java Programming Spring 2008 ©Weimin He12Rectangle rec = new Rectangle (p1, 5, 10); System.out.println( rec );Output: width = 5, height = 10 (instead of Rectangle@73d6at)2008-2-253Inheritancepublic class Circle{ private Point center;private double radius;public Circle( Point c, double r){ center = c;public class Rectangle{ private Point center;private double width;private double height;public Rectangle( Point c,double w, double h)CSE3302 Programming Languages Object-Oriented Java Programming Spring 2008 ©Weimin He13radius = r;}public double area(){ return Math.PI * radius* radius; }}{ center = c;width = w;height = h;}public double area(){ return width * height; }}Inheritance (Cont’d)public abstract class Shape{ private Point center;public Shape( Point c){ center = c; }CSE3302 Programming Languages Object-Oriented Java Programming Spring 2008 ©Weimin He14public abstractdouble area();}Inheritance (Cont’d)public class Circle extends Shape{ public Circle( Point c, double r){super(c);radius = r;}public class Rectangle extends Shape{ private double width;private double height;public Rectangle( Point c,double w, double h)CSE3302 Programming Languages Object-Oriented Java Programming Spring 2008 ©Weimin He15public double area(){ return Math.PI * radius* radius;}}{ super(c);width = w;height = h;}public double area(){ return width * height; }}Casting• Upcasting: Assign a subclass reference to a superclass variable• Downcasting:Assign Superclass Reference to subclass variableCSE3302 Programming Languages Object-Oriented Java Programming Spring 2008 ©Weimin He16gpExample of Castingpublic class Queue{ // constructors and instance variables omittedpublic void enqueue( int x) { … }public void dequeue() { … }public int front() { … }public boolean empty() { … }}Queue q1, q2;Deque d;d = new Deque();q1 = d;q1.deleteRear();// Compilation errorCSE3302 Programming Languages Object-Oriented Java Programming Spring 2008 ©Weimin He17}public class Deque extends Queue{ …public void addFront( int x ) { … }public void deleteRear() { … }} d = q1;// Compilation errord= (Deque) q1;d.deleteRear();q2 = new Queue();d = (Deque) q2;// Runtime errord.deleteRear();Dynamic Binding• Dynamic Binding– A mechanism by which, when the compiler can't determine which method implementation to use in advance, the runtime system (JVM) selects the appropriate method at runtime, based on the class of the object.– The process of binding a call to a particular method. This is performed dynamically at run-time. CSE3302 Programming Languages


View Full Document

UT Arlington CSE 3302 - Object-Oriented Java Programming

Documents in this Course
Smalltalk

Smalltalk

11 pages

Syntax

Syntax

5 pages

Syntax

Syntax

5 pages

JAVA

JAVA

57 pages

Semantics

Semantics

41 pages

Control

Control

74 pages

Load more
Download Object-Oriented Java Programming
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 Object-Oriented Java Programming 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 Object-Oriented Java Programming 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?