Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright 1992-2012 by Pearson Education, Inc. All Rights Reserved.

Slides:



Advertisements
Similar presentations
 Pearson Education, Inc. All rights reserved static Class Members static fields – Also known as class variables – Represents class-wide.
Advertisements

Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
CSE 1302 Lecture 8 Inheritance Richard Gesick Figures from Deitel, “Visual C#”, Pearson.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Road Map Introduction to object oriented programming. Classes
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
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)
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
1 Fall 2007ACS-1903 Chapter 6: Classes Classes and Objects Instance Fields and Methods Constructors Overloading of Methods and Constructors Scope of Instance.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 Pearson Education, Inc. All rights reserved Classes and Objects: A Deeper Look - modified by Eileen Kraemer.
Java™ How to Program, 9/e Presented by: Dr. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Object Based Programming. Summary Slide  Instantiating An Object  Encapsulation  Inheritance  Polymorphism –Overriding Methods –Overloading vs. Overriding.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
You gotta be cool. Introduction to Classes, Objects and Strings Introduction Defining a Class with a Member Function Defining a Member Function with a.
Dale Roberts Object Oriented Programming using Java - Packages Dale Roberts, Lecturer Computer Science, IUPUI Department.
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.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
 2005 Pearson Education, Inc. All rights reserved Classes and Objects: A Deeper Look.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Spring CISC6795: Object Oriented Programming II.
Object Based Programming Chapter 8. 2 In This Chapter We will learn about classes Garbage Collection Data Abstraction and encapsulation.
Android How to Program, 2/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Methods in Java. Program Modules in Java  Java programs are written by combining new methods and classes with predefined methods in the Java Application.
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.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
Reformatted slides from the textbook, C++ How to Program, 6/e Pearson Education, Inc. All rights reserved Chapter 3. [Lecture 02] Introduction to.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
Visual C# 2012 for Programmers © by Pearson Education, Inc. All Rights Reserved.
Visual C# 2012 for Programmers © by Pearson Education, Inc. All Rights Reserved.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo CET 3640 © Copyright by Pearson Education, Inc. All Rights Reserved.
 Pearson Education, Inc. All rights reserved Classes and Objects: A Deeper Look.
Dale Roberts Object Oriented Programming using Java - Final and Static Keywords Dale Roberts, Lecturer Computer Science, IUPUI
 2005 Pearson Education, Inc. All rights reserved Classes and Objects: A Deeper Look.
© 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.
Object Oriented Programming
CIS 270—Application Development II Chapter 8—Classes and Objects: A Deeper Look.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 5 Methods.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
Object-Based Programming in VB.NET. Must Understand Following: Encapsulation Information hiding Abstract Data Type Class, Instance, Reference Variable.
Chapter 4: More Object Concepts. Objectives Understand blocks and scope Overload a method Avoid ambiguity Create and call constructors with parameters.
 2005 Pearson Education, Inc. All rights reserved. 1 Classes and Objects: A Deeper Look.
 Pearson Education, Inc. All rights reserved Classes and Objects: A Deeper Look.
