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

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

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

Unformatted text preview:

Object-Oriented Java ProgrammingWhy Java?Objects, Classes, and MethodsConstructors OverloadingAccess ControlExample of Access ControlInheritanceClass ObjectMethod equals ( )Overide equals ( )Method toString( )Overide toString( )Slide 13Inheritance (Cont’d)Slide 15CastingExample of CastingDynamic BindingDynamic Binding ExampleDynamic Binding Example (Cont’d)Abstract ClassExample of Abstract ClassInterfaceInterface ExampleInterface Example (Cont’d)Slide 26Slide 27Interface (Java) vs. Multiple Inheritance (C++)Interfaces vs. Abstract ClassesCombination of Abstract Class and InterfaceString ProcessingImportant Methods in Class StringSlide 33Class StringBufferImportant Methods in StringBufferImportant Methods in Class StringBufferClass StringTokenizerImportant Methods in Class StringTokenizerCSE3302 Programming Languages Object-Oriented Java Programming Spring 2008 ©Weimin He 1Object-Oriented Java ProgrammingCSE3302 Programming Languages Object-Oriented Java Programming Spring 2008 ©Weimin He 2Why Java?•Simple•Platform Independent•Safe•Multi-Threaded•Garbage CollectedCSE3302 Programming Languages Object-Oriented Java Programming Spring 2008 ©Weimin He 3Objects, Classes, and Methods•ObjectSomething that occupies memory and has a state•ClassPattern (type) of object•MethodFunction and procedure to access object stateCSE3302 Programming Languages Object-Oriented Java Programming Spring 2008 ©Weimin He 4Constructors Overloadingpublic class Point{ … }public class Rectangle{ private int width = 0; private int height = 0; private Point origin; public Rectangle() { origin = new Point(0, 0); } public Rectangle(Point p, int w, int h) { origin = p; width = w; height = h; } …}CSE3302 Programming Languages Object-Oriented Java Programming Spring 2008 ©Weimin He 5Access Control•Public:•Private:•Protected:•Package:CSE3302 Programming Languages Object-Oriented Java Programming Spring 2008 ©Weimin He 6Example 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(); }}Derived.javapackage p2;import p1.Base;public class Derived extends Base{ void h () { f(); } void k () { g(); }}Test.javapackage p2;import p1.Base;public class Test { public static void main(String[] args) { Base b = new Base(); b.u = 1; // Compilation error b. v = 2; // Compilation error b.w = 3; // OK b.g(); // Compilation error b.f(); // Compilation error Derived d = new Derived(); d.h(); // OK d.k(); // Compilation error }}CSE3302 Programming Languages Object-Oriented Java Programming Spring 2008 ©Weimin He 7Inheritancepublic class B extends A {…}public class A {…}ShapeTwoDimensionalShape ThreeDimensionalShapeCircle Square Triangle Sphere CubeObjectCSE3302 Programming Languages Object-Oriented Java Programming Spring 2008 ©Weimin He 8Class Objectclass Object { … public boolean equals ( Object obj ) { … } public String toString() { …} …}CSE3302 Programming Languages Object-Oriented Java Programming Spring 2008 ©Weimin He 9Method equals ( )•==: identical references to objects String 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 :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”);CSE3302 Programming Languages Object-Oriented Java Programming Spring 2008 ©Weimin He 10Overide equals ( ) public class Rectangle{ … public boolean equals ( Object obj ) { Rectangle rec = (Rectangle) obj; return (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”!CSE3302 Programming Languages Object-Oriented Java Programming Spring 2008 ©Weimin He 11Method 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 He 12Overide toString( )public class Rectangle{ …public String toString() { return “width = ” + w + “, height = ” + h;}} Rectangle rec = new Rectangle (p1, 5, 10); System.out.println( rec );Output: width = 5, height = 10 (instead of Rectangle@73d6at)CSE3302 Programming Languages Object-Oriented Java Programming Spring 2008 ©Weimin He 13Inheritancepublic class Circle{ private Point center; private double radius; public Circle( Point c, double r) { center = c; radius = r; } public double area() { return Math.PI * radius* radius; }}public class Rectangle{ private Point center; private double width; private double height; public Rectangle( Point c, double w, double h) { center = c; width = w; height = h; } public double area() { return width * height; }}CSE3302 Programming Languages Object-Oriented Java Programming Spring 2008 ©Weimin He 14Inheritance (Cont’d)public abstract class Shape{ private Point center; public Shape( Point c) { center = c; } public abstract double area();}CSE3302 Programming Languages Object-Oriented Java Programming Spring 2008 ©Weimin He 15Inheritance (Cont’d)public class Circle extends Shape{ public Circle( Point c, double r) {super(c); radius = r; } public double area() { return Math.PI * radius* radius;}}public class Rectangle extends Shape{ private double width; private double height; public Rectangle( Point c, double w, double h){ super(c); width = w; height = h; } public double area() { return width * height; }}CSE3302 Programming Languages Object-Oriented Java Programming Spring 2008 ©Weimin He


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?