Introduction to Java 2 Programming - PowerPoint PPT Presentation

About This Presentation
Title:

Introduction to Java 2 Programming

Description:

java intro2java.Person ... Encapulation is enforced in Java by the visibility modifiers ... Use the throw keyword to ask Java to deal with it. throw new ... – PowerPoint PPT presentation

Number of Views:88
Avg rating:3.0/5.0
Slides: 37
Provided by: Leigh86
Category:

less

Transcript and Presenter's Notes

Title: Introduction to Java 2 Programming


1
Introduction to Java 2 Programming
  • Lecture 5
  • Quick Recap Error-handling

2
Overview
  • Quick Recap
  • Were half way through!
  • Review some of the material weve already covered
  • Walk through some of the exercises
  • Java Error Handling
  • Handling and creating Exceptions

3
The CLASSPATH
  • The CLASSPATH is
  • How Java finds compiled classes
  • A system property
  • Contains directories and/or jar files
  • Is a common source of frustration!
  • Classify Java tools into three groups
  • File based
  • CLASSPATH based
  • And mixed mode (I.e. used both)

4
The CLASSPATH
  • File based tools (e.g. javadoc, jar)
  • Accept parameters referring to files.
  • Read the directory structure to find related
    files
  • javadoc c\intro2java\src\intro2java\.java
  • Result in file-based errors
  • CLASSPATH based tools (e.g. java, javap)
  • Refer to classes and not files
  • Ignore the file-system, except for those
    directories mentioned in the CLASSPATH
  • java intro2java.Person
  • Result in exceptions or errors, e.g.
    ClassNotFoundException NoClassDefFoundErrors

5
The CLASSPATH
  • Mixed mode tools (e.g. javac)
  • Accept parameters referring to files
  • Read the CLASSPATH to find related classes
  • javac c\intro2java\src\intro2java\.java
  • Results in file errors (relating to parameters),
    and cannot resolve symbol errors (relating to
    missing classes)

6
CLASSPATH Tips
  • Always add the current working directory to the
    CLASSPATH
  • SET CLASSPATHCLASSPATH.
  • Keep classes and source separate (e.g. bin and
    src directories)
  • Use a global classes directory, e.g. c\classes
  • Add this to the CLASSPATH
  • Always compile into that directory
  • SET CLASSPATHCLASSPATHc\classes
  • javac d c\classes .java

7
Constructors
  • Why do we use them?
  • Give us chance to ensure our objects are properly
    initialised
  • Can supply default values to member variables
  • Can accept parameters to allow an object to be
    customised
  • Can validate this data to ensure that the object
    is created correctly.
  • A class always has at least one constructor
  • even if you dont define it, the compiler will
  • This is the default constructor

8
Constructors
  • public class EmailAddress
  • public EmailAddress(String address)
  • if (address.indexOf(_at_) -1)
  • //not a valid address, signal an error?
  • //must be valid
  • //methods

9
Constructors
  • When do they get called?
  • When we use the new keyword
  • Cannot be called explicitly like other methods
  • But one constructor can refer to another, using
    the super keyword
  • EmailAddress myAddress new EmailAddress(leigh_at_l
    dodds.com)
  • Constructors and inheritance
  • An object must ensure that its parent is properly
    initialised, by calling one of its constructors
  • Example

10
Constructors
  • public class Parent
  • private String msg
  • public Parent(String message)
  • msg message
  • public class Child  
  • public Child() 
  •    
  • super("Parent message") 

11
Inheritance
  • What happens when we call a method?
  • The JVM tries to find the implementation of that
    method and execute the code
  • Starts searching with the objects class, then
    works upward to its parentits parents
    parentetc, etc.
  • When do we override a method?
  • When we want to change the implementation, either
    completely or partially
  • To completely change the implementation, just
    define a new version
  • To partially change the implementation use the
    super keyword to refer to the parent
    implementation

12
Inheritance Using Super
  • In an E-Commerce system we might have a class
    responsible for calculating prices e.g 
  • public class Pricer
  • public float calculatePrice(Product p)
  •  
  • //implementation details irrelevant for
    example
  • The Product parameter lets the Pricer calculate
    the price

13
Inheritance Using Super
  • Assume we want to extend this functionality so
    that the calculation also includes tax (i.e.
    adding on VAT).
  • US purchases don't include tax, but UK ones do,
    so we can't just rewrite the original method.
  • Instead we create a subclass, called UKPricer
    that does the extra work. 
  • public class UKPricer extends Pricer
  • public float calculatePrice(Product p)
  •  
  • //this implementation will also add on VAT

14
Inheritance Using Super
  • Ideally we want to reuse the code in the base
    class, as all we need to do is add on the extra
    17.5 to the final price.
  • We can do better than copy-and-paste using super
  • public class UKPricer extends Pricer
  • public float calculatePrice(Product p)
  •  
  •   //call the superclass method  
  • float withoutTax super.calculatePrice(p)  
  • float tax withoutTax 17.5 / 100 
  • return withoutTax tax

15
Controlling Inheritance
  • Inheritance can be restricted using the final
    keyword
  • Applies to both methods and classes
  • A final class cannot be extended
  • A final method cannot be overridden
  • Inheritance can be forced by using the abstract
    keyword
  • Again applies to both methods and classes
  • An abstract class must be extended, cannot be
    used to create objects
  • An abstract method must be overrided by a
    sub-class

