Objects and Things
2
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671
Agenda
✦ Announcements
✦ Grade Reports and Midterms ✦ Review Lab
✦ Break
✦ Exception Handling
Review Java Syntax (Lab)
• Create a new project called Review
• Create a package called edu.aubih.review • Create a new class in the package called
– HelloWorld
• Write HelloWorld program from scratch • Write Circle class from scratch
4
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671
HelloWorld
• HelloWorld program should
– output Hello World! when invoked – define the Hello World message in a
Circle
• Circle program should
– declare radius as instance variable (private) – create getters and setters for the radius
– define a no-arg constructor that sets the
radius to 1.0
– define a constructor that accepts a double as
input and set the radius to that value
6
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671
Statistics
• Statistics program should
– calculateMean(double[] array) as class
method
– calculateMedian(double[] array) as class
method
– calculateMax(double[] array) as class
method
Test Your Code
• Verify that your code works • Run sample data
• Try to break it
• Use the main method to test each of your
objects
8
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671
Exception Handling
• Why exception handling important?
– quality software demands it – secure software demands it
• runtime errors cause program to terminate
abnormally
Runtime error example (p. 626)
• Write a program that accepts two integers as
input (Quotient.java)
• the program should divide the two numbers
and display the result to the user
• After testing it, see what happens if you use
10
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671
Runtime error example
• java.lang.ArithmeticException
• These error messages confuse users • They also can result in more serious
problems – security, etc
Prevent the Error (p. 626-627)
• Adjust Quotient.java to prevent the divide
by zero error.
• This is a perfectly acceptable solution
• In some cases, however, it is not so simple
to prevent errors.
• For those cases, Java provides exception
12
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671
Handle the Error
try{
// code to execute }
catch(type ex){ // exception we handle // code to handle exception
Handle the Error
• Rewrite the divide program with Java try
catch block (p. 627)
• If you have time, rewrite the divide program
14
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671
Exception Handling Advantages
• Exception handling enables a method to
throw an exception to its caller.
• Without this capability, a method must
Common Exceptions
• ClassNotFoundException
– use a class that does not exist
• IOException
– input/output problem: file does not exist
• ArithmeticException
– divide by zero
• NullPointerException
16
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671
Finally...
• Sometimes you want code to execute
regardless of whether an exception occurs or not
finally{ } block accomplishes this...
• Often used to clean up resources
– close file handles
Objects and Things
• Additional Keywords: this, protected • Encapsulation
• Inheritance
• Overriding vs. Overloading • Polymorphism
18
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671
This keyword
• How does an object refer to itself?
– this
• Common uses
– reference a class's hidden data fields
– invoke another constructor of the same class
• Example:
Design and Test Course Class
• Write the programs to model a class on
pages 343 and 342
• The Course class models the process of
registering a student in a class
• Review the following concepts:
– state and behavior
20
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671
Inheritance
• Inheritance allows new classes to be derived
from existing classes
• Every class you define in Java is inherited
from an existing class
• Think of shapes: square and circle • They have many things in common
• Can we use these common features to
Inheritance Concepts
• subclass: a subclass inherits accessible
fields and methods from its superclass
• subclass also called: child class, extended
class, derived class
22
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671
Modeling Geometric Objects
• Write code on pp. 359 – 362.
• Note that the keyword “extends” defines an
inheritance relationship
public class Circle4 extends GeometricObject1
• Therefore, if we want a class to be derived from another, we simply extend it in the class definition
Overriding vs. Overloading
• Overloading methods: methods with the
same name but different signatures
• Overriding methods: methods in a subclass
with the same method signature as methods in a superclass
• A method override provides different
24
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671
Polymorphism
• a variable of a supertype can refer to a
subtype object
• an object of a subclass can be used
whenever its superclass is required
• The JVM uses dynamic binding to
determine which implementation will be used
• Dynamic binding allows new classes to be
Polymorphism Example
• Write the code on page 369-370
• Notice that we can invoke m with any type
26
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671
Protected data and methods
• protected is a visibility modifier like public
and private
• protected resources can be accessed by any
class in the same package or its subclasses
• Three visibility modifiers
– public
Another use for final
• If you want methods or classes not to be
extended, use the final modifier
• public final class C will prevent extensions
to this class
• public final void m() will prevent the m
28
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671
Abstraction
• Abstract class cannot have any specific
instances
• Abstract classes are like regular classes
except that you cannot create an instance with the new operator
• Abstract methods represent behavior that is
only appropriate for subclasses
Abstraction Example
• Write new version of GeometricObject,
Circle, Rectangle, and
30
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671
Interfaces
• An interface is a classlike construct that
contains only constants and methods.
• Interfaces specify the behavior that a class
must implement (contract)
class Chicken extends Animal implements Edible
• implements keyword denotes using
Interface Example
• Write code on page 397 – 398 to
demonstrate how interfaces work
• If you want to implement features similar to
32
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671
Interfaces vs. Abstractions
• Interfaces
– variables must be public static final – no constructors
– all methods are public instance methods
• Abstraction
– all variables permitted
Class Design and Modeling
• Association
– Students take courses – Faculty teach courses
• Aggregation – has-a relationship
• Composition – exclusive has-a relationship • Dependency – one class uses another
34
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671
Class Design and Modeling
• Cohesion: classes should describe single
entities and class operations should fit together logically
• Consistency: choose names consistently,
follow a standard
• Encapsulation: data protection
Class Design and Modeling
• Completeness: classes can be configured for
many purposes
• Instance vs. Static
• Inheritance vs. Aggregation: prefer
composition whenever possible
36
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671
Design Patterns
• Design patterns are proven sound strategies
for designing classes.
• Help to arrange classes into meaningful
software solutions.
• Exercise: Write singleton pattern on page
442 – 443.