Jozef Goetz Credits: Copyright  Pearson Education, Inc. All rights reserved. expanded by J. Goetz, 2016.
Object-Oriented Programming: Classes and Objects Chapter 1 1.
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
(C) 2010 Pearson Education, Inc. All rights reserved.  Best way to develop and maintain a large program is to construct it from small, simple pieces,
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.
Classes and Objects: A Deeper Look
Classes and Objects: A Deeper Look (Chapter 8)
Chapter 3: Using Methods, Classes, and Objects
Object Based Programming
Chapter 3 Introduction to Classes, Objects Methods and Strings
Chapter 3 Introduction to Classes, Objects Methods and Strings
Chapter 6 Methods: A Deeper Look
Object Oriented Programming in java
Chapter 8 Classes and Objects: A Deeper Look
Presentation transcript:

Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.

 Deeper look at building classes, controlling access to members of a class, and creating constructors.  Composition—a capability that allows a class to have references to objects of other classes.  static class members  final instance variables.  Organize classes in packages. © Copyright by Pearson Education, Inc. All Rights Reserved.

 Access modifiers public and private control access to a class’s variables and methods.  public methods present the services the class provides.  Clients need not be concerned with implementation details.  private class members are not accessible outside the class. © Copyright by Pearson Education, Inc. All Rights Reserved.

 Every object can access a reference to itself with keyword this.  Enables the class’s code to know which object should be manipulated.  Can use the this reference implicitly and explicitly. © Copyright by Pearson Education, Inc. All Rights Reserved.

 When you compile a.java file containing more than one class, the compiler produces a separate class file with the.class extension for every compiled class.  When one source-code (.java ) file contains multiple class declarations, the compiler places both class files for those classes in the same directory.  A source-code file can contain only one public class—otherwise, a compilation error occurs.  Non- public classes can be used only by other classes in the same package. © Copyright by Pearson Education, Inc. All Rights Reserved.

 Overloaded constructors enable objects of a class to be initialized in different ways.  To overload constructors, provide multiple constructor declarations with different signatures.  The compiler differentiates signatures by the number, the types, and the order of the parameter types in each signature. © Copyright by Pearson Education, Inc. All Rights Reserved.

 Every class must have at least one constructor.  If you do not provide any constructors, the compiler creates a default constructor that takes no arguments and initializes instance variables to their default values  Zero for numeric types  false for boolean  null for references  If your class declares constructors, the compiler will not create a default constructor.  You must explicitly declare a no-argument constructor if default initialization is required. © Copyright by Pearson Education, Inc. All Rights Reserved.

 Classes often provide public methods to allow clients of the class to set or get private instance variables.  Set methods are also commonly called mutator methods, because they typically change an object’s state  Get methods are also commonly called accessor methods or query methods. © Copyright by Pearson Education, Inc. All Rights Reserved.

 It would seem that providing set and get capabilities is essentially the same as making the instance variables public.  A public instance variable can be read or written by any method that has a reference to an object that contains that variable.  If an instance variable is declared private, a public get method allows other methods to access it, but the get method controls access.  A public set method can—and should—carefully scrutinize at- tempts to modify the variable to ensure valid values.  Although set and get methods provide access to private data, it is restricted by the implementation of the methods. © Copyright by Pearson Education, Inc. All Rights Reserved.

 A class can have references to objects of other classes.  This is called composition and is sometimes referred to as a has-a relationship.  Example: An AlarmClock object needs to know the current time and when it is supposed to sound its alarm  It’s reasonable to include two references to Time objects in an AlarmClock object. © Copyright by Pearson Education, Inc. All Rights Reserved.

 Every class in Java has a set of default methods one of which is the finalize method.  Every object uses system resources, such as memory.  Need a disciplined way to give resources back to the system when they’re no longer needed; otherwise, “resource leaks” might occur.  The JVM performs automatic garbage collection to reclaim the memory occupied by objects that are no longer used.  When there are no more references to an object, the object is eligible to be collected.  This typically occurs when the JVM executes its garbage collector. © Copyright by Pearson Education, Inc. All Rights Reserved.

 Memory leaks that are common in other languages like C and C++ are less likely in Java, but some can still happen in subtle ways.  Memory is not automatically reclaimed in those languages  Other types of resource leaks can occur.  An application may open a file on disk to modify its contents.  If it does not close the file, the application must terminate before any other application can use it. © Copyright by Pearson Education, Inc. All Rights Reserved.

 The finalize method is called by the garbage collector to perform termination housekeeping on an object just before the garbage collector reclaims the object’s memory.  Method finalize does not take parameters and has return type void.  A problem with method finalize is that the garbage collector is not guaranteed to execute at a specified time.  The garbage collector may never execute before a program terminates.  Thus, it’s unclear if, or when, method finalize will be called.  For this reason, most programmers should avoid method finalize. © Copyright by Pearson Education, Inc. All Rights Reserved.

 In certain cases, only one copy of a particular variable should be shared by all objects of a class.  A static field is used in such cases.  Also called a class variable.  A static variable represents classwide information — all objects of the class share the same piece of data.  The declaration of a static variable begins with the keyword static. © Copyright by Pearson Education, Inc. All Rights Reserved.

 Static variables have class scope.  public static class members can be accessed through a reference to any object of the class, or by using the class name and a dot (. ), as in Math.random().  private static class members can be accessed only through methods of the class.  static class members are available as soon as the class is loaded into memory.  To access a public static member when no objects of the class exist, use the class name and a dot (. ) followed by the static member (e.g. Math.PI )  To access a private static member when no objects of the class exist, provide a public static get method. © Copyright by Pearson Education, Inc. All Rights Reserved.

 A static method cannot access non- static class members, because a static method can be called even when no objects of the class have been instantiated.  If a static variable is not initialized, the compiler assigns it a default value. © Copyright by Pearson Education, Inc. All Rights Reserved.

