Chapter 11 Object-Oriented Design Exception and binary I/O can be covered after Chapter 9

Similar documents
Chapter 11 Inheritance and Polymorphism. Motivations. Suppose you will define classes to model circles,

Inheritance and Polymorphism

Chapter 10 Inheritance and Polymorphism. Dr. Hikmat Jaber

CS 112 Programming 2. Lecture 06. Inheritance & Polymorphism (1) Chapter 11 Inheritance and Polymorphism

Inheritance and Polymorphism. CSE 114, Computer Science 1 Stony Brook University

Constructor. Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

Lecture Notes Chapter #9_b Inheritance & Polymorphism

Chapter 11 Inheritance and Polymorphism

Chapter 11 Inheritance and Polymorphism

Inheritance (continued) Inheritance

CISC 3115 TY3. C09a: Inheritance. Hui Chen Department of Computer & Information Science CUNY Brooklyn College. 9/20/2018 CUNY Brooklyn College

CS1150 Principles of Computer Science Objects and Classes

Chapter 11 Inheritance and Polymorphism

22. Inheritance. Java. Summer 2008 Instructor: Dr. Masoud Yaghini

Abstract Class. Lecture 21. Based on Slides of Dr. Norazah Yusof

24. Inheritance. Java. Fall 2009 Instructor: Dr. Masoud Yaghini

Introduction to OOP with Java. Instructor: AbuKhleif, Mohammad Noor Sep 2017

25. Generic Programming

IST311. Advanced Issues in OOP: Inheritance and Polymorphism

Object Oriented System Development Paradigm. Sunnie Chung CIS433 System Analysis Methods

CH. 2 OBJECT-ORIENTED PROGRAMMING

CS-202 Introduction to Object Oriented Programming