16
Encapsulation
  • Encapulation is information hiding, but what to
    hide?
  • Everything that isnt part of the interface
    (contract) of an object
  • This means all member variables
  • Encapulation is enforced in Java by the
    visibility modifiers
  • Public, protected, private, and package access
  • Modifiers can be applied to variables, methods
    and classes

17
Visibility Modifiers
18
Passing Parameters
  • Java has two ways of passing parameters
  • Pass by Reference
  • Pass by Value
  • Pass by Value applies to primitive types (int,
    float, etc)
  • Pass by Reference applies to objects and arrays

19
Passing Parameters
  • public class PassByValueTest
  • public static void add(int x)
  • x
  • public static void main(String args)
  • int x 0
  • System.out.println(x)
  • add(x)
  • System.out.println(x)

20
Passing Parameters
  • public class PassByReferenceTest
  • public static void reverse(StringBuffer buffer)
  • buffer.reverse()
  • public static void main(String args)
  • StringBuffer buffer new StringBuffer(Hello
    )
  • System.out.println(buffer)
  • reverse(buffer)
  • System.out.println(buffer)

21
Upcasting
  • Inheritance describes an isA relationship
  • E.g. A MotorVehicle isA type of Vehicle
  • Parent and child objects share a base type
  • So children can be treated as if they were an
    instance of the parent
  • This is known as upcasting

22
Upcasting
  • The type of the object determines its possible
    behaviours
  • Because we define them in the class
  • The object reference limits the behaviours we can
    invoke to those defined by the type of the
    reference
  • If an child class is upcast we can only invoke
    the base class methods
  • Weve forgotten the real type of the object

23
Upcasting Example 1
  • Parent and Child

24
Upcasting Example 2
  • PersonViewer, need to change references
  • Key to understanding this is that the compiler
    doesnt determine the method to call at compile
    time, but at runtime dynamic binding.
  • This is Polymorphism

25
Method Overloading
  • Already introduced overriding
  • Overloading is defining methods with the same
    name, but different parameters
  • Enhanced Person Viewer example

26
Error Handling in Java
  • Java uses Exceptions to handle errors
  • Events that happen whilst running a program that
    disrupts the normal flow of instructions
  • Exceptions can range in severity
  • Hardware or memory problems
  • Program errors Bugs, etc.
  • Exceptions are objects
  • Carry message, and state about what the program
    was doing when the problem was encounter
  • Because Exceptions are objects, you can subclass
    them to create your own

27
Exceptions
  • Exceptions are handled by separate blocks of code
    (exception handlers)
  • Separates error handling from normal code
  • Better than return codes
  • JVM is responsible for finding an error handler
    for each exception
  • Walks backwards through the call stack
  • If it cant find one, then the JVM will exit

28
Exceptions Terminology
  • Throwing an exception
  • Creating an Exception object, and asking the JVM
    to deal with it
  • Catching an exception
  • Dealing with the Exception object.
  • Exception handlers catch exceptions
  • Stack Trace
  • Debug information about the application state
    (call stack) when Exception was generated.

29
Exceptions Syntax
  • Throwing an Exception
  • Create a new Exception object (actually one of
    its subclasses)
  • Use the throw keyword to ask Java to deal with it
  • throw new Exception(error message)

30
Exceptions Syntax
  • Identifying methods that throw exceptions
  • Added a throws keyword to the method definition
  • public void readFile throws IOException,
    FileNotFoundException
  • //..code...
  • A method must list all exceptions that are thrown
    in the method body
  • Otherwise encounter compilation errors

31
Exceptions Syntax
  • Throwing an Exception
  • Create a new Exception object (actually one of
    its subclasses)
  • Use the throw keyword to ask Java to deal with it
  • throw new Exception(error message)

32
Exceptions Syntax
  • Handling exceptions
  • Use a try..catch block
  • try
  • readFile()
  • catch (FileNotFoundException fe)
  • //handler for file not found
  • catch (IOException ioe)
  • //handler for general I/O exceptions

33
Exceptions Syntax
  • Alternatively use a try..catch..finally block
  • The finally clause is always executed and is used
    for critical clean-up
  • try
  • readFile()
  • catch (IOException ioe)
  • //handler for general I/O exceptions
  • finally
  • //ALWAYS run
  • closeFile()

34
Exceptions Syntax
  • Viewing the stack trace
  • Exception.printStackTrace()
  • Example

35
Exceptions Syntax
  • java.lang.Exception Error message
  • at intro2java.GenerateStackTrace.methodThr
    ee(GenerateStackTrace.java18)
  • at intro2java.GenerateStackTrace.methodTwo
    (GenerateStackTrace.java13)
  • at intro2java.GenerateStackTrace.methodOne
    (GenerateStackTrace.java8)
  • at intro2java.GenerateStackTrace.main(Gene
    rateStackTrace.java27)

36
Exceptions Syntax
  • Defining new types of Exceptions is easy, just
    sub-class an existing one.
  • public MyException extends Exception
  • public MyException()
  • public MyException(String msg) super(msg)
Write a Comment
User Comments (0)
About PowerShow.com