1 Java Object Model Part 1. 2 Type Definition: set of values – a set of values and set of operations –a set of operations that can be applied to those.

Slides:



Advertisements
Similar presentations
STRING AN EXAMPLE OF REFERENCE DATA TYPE. 2 Primitive Data Types  The eight Java primitive data types are:  byte  short  int  long  float  double.
Advertisements

SUMMARY: abstract classes and interfaces 1 Make a class abstract so instances of it cannot be created. Make a method abstract so it must be overridden.
Java Review Interface, Casting, Generics, Iterator.
CERTIFICATION OBJECTIVES Use Class Members Develop Wrapper Code & Autoboxing Code Determine the Effects of Passing Variables into Methods Recognize when.
CSM-Java Programming-I Spring,2005 Exceptions Lesson - 7.
Generic programming in Java
CSE 341, Winter Type Systems Terms to learn about types: –Type –Type system –Statically typed language –Dynamically typed language –Type error –Strongly.
IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
Java Generics.
CS 106 Introduction to Computer Science I 01 / 30 / 2008 Instructor: Michael Eckmann.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Subclasses. Inheritance class Animal { int row, column; // will be inherited private Model model; // private prevents inheritance Animal( ) {... } //
1 Java object model Part 3: Serialization & Reflection.
1 Type Type system for a programming language = –set of types AND – rules that specify how a typed program is allowed to behave Why? –to generate better.
March 2005Java Programming1. March 2005Java Programming2 Why Java? Platform independence Object Oriented design Run-time checks (fewer bugs) Exception.
Differences between C# and C++ Dr. Catherine Stringfellow Dr. Stewart Carpenter.
Topic 3 The Stack ADT.
P Object type and wrapper classes p Object methods p Generic classes p Interfaces and iterators Generic Programming Data Structures and Other Objects Using.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Effective Java: Generics Last Updated: Spring 2009.
Inheritance. Inheritance Early programmers often wrote code very similar to existing code Example: A human resources system might handle different types.
GENERIC COLLECTIONS. Type-Wrapper Classes  Each primitive type has a corresponding type- wrapper class (in package java.lang).  These classes are called.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
The java.lang Package chapter 8 Java Certification Study Group February 2, 1998 Seth Ladd.
Page: 1 การโปรแกรมเชิงวัตถุด้วยภาษา JAVA บุรินทร์ รุจจนพันธุ์.. ปรับปรุง 15 มิถุนายน 2552 Keyword & Data Type มหาวิทยาลัยเนชั่น.
Comp 248 Introduction to Programming Chapter 4 & 5 Defining Classes Part C Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Generic Programming  Object Type  Autoboxing  Bag of Objects  JCL Collections  Nodes of Objects  Iterators.
Introduction to Java Lecture Notes 3. Variables l A variable is a name for a location in memory used to hold a value. In Java data declaration is identical.
1 Generics Chapter 21 Liang, Introduction to Java Programming.
SE-1010 Dr. Mark L. Hornick 1 Variables & Datatypes.
Stacks and Queues. 2 3 Runtime Efficiency efficiency: measure of computing resources used by code. can be relative to speed (time), memory (space), etc.
Types in programming languages1 What are types, and why do we need them?
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Lecture 4 Generic programming Advanced Java Programming 1 dr hab. Szymon Grabowski dr inż. Wojciech Bieniecki
Chapter 14 Abstract Classes and Interfaces. Abstract Classes An abstract class extracts common features and functionality of a family of objects An abstract.
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
Java 5 Part 2 CSE301 University of Sunderland Harry Erwin, PhD.
CMSC 341 Java Packages, Classes, Variables, Expressions, Flow Control, and Exceptions.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
U n i v e r s i t y o f H a i l 1 ICS 202  2011 spring  Data Structures and Algorithms 
Java Type System and Object Model Horstmann ch , 7.7.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler
1 CSC 2053 New from AutoBoxing 3 Before J2SE 5.0, working with primitive types required the repetitive work of converting between the primitive.
CS 151: Object-Oriented Design November 12 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
Spring 2009 Programming Fundamentals I Java Programming XuanTung Hoang Lecture No. 8.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Typecasting References Computer Science 3 Gerb Reference: Objective: Understand how to use the Object class in Java in the context of ArrayLists.
CS 884 (Prasad)Java Types1 Language Specification Semantics of constructs. –Definition and use of name-value bindings: Name Resolution. Soundness : No.
UMBC CMSC 331 Java Review of objects and variables in Java.
100e Hosted by vhs © Don Link, Indian Creek School, 2004 Jeopardy.
Lecture 5:Interfaces and Abstract Classes Michael Hsu CSULA.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
JAVA GENERICS Lecture 16 CS2110 – Spring 2016 Photo credit: Andrew Kennedy.
Lecture 6:Interfaces and Abstract Classes Michael Hsu CSULA.
Modern Programming Tools And Techniques-I
Chapter 7: Cloning and RTTI
JAVA MULTIPLE CHOICE QUESTION.
Object-oriented Programming in Java
CSE 413, Autumn 2002 Programming Languages
Type Systems Terms to learn about types: Related concepts: Type
Computer Science II Exam 1 Review.
null, true, and false are also reserved.
Extending Classes.
Generic programming in Java
ArrayLists 22-Feb-19.
Java Programming Language
Chap 2. Identifiers, Keywords, and Types
Chapter 7 Java Object Model
Presentation transcript:

1 Java Object Model Part 1

2 Type Definition: set of values – a set of values and set of operations –a set of operations that can be applied to those values strongly-typedJava is a strongly-typed language: compiler & run- time system ensure that programs don’t violate type system rules –Compiler catches most illegal operation attempts –Java virtual machine catches a few (e.g. invalid casts) By contrast, C/C++ not strongly typed

3 Types in Java Primitive: int, double, char, boolean, etc. Class Interface Array –Array in itself is distinct type component –Individual array elements are of some component type Null

4 Values in Java Every value is one of the following: –Value of a primitive type (e.g. 1.4, -9, ‘c’) –Reference to a class object –Reference to an array –null Notes: –No such thing as a value of an interface type –Void is not a data type – just a method tag to indicate no return value

5 Subtype relationship Subtype contains subset of values of a given type Can use subtype where supertype is specified

6 S is a subtype of T if: 1.S and T are the same type 2.S and T are both class types, and T is a direct or indirect superclass of S 3.S is a class type, T is an interface type, and S or one of its superclasses implements T 4.S and T are both interface types, and T is a direct or indirect superinterface of S 5.S and T are both array types, and the component type of S is a subtype of the component type of T 6.S is not a primitive type and T is the type Object 7.S is an array type and T is Cloneable or Serializable 8.S is the null type and T is not a primitive type

7 Examples ListIterator is a subtype of Iterator Container is a subtype of Component JButton is a subtype of Component FlowLayout is a subtype of Layout Manager

8 Examples Rectangle[] is a subtype of Shape[] int[] is a subtype of Object int is not a subtype of long long is not a subtype of int int[] is not a subtype of Object[]

9 Array Types Recall rule 5: S[] is a subtype of T[] is S is a subtype of T Example: Rectangle [] r = new Rectangle[10]; Can store object r in an array of Shape, since Rectangle is a subset of Shape: Shape [] s = r; Variables r and s both refer to the same array

10 Example continued What happens if a non-Rectangle Shape is stored in s[]? Throws ArrayStoreException at runtime Every array object remembers its component type Could store a subtype of Rectangle (but not just of Shape) without throwing exception

11 Primitive type wrappers Many services in Java API deal with Objects; for example, ArrayLists are collections of Objects Primitive types are not subtypes of Object Useful to have classes to “wrap” primitive types so they can avail themselves of Object services

12 Wrapper classes in Java Wrapper classes exist for all primitive types: –Integer, Short, Long –Byte, Char –Float, Double –Boolean Wrapper classes are immutable

13 Wrapper class methods Constructor take primitive type argument: double num = ; Double d = new Double(num); Can “unwrap” to get primitive value: num = d.doubleValue(); Some wrappers have parse methods that convert from String to primitive: String s = “52406”; int n = Integer.parseInt(s);

14 Enumerated types Type with finite set of values C++ has enum mechanism for creating such a type; Java doesn’t Can simulate some of the same functionality in Java using typesafe enumeration (example on next slide) Only operation on these objects is comparison for equality

15 Typesafe enumeration example public class Science { private Science (String name) { this.name = name;} private String name; public static final Science BIOLOGY = new Science(“BIOLOGY”); public static final Science CHEMISTRY = new Science(“CHEMISTRY”); public static final Science PHYSICS = new Science(“PHYSICS”); … }

16 Type inquiry Can use instanceof operator to test whether an expression is a reference to an object of a given type or one of its subtypes Operator returns true if expression is a subtype of the given type; otherwise (including in the case of a null expression) returns false

17 Example using instanceof JPanel picPanel = new JPanel(); ArrayList images = new ArrayList(); … for (int x=0; x<images.size(); x++) { Object o = images.get(x); if (o instanceof Icon) { Icon pic = (Icon)o; picPanel.add(pic); }

18 Notes on instanceof Can test whether value is subtype of given type, but doesn’t distinguish between subtypes In previous example, o could be any kind of Icon (since Icon is an interface, couldn’t be an Icon per se)

19 Getting exact class of object Can get exact class of object reference by invoking getClass() method Method getClass() returns an object of type Class; can invoke getName() on Class object to retrieve String containing class name: System.out.println(o.getClass().getName());

20 Class object Type descriptor Contains information about a given type, e.g. type name and superclass

21 Ways to obtain Class object Call getClass() on an object reference Call static Class method forName(): Class c = Class.forName(“java.awt.Rectangle”); Add suffix.class to a type name to obtain a literal Class object: Class c = Rectangle.class;

22 Notes on Class type There is exactly one Class object for every type loaded in the virtual machine Class objects describe any type, including primitive types Can use == to test whether two Class objects describe same type: if (o.getClass() == Rectangle.class) True if o is exactly a Rectangle (not a subclass)

23 Arrays and getClass() When applied to an array, getClass() returns a Class object that describes the array type Use Class method getComponentType() to get a Class object describing the array’s components Use Class method isArray() to determine whether or not an object is an array

24 Arrays and Class.getName() Array name is built from these rules: [type Where type is one of the following: B:byteC:char D:doubleF:float I:intJ:long S:shortZ: boolean For non-primitive types, replace “type” with Lname; (where name is the name of the class or interface – note semicolon)