Java Programming, Second Edition Chapter Four Advanced Object Concepts.

Slides:



Advertisements
Similar presentations
Core Java Lecture 4-5. What We Will Cover Today What Are Methods Scope and Life Time of Variables Command Line Arguments Use of static keyword in Java.
Advertisements

 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
1 Chapter Three Using Methods. 2 Objectives Learn how to write methods with no arguments and no return value Learn about implementation hiding and how.
Variable types We have already encountered the idea of a variable type. It is also possible to think about variables in terms of their kind, namely: 1)
 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Road Map Introduction to object oriented programming. Classes
VBA Modules, Functions, Variables, and Constants
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
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.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
Applying OO Concepts Using Java. In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The.
Day 4 Objectives Constructors Wrapper Classes Operators Java Control Statements Practice the language.
Review of C++ Programming Part II Sheng-Fang Huang.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
LESSON 2 CREATING A JAVA APPLICATION JAVA PROGRAMMING Compiled By: Edwin O. Okech [Tutor, Amoud University]
Chapter 8 More Object Concepts
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
More Object Concepts Chapter 4.  Our class is made up of several students and each student has a name and test grades  How do we assign the variables.
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.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Chapter 8: Arrays.
1 Java Programming Using Methods, Classes, and Objects.
 2005 Pearson Education, Inc. All rights reserved Methods: A Deeper Look.
Methods in Java. Program Modules in Java  Java programs are written by combining new methods and classes with predefined methods in the Java Application.
Slides prepared by Rose Williams, Binghamton University Chapter 5 Defining Classes II.
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.
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
Visual C# 2012 for Programmers © by Pearson Education, Inc. All Rights Reserved.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo CET 3640 © Copyright by Pearson Education, Inc. All Rights Reserved.
Chapter 3 Introduction To Java. OBJECTIVES Packages & Libraries Statements Comments Bytecode, compiler, interpreter Outputting print() & println() Formatting.
1 Chapter Four Creating and Using Classes. 2 Objectives Learn about class concepts How to create a class from which objects can be instantiated Learn.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
1 Chapter 5: Defining Classes. 2 Basics of Classes An object is a member of a class type What is a class? Fields & Methods Types of variables: –Instance:
More Object Concepts— Farrell, Chapter 4 Dr. Burns.
Chapter 8: Advanced Method Concepts. Understanding Parameter Types Mandatory parameter – An argument for it is required in every method call Four types.
Java Classes Chapter 1. 2 Chapter Contents Objects and Classes Using Methods in a Java Class References and Aliases Arguments and Parameters Defining.
Java Programming, Second Edition Chapter Twelve Advanced Inheritance Concepts.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Chapter 5 Defining Classes II Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Java Programming, Second Edition Chapter Three Using Methods, Classes, and Objects.
In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The import statement and using prewritten.
Chapter 4: More Object Concepts. Objectives Understand blocks and scope Overload a method Avoid ambiguity Create and call constructors with parameters.
Chapter 7 - Functions. Functions u Code group that performs single task u Specification refers to what goes into and out of function u Design refers to.
Object-Oriented Programming: Classes and Objects Chapter 1 1.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
JAVA MULTIPLE CHOICE QUESTION.
Chapter 4 Assignment Statement
3 Introduction to Classes and Objects.
Java Primer 1: Types, Classes and Operators
Yanal Alahmad Java Workshop Yanal Alahmad
Chapter 3: Using Methods, Classes, and Objects
CMSC 202 Static Methods.
Chapter 3 Introduction to Classes, Objects Methods and Strings
Can perform actions and provide communication
MSIS 655 Advanced Business Applications Programming
Interfaces.
Classes and Objects 5th Lecture
Can perform actions and provide communication
Applying OO Concepts Using Java
Classes and Objects Static Methods
Object Oriented Programming in java
Presentation transcript:

Java Programming, Second Edition Chapter Four Advanced Object Concepts

