Java Programming, Second Edition - PowerPoint PPT Presentation

1 / 26
About This Presentation
Title:

Java Programming, Second Edition

Description:

This reference is a critical difference between types of methods. Since class methods do not have a this reference they can not be used with object variables. ... – PowerPoint PPT presentation

Number of Views:23
Avg rating:3.0/5.0
Slides: 27
Provided by: dwi89
Category:

less

Transcript and Presenter's Notes

Title: Java Programming, Second Edition


1
Java Programming, Second Edition
  • Chapter Four
  • Advanced Object Concepts

2
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

3
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

4
Understanding Scope
  • The portion of a program within which you can
    reference a variable
  • 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

5
Variable Scope
  • Multiple uses of variable names
  • You can not declare the same variable name more
    than once within nested blocks
  • You can not declare the same variable twice
    within the same method
  • If you declare a variable within a class you can
    use the same variable name within a method
  • The method name would take precedence or override
    the class variable

6
Variable Scope
  • Public class CircleClass
  • private float radius
  • private int number
  • CircleClass(float r)
  • int number 0
  • radius r

7
Overriding a Method
  • Overriding
  • 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

8
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

9
(No Transcript)
10
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

11
Ambiguity
  • public static void simpleInterest(double bal,
    double rate)
  • public static void simpleInterest(float bal,
    double rate)
  • total SimpleInterest(10000, 4.0)
  • Which function gets called for the argument
    values?
  • Ambiguous situation for the compiler

12
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

13
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

14
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 objects 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

15
this Reference
  • this reference
  • this is a keyword
  • Implicitly passed to the object
  • Does not have to be explicitly used
  • When you pass a reference you are passing a
    memory address
  • this points to the instance of the objects data
    members

16
Code Example - Methods
  • public int getEmpNum( )
  • return empNum
  • public int getEmpNum( )
  • return this.empNum

17
Code Example - Constructors
  • public CircleClass( double radius )
  • this.radius radius
  • public CircleClass( )
  • this( 1.0 )

18
this Reference Methods
  • This reference is a critical difference between
    types of methods
  • Since class methods do not have a this reference
    they can not be used with object variables.
  • Class methods can not access the instance data of
    an object (class methods can only access class
    data (defined with the keyword static)
  • Object methods are more flexible
  • All objects share a class method

19
(No Transcript)
20
Class Variables
  • Class variables- Variables that are shared by
    every instantiation of a class

21
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
  • final short COMPANY_ID 12345

22
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

23
Using Automatically Imported, Prewritten
Constants and Methods
  • 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, and Double
  • Optional classes Must be explicitly named

24
Using Prewritten Imported Methods
  • To use any of the prewritten classes (other than
    java.lang)
  • Use the entire path with the class name
  • OR
  • Import the class
  • OR
  • Import the package which contains the class you
    are using

25
Using Prewritten Imported Methods
  • To import an entire package of classes use the
    wildcard symbol
  • For example
  • import java.util.
  • import java.io.
  • Represents all the classes in a package

26
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()
Write a Comment
User Comments (0)
About PowerShow.com