Advanced Java Concepts - PowerPoint PPT Presentation

1 / 13
About This Presentation
Title:

Advanced Java Concepts

Description:

{ public void manufacture() { System.out.println( 'Making a sports ball. ... ball.manufacture(); g1( ball ); g2( ball ); g3( ball ); 8. Exceptions ... – PowerPoint PPT presentation

Number of Views:1833
Avg rating:3.0/5.0
Slides: 14
Provided by: carbonC
Category:

less

Transcript and Presenter's Notes

Title: Advanced Java Concepts


1
Advanced Java Concepts
  • Chapter 4
  • Class members
  • Garbage collection
  • Packages
  • Chapter 5
  • Inheritance
  • Interfaces
  • Chapter 6
  • Exceptions
  • Chapter 9
  • Nested and inner classes

2
Class Members
  • package examples.classes
  • public class Counted
  • private int value
  • private static int count 0 // simple init
    clause
  • public Counted()
  • count
  • System.out.println( "Creating a "
    "Counted object" )
  • value -1
  • public void finalize()
  • --count
  • System.out.println( "Destroying a "
    "Counted object" )
  • public static int getCount() return count
  • public Counted setValue( int newValue )
  • value newValue
  • return this
  • public static void main( String args )
  • Counted a new Counted()
  • Counted b new Counted()
  • Counted c new Counted()
  • System.out.println( "There are "
    Counted.getCount() " Counted objects" )
  • Counted d new Counted()
  • System.out.println( "There are "
    Counted.getCount() " Counted objects" )
  • a null
  • // start garbage collection
  • System.gc()
  • // execute finalize methods of deleted objects
  • System.runFinalization()
  • System.out.println( "There are " a.getCount()
    " Counted objects" )
  • / without the next three method calls, the
  • objects b, c, and d would be picked up in
  • the preceding garbage collection
  • /
  • b.setValue( 1 )

3
Packages
  • Package is a folder for storing classes into
    logical groupings.
  • Subpackages (subfolders) may be used to
    hierarchically arrange packages. This
    hierarchical structure does not reflect an
    inheritance hierarchy.
  • A specific class of a package is denoted by
    package_name.class_name.
  • The import directive is used as a convenient way
    to tell the java compiler where class definitions
    are stored in addition to the current directory.
    Import is not the same as the include directive
    in C. For example import java.util.Vector
    and import java.util.
  • Table 4-3 shows the core java packages.

4
Inheritance
  • public class Vehicle
  • private int wheels, range
  • public Vehicle( int w, int r )
  • wheels w
  • range r
  • public String toString()
  • return ( "Wheels " wheels
  • " Range " range )
  • public class Car extends Vehicle
  • private static final int NUMBER_OF_WHEELS 4
  • private int passengers
  • public Car( int p, int r )
  • super( NUMBER_OF_WHEELS, r )
  • passengers p
  • public String toString()
  • return( super.toString() " Passengers "
    passengers )

5
Inheritance
  • Private, public and protected are the same as in
    C
  • Dynamic Binding Inherited methods that the
    programmer overrides are automatically virtual
    methods.
  • Keyword, super, is used to invoke explicitly a
    method belonging to a parent. e.g.
    "super.method()"
  • "Abstract" classes are denoted by qualifying the
    class definition and pure virtual methods are
    also qualified as "abstract." In C abstract
    classes are indicated by "pure virtual
    functions."
  • Packages are used to implement "friend"
    relationship and organize related classes. See
    Table 5.1. E.g., classes belonging to the same
    package can access each other's protected
    attributes and methods.
  • Multiple inheritance is accomplished via
    interfaces.

6
Interface
  • Interface defines the public methods that must be
    provided by classes that implement the interface.
  • The defined methods are abstract.
  • Interface may define attributes that are static
    and final.
  • Interfaces may be derived from other interfaces.
    (Inheritance)
  • A class may implement one or more interfaces.
  • An object that implements an interface is deemed
    to be of that object type.

7
Interface
  • public static void g1( Inflatable x )
  • x.inflate()
  • public static void g2( Kickable y )
  • y.kick()
  • public static void g3( SportsBall z )
  • z.manufacture()
  • public static void main( String args )
  • SoccerBall ball new SoccerBall()
  • ball.inflate()
  • ball.kick()
  • ball.manufacture()
  • g1( ball )
  • g2( ball )
  • g3( ball )
  • interface Inflatable
  • public void inflate()
  • interface Kickable
  • public void kick()
  • class SportsBall
  • public void manufacture()
  • System.out.println( "Making a sports ball."
    )
  • public class SoccerBall extends SportsBall
  • implements Inflatable, Kickable
  • public void inflate()
  • System.out.println( "Inflating a soccer
    ball." )
  • public void kick()
  • System.out.println( "Kicking a soccer
    ball." )

8
Exceptions
  • Objects that specify a throws clause throw an
    exception to indicate an error condition to the
    caller.
  • Custom exception objects should be derived from
    the class java.lang.Exception.
  • Exception objects implement the methods
  • String getMessage()
  • void printStackTrace().
  • The caller of an object that throws exceptions
    uses a try block to invoke a method and a catch
    clause to capture the exception if one occurs.

9
An Object that Throws Exceptions
  • class MathException extends Exception
  • public MathException(String s) super (s)
  • public class Fraction
  • private int num // fraction numerator
  • private int den // fraction denominator
  • public Fraction( int initNum, int initDen )
  • throws MathException
  • if ( 0 initDen )
  • throw new MathException("Zero
    Denominator")
  • num initNum
  • den initDen
  • Fraction divideBy( Fraction divisor )
  • throws MathException
  • if ( 0 divisor.num )
  • throw new MathException("Division by
    Zero" )
  • public static void main( String args )
  • try
  • Fraction a new Fraction( -102, 6 )
  • Fraction b new Fraction( 0, 1)
  • Fraction c a.divideBy( b )
  • catch( MathException x )
  • System.out.println(x.getMessage())
  • x.printStackTrace( )
  • Output
  • Division by Zero
  • packageFraction.MathException Division by Zero
  • packageFraction.Fraction packageFraction.Fraction
    .divideBy(packageFraction.Fraction)
  • void packageFraction.Fraction.main(java.lang.Stri
    ng)

10
Nested and Inner Classes
  • A nested class is virtually the same as any other
    class definition.
  • An inner class has access to the attributes of
    the outer class.
  • A local inner class is nested within a method of
    the outer class is available only to that method
    -- a helper class for that method only.
  • Anonymous local inner classes are nested within a
    method and are unnamed.
  • Nested and inner classes are great for building
    helper classes or one-time-use classes.

11
Nested Class
  • public class Employee
  • private Name name
  • private int id
  • public Employee (String first, String last, int
    i)
  • id i
  • name new Name(first, last)
  • public static void main (String args)
  • Employee emp1 new Employee ("John", "Smith",
    123)
  • Employee emp2 new Employee ("Sue", "Johnson",
    321)
  • private static class Name
  • private String first, last
  • public Name (String f, String l)
  • first f
  • last l
  • // end of Name class
  • // end of Employee class

12
Inner Class
  • public class Employee2
  • private Name2 name
  • private int id
  • public Employee2 (String first, String last, int
    i)
  • id i
  • new Name2(first, last)
  • public static void main (String args)
  • Employee2 emp1 new Employee2 ("John", "Smith",
    123)
  • Employee2 emp2 new Employee2 ("Sue", "Johnson",
    321)
  • // notice removal of keyword static
  • private class Name2
  • private String first, last
  • public Name2 (String f, String l)
  • first f
  • last l
  • name this // Class name2 is accessing private
    attribute of Employee2
  • // end of Name2 classs

13
Local Inner Class
  • What if we nested the Name class within the
    Employee constructor?
Write a Comment
User Comments (0)
About PowerShow.com