8 Copyright © 2005, Oracle. All rights reserved. Object Life Cycle and Inner Classes.

Slides:



Advertisements
Similar presentations
1 VBScript Session What we learn last session?
Advertisements

1 Classes and Objects in Java Basics of Classes in Java.
Final and Abstract Classes
1 Inheritance Classes and Subclasses Or Extending a Class.
Classes and Objects in Java
Chapter 7 Constructors and Other Tools. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 7-2 Learning Objectives Constructors Definitions.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12 Introduction to ASP.NET.
1 Copyright © 2010, Elsevier Inc. All rights Reserved Fig 2.1 Chapter 2.
12 Copyright © 2005, Oracle. All rights reserved. Structuring Code Using Abstract Classes and Interfaces.
4 Copyright © 2005, Oracle. All rights reserved. Creating the Web Tier: Servlets.
3 Copyright © 2005, Oracle. All rights reserved. Basic Java Syntax and Coding Conventions.
11 Copyright © 2005, Oracle. All rights reserved. Using Arrays and Collections.
7 Copyright © 2005, Oracle. All rights reserved. Creating Classes and Objects.
8 Copyright © 2005, Oracle. All rights reserved. Creating the Web Tier: JavaServer Pages.
10 Copyright © 2005, Oracle. All rights reserved. Reusing Code with Inheritance and Polymorphism.
6 Copyright © 2005, Oracle. All rights reserved. Building Applications with Oracle JDeveloper 10g.
17 Copyright © 2005, Oracle. All rights reserved. Deploying Applications by Using Java Web Start.
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
Title Subtitle.
DIVIDING INTEGERS 1. IF THE SIGNS ARE THE SAME THE ANSWER IS POSITIVE 2. IF THE SIGNS ARE DIFFERENT THE ANSWER IS NEGATIVE.
Addition Facts
ABC Technology Project
1 / / / /. 2 (Object) (Object) –, 10 (Class) (Class) –, –, – (Variables) [ Data member Field Attribute](, ) – (Function) [ Member function Method Operation.
Object Oriented Programming with Java
Understanding class definitions Looking inside classes 5.0.
1 public class Newton { public static double sqrt(double c) { double epsilon = 1E-15; if (c < 0) return Double.NaN; double t = c; while (Math.abs(t - c/t)
Object-Oriented Programming. 2 An object, similar to a real-world object, is an entity with certain properties, and with the ability to react in certain.
Lilian Blot VARIABLE SCOPE EXCEPTIONS FINAL WORD Final Lecture Spring 2014 TPOP 1.
Addition 1’s to 20.
25 seconds left…...
Week 1.
We will resume in: 25 Minutes.
Copyright © 2012 Pearson Education, Inc. Chapter 14: More About Classes.
Abstract Class, Packages and interface from Chapter 9
1 Abstract Class and Packages from Chapter 9 Lecture.
Chapter 9: Using Classes and Objects. Understanding Class Concepts Types of classes – Classes that are only application programs with a Main() method.
Java Programming 2 Dr. Priti Srinivas Sajja Introductory concepts of java programming as specified in PGDCA 203:Object Technology, S P University.
Review of ICS 102. Lecture Objectives To review the major topics covered in ICS 102 course Refresh the memory and get ready for the new adventure of ICS.
Lecture 2: Object Oriented Programming I
Road Map Introduction to object oriented programming. Classes
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
1 Further OO Concepts II – Java Program at run-time Overview l Steps in Executing a Java Program. l Loading l Linking l Initialization l Creation of Objects.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Lecture From Chapter 6 & /8/10 1 Method of Classes.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Review of ICS 102. Lecture Objectives To review the major topics covered in ICS 102 course Refresh the memory and get ready for the new adventure of ICS.
1 Chapter 8 – Classes and Object: A Deeper Look Outline 1 Introduction 2 Implementing a Time Abstract Data Type with a Class 3 Class Scope 4 Controlling.
Visual C# 2012 for Programmers © by Pearson Education, Inc. All Rights Reserved.
Dale Roberts Object Oriented Programming using Java - Final and Static Keywords Dale Roberts, Lecturer Computer Science, IUPUI
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
Classes, Interfaces and Packages
Chapter 4: More Object Concepts. Objectives Understand blocks and scope Overload a method Avoid ambiguity Create and call constructors with parameters.
YG - CS Concept of Encapsulation What is encapsulation? - data and functions/methods are packaged together in the class normally.
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
JAVA ACCESS MODIFIERS. Access Modifiers Access modifiers control which classes may use a feature. A classes features are: - The class itself - Its member.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Object Based Programming Chapter 8. 2 Contrast ____________________ Languages –Action oriented –Concentrate on writing ________________ –Data supports.
N ESTED C LASSES -1. T YPES OF N ESTED C LASSES & I NTERFACES top-level nested classes interfaces inner classes member local named anonymous.
Overloading and Overriding
Method of Classes Chapter 7, page 155 Lecture /4/6.
Chapter 5 Classes.
Presentation transcript:

8 Copyright © 2005, Oracle. All rights reserved. Object Life Cycle and Inner Classes

8-2 Copyright © 2005, Oracle. All rights reserved. Objectives After completing this lesson, you should be able to do the following: Provide two or more methods with the same name in a class Provide one or more constructors for a class Use initializers to initialize both instance and class variables Describe the class loading and initializing process, and the object life cycle Define and use inner classes

8-3 Copyright © 2005, Oracle. All rights reserved. Notes Page

8-4 Copyright © 2005, Oracle. All rights reserved. Overloading Methods Several methods in a class can have the same name. The methods must have different signatures. public class Movie { public void setPrice() { price = 3.50F; } public void setPrice(float newPrice) { price = newPrice; } … } Movie mov1 = new Movie(); mov1.setPrice(); mov1.setPrice(3.25F);

8-5 Copyright © 2005, Oracle. All rights reserved. Using the this Reference Instance methods receive an argument called this, which refers to the current object. public class Movie { public void setRating(String newRating) { this.rating = newRating; } void anyMethod() { Movie mov1 = new Movie(); Movie mov2 = new Movie(); mov1.setRating("PG"); … title: null rating: null mov2 mov1 title : null rating: PG this

8-6 Copyright © 2005, Oracle. All rights reserved. Initializing Instance Variables Instance variables can be explicitly initialized at declaration. Initialization happens at object creation. All instance variables are initialized implicitly to default values depending on data type. More complex initialization must be placed in a constructor. public class Movie { private String title; private String rating = "G"; private int numOfOscars = 0;

8-7 Copyright © 2005, Oracle. All rights reserved. What Are Constructors? For proper initialization, a class must provide a constructor. A constructor is called automatically when an object is created: –It is usually declared public. –It has the same name as the class. –It must not specify a return type. The compiler supplies a no-arg constructor if and only if a constructor is not explicitly provided. –If any constructor is explicitly provided, then the compiler does not generate the no-arg constructor.

8-8 Copyright © 2005, Oracle. All rights reserved. Defining and Overloading Constructors public class Movie { private String title; private String rating = "PG"; public Movie() { title = "Last Action …"; } public Movie(String newTitle) { title = newTitle; } Movie mov1 = new Movie(); Movie mov2 = new Movie("Gone …"); Movie mov3 = new Movie("The Good …"); The Movie class now provides two constructors.

8-9 Copyright © 2005, Oracle. All rights reserved. Sharing Code Between Constructors public class Movie { private String title; private String rating; public Movie() { this("G"); } public Movie(String newRating) { rating = newRating; } A constructor can call another constructor by using this(). Movie mov2 = new Movie(); What happens here?

8-10 Copyright © 2005, Oracle. All rights reserved. final Variables, Methods, and Classes A final variable is a constant and cannot be modified. –It must therefore be initialized. –It is often declared public static for external use. A final method cannot be overridden by a subclass. A final class cannot be subclassed. public final class Color { public final static Color black=new Color(0,0,0); … }

8-11 Copyright © 2005, Oracle. All rights reserved. Reclaiming Memory When all references to an object are lost, the object is marked for garbage collection. Garbage collection reclaims memory that is used by the object. Garbage collection is automatic. There is no need for the programmer to do anything, but the programmer can give a hint to System.gc();.

8-12 Copyright © 2005, Oracle. All rights reserved. Using the finalize() Method If an object holds a resource such as a file, then the object should be able to clean it up. You can provide a finalize() method in that class. The finalize() method is called just before garbage collection. public class Movie { … public void finalize() { System.out.println("Goodbye"); } Any problems?

8-13 Copyright © 2005, Oracle. All rights reserved. What Are Inner Classes? Inner classes are nested classes, defined in a class or method. They enforce a relationship between two classes. They are of four types: –Static –Member –Local –Anonymous public class Outer { … class Inner { … } Enclosing class

8-14 Copyright © 2005, Oracle. All rights reserved. Using Member Inner Class It is declared within another class. Nesting is allowed. It can access variables within its own class and any enclosing classes. It can only declare final static methods. public class CalendarPopup {... class MonthSelector { class DayOfMonth{...}; DayOfMonth[] NumberOfDaysInMonth... }

8-15 Copyright © 2005, Oracle. All rights reserved. Using Local Inner Class It is declared within a code block (inside a method). All final variables or parameters declared in the block are accessible by the methods of the inner class. public class CalendarPopup {... public void handlerMethod(){ class DateHandler{…}; DateHandler sc = new DateHandler();... }

8-16 Copyright © 2005, Oracle. All rights reserved. Defining Anonymous Inner Classes They are defined at method level. They are declared within a code block. They lack the class, extends, and implements keywords. They cannot have a constructor. public class Outer {... public void outerMethod(){... myObject.myAnonymous(new SomeOtherClass(){... } ) } }

8-17 Copyright © 2005, Oracle. All rights reserved. Using the Calendar Class It converts between a date object and a set of integer fields. It represents a specific moment in time. Subclasses interpret a date according to the specific calendar system. public class Order {... public void String getShipDate(){... Calendar c = Calendar.getInstance(); c.setTime(orderDate);... } }

8-18 Copyright © 2005, Oracle. All rights reserved. Summary In this lesson, you should have learned the following: Methods can be overloaded in Java. Instance methods receive a this reference to the current object. Most classes provide one or more constructors to initialize new objects. Class variables and class methods can be defined for classwide properties and behaviors. Classes can be defined in various ways within a class.

8-19 Copyright © 2005, Oracle. All rights reserved. Practice 8: Overview This practice covers: Defining and using overloaded methods Providing a no-arg constructor for a class Providing additional constructors for a class Defining static variables and static methods for classwide behavior Using static methods

8-20 Copyright © 2005, Oracle. All rights reserved. Notes Page for Practice 8

8-21 Copyright © 2005, Oracle. All rights reserved. Practice 8: Notes

8-22 Copyright © 2005, Oracle. All rights reserved. Practice 8: Notes

8-23 Copyright © 2005, Oracle. All rights reserved. Practice 8: Notes

8-24 Copyright © 2005, Oracle. All rights reserved.