Java Programming Fifth Edition: More Object Concepts
Java Programming Fifth Edition: More Object Concepts
Fifth Edition
Chapter 4
More Object Concepts
Objectives
• Blocks
– Use opening and closing curly braces
– Can exist entirely within another block or entirely
outside and separate from another block
– Cannot overlap
– Types
• Outside or outer blocks
• Inside or inner blocks
• Nested
• Overloading
– Using one term to indicate diverse meanings
– Writing multiple methods with same name but with
different arguments
– Compiler understands meaning based on arguments
used with method
– Convenience for programmers to use one
reasonable name
• For tasks that are functionally identical
• Except for argument types
• Overload methods
– Correctly provide different argument lists for
methods with same name
• Illegal methods
– Methods with identical names that have identical
argument lists but different return types
• Class methods
– Do not have this reference
– Have no object associated with them
• Class variables
– Shared by every instantiation of class
– Only one copy of static class variable per class
• Composition
– Describes relationship between classes when object
of one class data field is within another class
– Called has-a relationship
• Because one class “has an” instance of another
• Remember to supply values for contained object if
it has no default constructor
• Demonstrating scope
• Overloading methods
• Creating a constructor that requires an argument
• Using an explicitly imported prewritten class
• Creating an interactive application with a timer
• Variable’s scope
– Portion of program in which you can reference
variable
• Block
– Code between a pair of curly braces
• Overloading
– Writing multiple methods with same name but
different argument lists
• Store separate copies of data fields for each object
– But just one copy of each method
Java Programming, Fifth Edition 47
Summary (continued)
• static class variables
– Shared by every instantiation of a class
• Literal constants never change
• Prewritten classes
– Stored in packages
• import statement
– Notifies Java program that class names refer to
those within imported class