Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 7 Java Object Model

Similar presentations


Presentation on theme: "Chapter 7 Java Object Model"— Presentation transcript:

1 Chapter 7 Java Object Model

2 Types Type: set of values and the operations that can be applied to the values Strongly typed language: compiler and run-time system check that no operation can execute that violates type system rules Compile-time check Employee e = new Employee(); e.clear(); // ERROR Run-time check:

3 Java Types Primitive types: int short long byte char float double boolean Class types Interface types Array types The null type Note: void is not a type

4 Java Values value of primitive type reference to object of class type
reference to array null Note: Can't have value of interface type

5 Subtype Relationship S is a subtype of T if S and T are the same type
S and T are both class types, and T is a direct or indirect superclass of S S is a class type, T is an interface type, and S or one of its superclasses implements T S and T are both interface types, and T is a direct or indirect superinterface of S S and T are both array types, and the component type of S is a subtype of the component type of T S is not a primitive type and T is the type Object S is an array type and T is Cloneable or Serializable S is the null type and T is not a primitive type

6 Type Inquiry Test whether e is a Shape: if (e instanceof Shape) . . .
Common before casts: Shape s = (Shape) e; Don't know exact type of e Could be any class implementing Shape If e is null, test returns false (no exception)

7 The Class Class getClass method gets class of any object
Returns object of type Class Class object describes a type Object e = new Rectangle(); Class c = e.getClass(); System.out.println(c.getName()); // prints java.awt.Rectangle

8 The equals Method equals tests for equal contents
== tests : whether two references are the identical objects. Used in many standard library methods

9 Requirements for equals Method
reflexive: x.equals(x) symmetric: x.equals(y) if and only if y.equals(x) transitive: if x.equals(y)  and y.equals(z), then x.equals(z) x.equals(null) must return false

10 Chapter 8 Frameworks

11 Frameworks Set of cooperating classes
Structures the essential mechanisms of a problem domain Example: Swing is a GUI framework Framework != design pattern Typical framework uses multiple design patterns

12 Application Frameworks
Implements services common to a type of applications Programmer forms subclasses of framework classes Result is an application Inversion of control: framework controls execution flow

13 Applets Applet: Java program that runs in a web browser
Programmer forms subclass of Applet or JApplet Overwrites init/destroy start/stop paint

14 Applets

15 Applets Interacts with ambient browser getParameter showDocument
HTML page contains applet tag and parameters <applet code="BannerApplet.class" width="300" height="100"> <param name="message" value="Hello, World!"/> <param name="fontname" value="Serif"/> <param name="fontsize" value="64"/> <param name="delay" value="10"/> </applet>

16 Example Applet Shows scrolling banner init reads parameters
start/stop start and stop timer paint paints the applet surface Ch8/applet/BannerApplet.java

17 Example Applets

18 Applets as a Framework Applet programmer uses inheritance
Applet class deals with generic behavior (browser interaction) Inversion of control: applet calls init, start,stop,destroy


Download ppt "Chapter 7 Java Object Model"

Similar presentations


Ads by Google