Accessing static Class Members

 The principle of least privilege is fundamental to good software engineering.  Code should be granted only the amount of privilege and access that it needs to accomplish its designated task, but no more.  Makes your programs more robust by preventing code from accidentally (or maliciously) modifying variable values and calling methods that should not be accessible.  Keyword final specifies that a variable is not modifiable (i.e., it’s a constant).  e.g. private final int INCREMENT; © Copyright by Pearson Education, Inc. All Rights Reserved.

 final variables can be initialized when they are declared or by the constructor(s).  If a class provides multiple constructors, every one would be required to initialize each final variable.  A final variable cannot be modified by assignment after it’s initialized.  If a final variable is not initialized, a compilation error occurs. © Copyright by Pearson Education, Inc. All Rights Reserved.

 String objects in Java are immutable—they cannot be modified after they are created.  String concatenation actually creates a new String object containing the concatenated values.  Strings are final. © Copyright by Pearson Education, Inc. All Rights Reserved.

 A package is a group of related classes  Each class in the Java API belongs to a package.  Packages help programmers manage the complexity of application components.  Packages facilitate software reuse by enabling programs to import classes from other packages, rather than copying the classes into each program that uses them.  Packages provide a convention for unique class names, which helps prevent class-name conflicts. © Copyright by Pearson Education, Inc. All Rights Reserved.

Steps for creating a reusable class:  Declare a public class.  Choose a unique package name and add a package declaration.  In each Java source-code file there can be only one package declaration, and it must precede all other declarations and statements.  Compile the class so that it’s placed in the appropriate package directory.  Import the reusable class into a program and use the class. © Copyright by Pearson Education, Inc. All Rights Reserved.

 Placing a package declaration at the beginning of a Java source file indicates that the class declared in the file is part of the specified package.  A Java source-code file must have the following order:  package declaration (if any),  import declarations (if any),  class declaration (required). © Copyright by Pearson Education, Inc. All Rights Reserved.

 Usually a package name start with your Internet domain name in reverse order.  For example, our domain name is citytech.cuny.edu, so our package names begin with edu.cuny.citytech.  After the domain name is reversed, you can choose any other names you want for your package.  Previous example use jhtp as the next name in the package name to indicate that this class is from Java How to Program followed by an indication to Chapter 8 ( ch08 ).  In our case, packages would look something like citytech.cuny.edu.cet3640.s14.lab2 © Copyright by Pearson Education, Inc. All Rights Reserved.

 Compile the class so that it’s stored in the appropriate package.  When a Java file containing a package declaration is compiled, the resulting class file is placed in the directory specified by the declaration.  The package declaration package e du.cuny.citytech ; indicates that class should be placed in the directory edu cuny citytech  The directory names in the package declaration specify the exact location of the classes in the package.  Eclipse allows you to create packages very easily. © Copyright by Pearson Education, Inc. All Rights Reserved.

 The package name is part of the fully qualified class name.  Class Scanner name is actually java.util.Scanner  You can use the fully qualified name, or import the class and use its simple name.  If another package contains a class by the same name, the fully qualified class names should be used to differentiate them.  When your program uses multiple classes from the same package, you can import those classes with a type-import-on- demand declaration. e.g. import java.util.*; © Copyright by Pearson Education, Inc. All Rights Reserved.

 Check the class OpenLab site for new Labs  Check Blackboard for new Quizzes  Study for the Midterm  Prepare the Project Proposal © by Pearson Education, Inc. All Rights Reserved.