In this chapter, you will:  Understand blocks and scope  Overload a method  Learn about ambiguity  Send arguments to constructors  Overload constructors  Learn about the this reference  Work with constants  Use automatically imported, prewritten constants and methods  Use prewritten imported methods  Learn about Gregorian calendars

Understanding Blocks  Blocks-Within any class or method, the code between a pair of curly braces  Outside block- The first block, begins immediately after the method declaration and ends at the end of the method  Inside block- The second block, contained within the second pair of curly braces  The inside block is nested within the outside block

Understanding Scope  The portion of a program within which you can reference a variable is its scope  A variable comes into existence, or comes into scope, when you declare it  A variable ceases to exist, or goes out of scope, at the end of the block in which it is declared  If you declare a variable within a class, and use the same variable name within a method of the class, then the variable used inside the method takes precedence, or overrides, the first variable

Overloading a Method Overloading:  Involves using one term to indicate diverse meanings  Writing multiple methods with the same name, but with different arguments  Overloading a Java method means you write multiple methods with a shared name

Learning about Ambiguity  When you overload a method you run the risk of ambiguity  An ambiguous situation is one in which the compiler cannot determine which method to use  Example on page 107 of text

Sending Arguments to Constructors  Java automatically provides a constructor method when you create a class  Programmers can write their own constructor classes  Programmers can also write constructors that receive arguments  Such arguments are often used for initialization purposes when values of objects might vary

Overloading Constructors  If you create a class from which you instantiate objects, Java automatically provides a constructor  But, if you create your own constructor, the automatically created constructor no longer exists  As with other methods, you can overload constructors  Overloading constructors provides a way to create objects with or without initial arguments, as needed

Learn about the this Reference  Classes can become large very quickly  Each class can have many data fields and methods  If you instantiate many objects of a class, the computer memory requirements can become substantial  It is not necessary to store a separate copy of each variable and method for each instantiation of a class  The compiler accesses the correct object’s data fields because you implicitly pass a this reference to class methods  Static methods, or class methods, do not have a this reference because they have no object associated with them

Class Variables  Class variables- Variables that are shared by every instantiation of a class  Company Name = “ABC Company”  Every employee would work for the same company static private String COMPANY_ID = “ABC Company”;

Working with Constants Constant variable:  A variable or data field that should not be changed during the execution of a program  To prevent alteration, use the keyword final  Constant fields are written in all uppercase letters  For example:  COMPANY_ID static final double SALES_TAX =.07;

Using Automatically Imported, Prewritten Constants and Methods  The creators of Java created nearly 500 classes  For example:  System, Character, Boolean, Byte, Short, Integer, Long, Float, and Double are classes  These classes are stored in a package, or a library of classes, which is a folder that provides a convenient grouping for classes

 java.lang – The package that is implicitly imported into every Java program and contains fundamental classes, or basic classes  Fundamental classes include:  System, Character, Boolean, Byte, Short, Integer, Long, Float, Double, String  Optional classes – Must be explicitly named Using Automatically Imported, Prewritten Constants and Methods

Using Prewritten Imported Methods  To use any of the prewritten classes (other than java.lang):  Use the entire path with the class name area = Math.PI * radius * radius; OR  Import the class OR  Import the package which contains the class you are using

Using Prewritten Imported Methods  To import an entire package of classes use the wildcard symbol *  For example:  import java.util.*;  Represents all the classes in a package

The Math class  Accessible with Math. and the name of the constant or class (Math.PI, Math.sin(x)) abs(x)acos(x)asin(x)atan(x)atan2(x,y)ceil(x)cos(x)exp(x)floor(x)log(x)max(x,y)min(x,y)pow(x)random()rint(x)round(x)sin(x)sqrt(x)

Learning about the Gregorian Calendar  The Gregorian calendar is the calendar used in most of the western world  There are seven constructors for GregorianCalendar objects  The default creates a calendar with the current date and time in the default locale  You can use other constructors to specify the year, month, day, hour, minute, and second  You create a calendar object with the default constructor GregorianCalendar calendar = new GregorianCalendar();