Chapter 21- Using Generics Case Study: Geometric Bunch. Class: Driver. package csu.matos; import java.util.arraylist; public class Driver {

We are on the GUI fast track path

Lecture Notes Chapter #9_c Abstract Classes & Interfaces

COMP200 INHERITANCE. OOP using Java, from slides by Shayan Javed

CS171:Introduction to Computer Science II. Li Xiong

Chapter 11 Object-Oriented Design Exception and binary I/O can be covered after Chapter 9

Introduction Programming Using Python Lecture 8. Dr. Zhang COSC 1437 Fall 2017 Nov 30, 2017

Practice for Chapter 11

INHERITANCE & POLYMORPHISM. INTRODUCTION IB DP Computer science Standard Level ICS3U. INTRODUCTION IB DP Computer science Standard Level ICS3U

CSA 1019 Imperative and OO Programming

EKT472: Object Oriented Programming. Overloading and Overriding Method

In this lab, you will be given the implementation of the classes GeometricObject, Circle, and Rectangle, as shown in the following UML class diagram.

Chapter 14 Abstract Classes and Interfaces

Inheritance. Inheritance allows the following two changes in derived class: 1. add new members; 2. override existing (in base class) methods.

Java Classes, Objects, Inheritance, Abstract and Interfaces Recap

Chapter 8 Objects and Classes

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn how to describe objects and classes and how to define classes and create objects

Java Object Oriented Design. CSC207 Fall 2014

Chapter 8 Objects and Classes. Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

What is Inheritance?

HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance

Programming 2. Inheritance & Polymorphism

OO Programming Concepts

Outline. Inheritance. Abstract Classes Interfaces. Class Extension Overriding Methods Inheritance and Constructors Polymorphism.

ITI Introduction to Computing II

More Relationships Between Classes

ITI Introduction to Computing II

Day 4. COMP1006/1406 Summer M. Jason Hinek Carleton University

COMP200 - Object Oriented Programming: Test One Duration - 60 minutes

Chapter 9 Objects and Classes. OO Programming Concepts. Classes. Objects. Motivations. Objectives. CS1: Java Programming Colorado State University

Chapter 5 Object-Oriented Programming

HAS-A Relationship. If A uses B, then it is an aggregation, stating that B exists independently from A.

Computer Science II (20073) Week 1: Review and Inheritance

Chapter 9 Objects and Classes. Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All rights reserved.

Making New instances of Classes

First IS-A Relationship: Inheritance

Questions Answer Key Questions Answer Key Questions Answer Key

Chapter 6 Introduction to Defining Classes

Inheritance Motivation

HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.

Rules and syntax for inheritance. The boring stuff

Inheritance and Polymorphism

Inheritance (Deitel chapter 9)

COP 3330 Final Exam Review

Object Oriented Programming. Java-Lecture 11 Polymorphism

CS260 Intro to Java & Android 03.Java Language Basics

Chapter 6: Inheritance

Inheritance and Polymorphism

More About Objects. Zheng-Liang Lu Java Programming 255 / 282

Inheritance. The Java Platform Class Hierarchy

UFCE3T-15-M Object-oriented Design and Programming

Class, Variable, Constructor, Object, Method Questions

Chapter 13 Abstract Classes and Interfaces

Introductory Programming Inheritance, sections

AP CS Unit 6: Inheritance Notes

Generics. CSE260, Computer Science B: Honors Stony Brook University

Inheritance, Polymorphism, and Interfaces

Language Features. 1. The primitive types int, double, and boolean are part of the AP

Object Oriented Programming

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

Java: introduction to object-oriented features

Object Orientated Programming Details COMP360

Implements vs. Extends When Defining a Class

OBJECT ORİENTATİON ENCAPSULATİON

Java Fundamentals (II)

Lecture 4: Extending Classes. Concept

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs.

Reusing Classes. Hendrik Speleers

CISC370: Inheritance

CLASS DESIGN. Objectives MODULE 4

Java Primer. CITS2200 Data Structures and Algorithms. Topic 2

C12a: The Object Superclass and Selected Methods

Programming in Java, 2e Sachin Malhotra Saurabh Choudhary

Binghamton University. CS-140 Fall Dynamic Types

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

Introduction to Inheritance

Transcription:

Chapter 6 Arrays Chapter 7 Objects and Classes Chapter 8 Strings Chapter 9 Inheritance and Polymorphism GUI can be covered after 10.2, Abstract Classes Chapter 12 GUI Basics 10.2, Abstract Classes Chapter 13 Graphics 10.4, Interfaces Chapter 14 Event-Driven Programming Chapter 11 Object-Oriented Design Exception and binary I/O can be covered after Chapter 9 Chapter 17 Exceptions and Assertions Chapter 18 Binary I/O 1

To develop a subclass from a superclass through inheritance ( 9.2). To invoke the superclass s constructors and methods using the super keyword ( 9.3). To override methods in the subclass ( 9.4). To distinguish differences between overriding and overloading ( 9.5). To comprehend polymorphism, dynamic binding, and generic programming ( 9.7). To describe casting and explain why explicit downcasting is necessary ( 9.8). To restrict access to data and methods using the protected visibility modifier ( 9.11). To declare constants, unmodifiable methods, and nonextendable classes using the final modifier ( 9.12). (Optional) To understand the effect of hiding data fields and static methods ( 9.14). (Optional) To initialize data using initialization blocks and to distinguish between instance initialization and static initialization blocks ( 9.15). To store, retrieve, and manipulates objects in an ArrayList,Vector ( 9.9). 2

GeometricObject -color: String -filled: boolean -datecreated: java.util.date +GeometricObject() +getcolor(): String +setcolor(color: String): void +isfilled(): boolean +setfilled(filled: boolean): void +getdatecreated(): java.util.date +tostring(): String The color of the object (default: white). Indicates whether the object is filled with a color (default: false). The date when the object was created. Creates a GeometricObject. Returns the color. Sets a new color. Returns the filled property. Sets a new filled property. Returns the datecreated. Returns a string representation of this object. Circle -radius: double +Circle() +Circle(radius: double) +getradius(): double +setradius(radius: double): void +getarea(): double +getperimeter(): double +getdiameter(): double Rectangle -width: double -height: double +Rectangle() +Rectangle(width: double, height: double) +getwidth(): double +setwidth(width: double): void +getheight(): double +setheight(height: double): void +getarea(): double +getperimeter(): double 3

No. They are not inherited. A constructor is used to construct an instance of a class. Unlike properties and methods, a superclass's constructors are not inherited in the subclass. They are invoked explicitly or implicitly. Explicitly using the super keyword. They can only be invoked from the subclasses' constructors, using the keyword super. If the keyword super is not explicitly used, the superclass's no-arg constructor is automatically invoked.[super()] A subclass inherits accessible data fields and methods from its superclass. 4

A constructor may invoke an overloaded constructor (this([args])) or its superclass s constructor (super([args])). If none of them is invoked explicitly, the compiler puts super() as the first statement in the constructor. For example, public A() { is equivalent to public A() { super(); public A(double d) { // some statements is equivalent to public A(double d) { super(); // some statements 5

The keyword super refers to the superclass of the class in which super appears. This keyword can be used in two ways: To call a superclass constructor: super([args]); To call a superclass method: super.m(); 6

You must use the keyword super to call the superclass constructor. Invoking a superclass constructor s name in a subclass causes a syntax error. Java requires that the statement that uses the keyword super appear first in the constructor. 7

Constructing an instance of a class invokes all the superclasses constructors along the inheritance chain. This is called constructor chaining. 1.class Person { 2. public Person() { 3. System.out.println("(1) Person's no-arg constructor is invoked"); 4.class Employee extends Person { 5. public Employee() { 6. this("(2) Invoke Employee s overloaded constructor"); 7. System.out.println("(3) Employee's no-arg constructor is invoked"); 8. public Employee(String s) { 9. System.out.println(s); super(); super(); 10. public class Faculty extends Employee { 11. public Faculty() { 12. System.out.println("(4) Faculty's no-arg constructor is invoked"); 13. public static void main(string[] args) { 14. new Faculty(); (1) Person's no-arg constructor is invoked (2) Invoke Employee s overloaded constructor (3) Employee's no-arg constructor is invoked (4) Faculty's no-arg constructor is invoked 8

Find out the errors in the program: class Fruit { public Fruit(String name) { System.out.println("Fruit's constructor is invoked"); public class Apple extends Fruit { public Apple(){ super(); ว ธ แก ไข 1. แก ท คลาส Apple 2. แก ท คลาส Fruit 9

A subclass extends properties and methods from the superclass. You can also: Add new properties (data fields) Add new methods Override the methods of the superclass 10

GeometricObject -color: String -filled: boolean -datecreated: java.util.date +GeometricObject() +getcolor(): String +setcolor(color: String): void +isfilled(): boolean +setfilled(filled: boolean): void +getdatecreated(): java.util.date +tostring(): String The color of the object (default: white). Indicates whether the object is filled with a color (default: false). The date when the object was created. Creates a GeometricObject. Returns the color. Sets a new color. Returns the filled property. Sets a new filled property. Returns the datecreated. Returns a string representation of this object. Circle -radius: double +Circle() +Circle(radius: double) +getradius(): double +setradius(radius: double): void +getarea(): double +getperimeter(): double +getdiameter(): double Rectangle -width: double -height: double +Rectangle() +Rectangle(width: double, height: double) +getwidth(): double +setwidth(width: double): void +getheight(): double +setheight(height: double): void +getarea(): double +getperimeter(): double เพ มเมธอดลงใน Circle ช อ printcircle() ซ งท าการเร ยกใช เมธอด getdatecreated() 11

You could rewrite the printcircle() method in the Circle class as follows: GeometricObject -color: String -filled: boolean -datecreated: java.util.date +GeometricObject() +getcolor(): String +setcolor(color: String): void +isfilled(): boolean +setfilled(filled: boolean): void +getdatecreated(): java.util.date +tostring(): String public class Circle extends GeometricObject{ public void printcircle() { System.out.println("The circle is created " + super.getdatecreated() + " and the radius is " + radius); //ไม จ าเป นต องเร ยกผ านค าส ง super 12

A subclass inherits methods from a superclass. Sometimes it is necessary for the subclass to modify the implementation of a method defined in the superclass. This is referred to as method overriding. public class Circle extends GeometricObject { // Other methods are omitted /** Override the tostring method defined in GeometricObject */ public String tostring() { return super.tostring() + "\nradius is " + radius; 13

An instance method can be overridden only if it is accessible. Thus a private method cannot be overridden, because it is not accessible outside its own class. If a method defined in a subclass is private in its superclass, the two methods are completely unrelated. 14

1. class A{ 2. private void m(){ 3. System..out.println( call from A ); 4. 5. public void m2(){ 6. m(); 7. 8. 9. class B extends A{ 10. private void m(){ 11. System.out.println( call from B ); 12. 13. public void m2(){ 14. m(); 15. 16. 17. public class TestOveridePrivateMethod { 18. public static void main(string[] args) { 19. B obj = new B(); 20. obj.m2();//call from B 21. 22. แทรกลงไปก อน line 15 ด วย super.m(); // can invoke or not? 15

class B { public void p(int i) { class A extends B { // This method overrides the method in B public void p(int i) { System.out.println(i); public class Test { public static void main(string[] args) { A a = new A(); a.p(10); inheritance Same name Same signature Same return type class B { public void p(int i) { class A extends B { // This method overloads the method in B public void p(double i) { System.out.println(i); public class Test { public static void main(string[] args) { A a = new A(); a.p(10); Same class, inheritance Same name different signature No relate to return type 16

Every class in Java is descended from the java.lang.object class. If no inheritance is specified when a class is defined, the superclass of the class is Object. public class Circle {... Equivalent public class Circle extends Object {... 17

The tostring() method returns a string representation of the object. The default implementation returns a string consisting of a class name of which the object is an instance, the at sign (@), and a number representing this object (hash code in hexadecimal). Loan loan = new Loan(); System.out.println(loan.toString()); The code displays something like Loan@15037e5. This message is not very helpful or informative. Usually you should override the tostring method so that it returns a digestible string representation of the object. 18

class Person extends Object { public String tostring() { return "Person"; class Student extends Person { public String tostring() { return "Student"; class GraduateStudent extends Student { public class PolymorphismDemo { public static void m(object x) { System.out.println(x.toString()); public static void main(string[] args) { m(new GraduateStudent()); m(new Student()); m(new Person()); m(new Object()); Method m takes a parameter of the Object type. You can invoke it with any object. An object of a subtype can be used wherever its supertype value is required. This feature is known as polymorphism. When the method m(object x) is executed, the argument x s tostring method is invoked. x may be an instance of GraduateStudent, Student, Person, or Object. Classes GraduateStudent, Student, Person, and Object have their own implementation of the tostring method. Which implementation is used will be determined dynamically by the Java Virtual Machine at runtime. This capability is known as dynamic binding. Polymorphism allows methods to be used generically for a wide range of object arguments. This is known as generic programming. 19

Matching a method signature and binding a method implementation are two issues. The compiler finds a matching method according to parameter type, number of parameters, and order of the parameters at compilation time.(ด data type) A method may be implemented in several subclasses. The Java Virtual Machine dynamically binds the implementation of the method at runtime.(ด ตรง new) 20

Dynamic binding works as follows: Suppose an object o is an instance of classes C 1, C 2,..., C n-1, and C n, where C 1 is a subclass of C 2, C 2 is a subclass of C 3,..., and C n-1 is a subclass of C n. That is, C n is the most general class, and C 1 is the most specific class. In Java, C n is the Object class. If o invokes a method p, the JVM searches the implementation for the method p in C 1, C 2,..., C n-1 and C n, in this order, until it is found. Once an implementation is found, the search stops and the first-found implementation is invoked. Since o is an instance of C 1, o is also an instance of C 2, C 3,, C n-1, and C n C n C n-1..... C 2 C 1 Object C 1 o = new C 1 (); o.p(); 21

Object C n-1... C 2 +p() print C2 C 1 o = new C 1 (); o.p(); C 2 o = new C 2 (); o.p(); C 2 o = new C 1 (); o.p(); C 1 +p() print C1 ถ าในคลาส C 2 ไม ม เมธอด p() และ superclass อ นก ไม ม เมธอดน 22

You have already used the casting operator to convert variables of one primitive type to another. Casting can also be used to convert an object of one class type to another within an inheritance hierarchy. In the preceding section, the statement m(new Student()); assigns the object new Student() to a parameter of the Object type. This statement is equivalent to: Object o = new Student(); // Implicit casting m(o); The statement Object o = new Student(), known as implicit casting, is legal because an instance of Student is automatically an instance of Object. public class PolymorphismDemo { public static void m(object x) { System.out.println(x.toString()); public static void main(string[] args) { m(new Student()); 23

Suppose you want to assign the object reference o to a variable of the Student type using the following statement: Object o = new Student(); Student b = o;// A compilation error would occur. Why does the statement Object o = new Student() work and the statement Student b = o doesn t This is because a Student object is always an instance of Object, but an object is not necessarily an instance of Student. Even though you can see that o is really a Student object, the compiler is not so clever to know it. To tell the compiler that o is a Student object, use an explicit casting. The syntax is similar to the one used for casting among primitive data types. Enclose the target object type in parentheses and place it before the object to be cast, as follows: Object o = new Student(); Student b = (Student)o; // Explicit casting 24

Explicit casting must be used when casting an object from a superclass to a subclass. This type of casting may not always succeed. Fruit fruit = new Fruit(); Apple x = (Apple)fruit; //no Orange x2 = (Orange)fruit;//no //runtime error, ClassCastException Apple apple = new Apple (); Fruit x = apple; //ok Fruit x2 =(Fruit)apple;//ok Fruit fruit = new Apple(); Apple x = (Apple)fruit; Orange x2 = (Orange)fruit;//no //runtime error, ClassCastException Apple +p() +m() fruit.m(); x.m(); Fruit +p() Orange +p() +m() 25

Use the instanceof operator to test whether an object is an instance of a class: Fruit myobject = new Apple();... // Some lines of code /** Perform casting if myobject is an instance of Apple */ if (myobject instanceof Apple) { Apple obj = (Apple)myObject; System.out.println( call m: + obj.m());... Fruit +p() Apple +p() +m() Orange +p() +m() 26

GeometricObject -color: String -filled: boolean -datecreated: java.util.date +GeometricObject() +getcolor(): String +setcolor(color: String): void +isfilled(): boolean +setfilled(filled: boolean): void +tostring(): String +getarea():double The color of the object (default: white). Indicates whether the object is filled with a color (default: false). The date when the object was created. Creates a GeometricObject. Returns the color. Sets a new color. Returns the filled property. Sets a new filled property. Returns a string representation of this object. public class Rectangle extends GeometricObject{... public Rectangle(double width, double height) { this.width = width; this.height = height; public double getarea() { return width * height; Circle -radius: double +Circle() +Circle(radius: double) +getradius(): double +setradius(radius: double): void +getarea(): double +getperimeter(): double +getdiameter(): double Rectangle -width: double -height: double +Rectangle() +Rectangle(width: double, height: double) +getwidth(): double +setwidth(width: double): void +getheight(): double +setheight(height: double): void +getarea(): double +getperimeter(): double public class Circle extends GeometricObjet{... public Circle(double radius) { this.radius = radius; public double getarea() { return radius * radius * Math.PI; 27

1. public class TestCircleRectangle { 2. public static void main(string[] args) { 3. double totalarea =0; 4. GeometricObject obj[] = {new Circle(5),new Rectangle(6,7); 5. for (int i = 0; i < obj.length; i++) { 6. System.out.println( area of obj[ + i + ] is : +obj[i].getarea()); 7. totalarea = totalarea+obj[i].getarea(); 8. 9. System.out.println( totalarea : + totalarea); 10. 11. area of obj[0] is :78.53981633974483 area of obj[1] is :42.0 totalarea :120.53981633974483 ถ าจะเร ยก getradius() จาก object ของคลาส Circle ต องท าอย างไร if(obj[i] instanceof Circle){ Circle tmp =(Circle)obj[i]; tmp.getradius(); //((Circle)obj[i]).getRadius(); 28

The protected modifier can be applied on data fields and methods in a class. A protected data field or a protected method in a public class can be accessed by any class in the same package or its subclasses, even if the subclasses are in a different package. private, default, protected, public Visibility increases private, none (if no modifier is used), protected, public 29

package p1; public class C1 { public int x; protected int y; int z; private int u; protected void m() { public class C2 { C1 o = new C1(); can access o.x; can access o.y; can access o.z; cannot access o.u; can invoke o.m(); package p2; public class C3 extends C1 { can access x; can access y; can access z; cannot access u; public class C4 extends C1 { can access x; can access y; cannot access z; cannot access u; public class C5 { C1 o = new C1(); can access o.x; cannot access o.y; cannot access o.z; cannot access o.u; can invoke m(); can invoke m(); cannot invoke o.m(); import p1.*; 30

Modifier on members in a class Accessed from the same class Accessed from the same package Accessed from a subclass Accessed from a different package public protected - default - - private - - - 31

A subclass may override a protected method in its superclass and change its visibility to public. However, a subclass cannot weaken the accessibility of a method defined in the superclass. For example, if a method is defined as public in the superclass, it must be defined as public in the subclass. 32

The final variable is a constant: final static double PI = 3.14159; The final class cannot be extended: final class Math {... The final instance method cannot be overridden by its subclasses. final void method(){ final class A{ class B extends A{ //compile error class A{ final void m(){ class B extends A{{ void m() { //compile error 33

You can override an instance method, but you cannot override a data field (instance or static) or a static method. If you declare a data field or a static method in a subclass with the same name as one in the superclass, the one in the superclass is hidden(กรณ เร ยกใน subclass), but it still exists. The two data fields or static methods are independent. You can reference the hidden field or static method using the super keyword in the subclass. The hidden field or method can also be accessed via a reference variable of the superclass s type. 34

When accessing a data field or a static method, the declared type of the reference variable decides which method is used at compilation time. (static binding) A obj = new B(); When invoking an instance method from a reference variable, the actual class of the object referenced by the variable decides which implementation of the method is used at runtime. (dynamic binding) A obj = new B(); 35

public class A { public int i = 1; public static int j = 11; public static String m1() {return "A's static m1"; public String m2() { return "A's instance m2"; public String m3() {return "A's instance m3"; public class B extends A { public int i = 2; public static int j = 12; (1) x.i is 1 (2) (B)x.i is 2 (3) x.j is 11 (4) ((B)x).j is 12 (5) x.m1() is A's static m1 (6) ((B)x).m1() is B's static m1 (7) x.m2() is B's instance m2 public static String m1(){return "B's static m1"; (8) x.m3() is A's instance m3 public String m2() {return "B's instance m2"; public class HidingDemo { public static void main(string[] args) { A x = new B(); // Access instance data field i System.out.println("(1) x.i is " + x.i); System.out.println("(2) (B)x.i is " + ((B)x).i); // Access static data field j System.out.println("(3) x.j is " + x.j); System.out.println("(4) ((B)x).j is " + ((B)x).j); // Invoke static method m1 System.out.println("(5) x.m1() is " + x.m1()); System.out.println("(6) ((B)x).m1() is " + ((B)x).m1()); // Invoke instance method m2 System.out.println("(7) x.m2() is " + x.m2()); System.out.println("(8) x.m3() is " + x.m3()); 36

Share common codes Initialization blocks can be used to initialize objects along with the constructors. An initialization block is a block of statements enclosed inside a pair of braces. An initialization block appears within the class declaration, but not inside methods or constructors. It is executed as if it were placed at the beginning of every constructor in the class. public class Book { private static int numofobjects; private String title; private int id; public Book(String title) { numofobjects++; this.title = title; public Book(int id) { numofobjects++; this.id = id; Equivalent public class Book { private static int numofobjects; private String title; private int id; public Book(String title) { this.title = title; public Book(int id) { this.id = id; { numofobjects++; 37

A static initialization block is much like a nonstatic initialization block except that it is declared static, can only refer to static members of the class, and is invoked when the class is loaded. The JVM loads a class when it is needed. A superclass is loaded before its subclasses. 38

1 2 3 4 5 6 7 8 class N { N() { System.out.println("(7) N's constructor body"); { System.out.println("(5) N's instance initialization block"); { System.out.println("(6) N's instance initialization block"); static { System.out.println("(2) N's static initialization block"); 9 10 11 12 13 14 15 16 class M extends N { M() { System.out.println("(9) M's constructor body"); { System.out.println("(8) M's instance initialization block"); static { System.out.println("(3) M's static initialization block"); static { System.out.println("(4) M's static initialization block"); 17 18 19 20 21 22 public class InitializationDemo { public static void main(string[] args) { M object = new M(); static { System.out.println("(1) InitializationDemo's static block"); 39

(1) InitializationDemo's static block (2) N's static initialization block (3) M's static initialization block (4) M's static initialization block (5) N's instance initialization block (6) N's instance initialization block (7) N's constructor body (8) M's instance initialization block (9) M's constructor body 40

You can create an array to store objects. But the array s size is fixed once the array is created. Java provides the ArrayList class that can be used to store an unlimited number of objects. java.util.arraylist +ArrayList() +add(o: Object) : void +add(index: int, o: Object) : void +clear(): void +contains(o: Object): boolean +get(index: int) : Object +indexof(o: Object) : int +isempty(): boolean +lastindexof(o: Object) : int +remove(o: Object): boolean +size(): int +remove(index: int) : Object +set(index: int, o: Object) : Object Creates an empty list. Appends a new element o at the end of this list. Adds a new element o at the specified index in this list. Removes all the elements from this list. Returns true if this list contains the element o. Returns the element from this list at the specified index. Returns the index of the first matching element in this list. Returns true if this list contains no elements. Returns the index of the last matching element in this list. Removes the element o from this list. Returns the number of elements in this list. Removes the element at the specified index. Sets the element at the specified index. 41