Download Java Code Containers class interface enumeration (enum keyword

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Java Code Containers
 class
 interface
 enumeration
(enum keyword)
Contents of a Class
 fields
o instance variables
o class variables (static keyword)
 methods
 enumerations
 inner classes (such as the event handler classes of Chapter 14)
Contents of an Interface
 public abstract methods
 public static final constants
Access Modifiers
 public
 private
 protected
 “package” (absence of an access modifier)
Package
 A collection of related classes in the same folder
“package” access
 public to all classes in the same folder (no access modifier in class heading
package Keyword
 package keyword causes a folder to be created to store the class created from a Java source file
o package keyword was used in Chapter 17 for the class ItemRecord
Java Relationships
 is-a
 has-a
 uses-a
inheritance relationship
composition relationship
use of a public static method in a class
a SalariedEmployee is-a Employee
a GradeBook has-a String object
Math.pow(2.0, 3.0)
extends Keyword
 public class HourlyEmployee extends Employee
HourlyEmployee inherits directly from Employee
implements Keyword
 public class Invoice implements Payable
Class Invoice must implement the interface Payable
Polymorphism
 Classes can have a common method that is implemented differently in each class.
o Employee / HourlyEmployee / SalaryEmployee may all have a method called earnings
 Polymorphism is achieved by using a superclass reference to a subclass object
Class Categories
 abstract
 concrete
 final
An instance of the class can NOT be created
A class can be used to create an instance of the class (an object)
A final class can not be used to create a subclass
Downcast a reference
//superclass reference to a subclass object

Employee employee = new BasePlusCommissionEmployee();

if ( employee instanceof BasePlusCommissionEmployee ) //test to see if BPCE
{
BasePlusCommissionEmployee bpce = ( BasePlusCommissionEmployee ) employee;
bpce.setBaseSalary( bpce.getBaseSalary() * 1.05 );
//cast
}
Test for an Inheritance Relationship Using instanceof Operator
 hourlyEmployee instanceof HourlyEmployee
 hourlyEmployee instanceof Employee
 basePlusCommissionEmployee instanceof CommissionEmployee
 basePlusCommissionEmployee instanceof Employee
Exception Handling
 “checked” Exceptions
o all exception classes that are NOT a subclass of RuntimeException
o checked exceptions must be caught or declared to be thrown
o Exceptions can be caught using try / catch / finally blocks
 “unchecked” Exceptions
o Exception class and all subclasses of it EXCEPT for the subclasses of RuntimeException
Exception Handling Keywords
 throw
o throw new ArithmeticException(); //throw an anonymous reference to an ArithmeticException object
 throws
o //this fulfills Java’s requirement that a “checked” exception must be “caught or declared to be thrown”
public static void main( String[ ] args ) throws FileNotFoundException //declare to be thrown