basicjava4

Upload: kishored

Post on 31-May-2018

220 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/14/2019 BasicJava4

    1/24

    Advanced Class Features

  • 8/14/2019 BasicJava4

    2/24

    Objectives

    Describe static variables, methods andinitializers

    Describe final classes, methods and variables

    Abstract classes and methods

    Inner classes

    Static and non-static inner classes

    interface

  • 8/14/2019 BasicJava4

    3/24

    The static Keyword

    The static keyword is used as a modifier onvariables and inner classes

    The static keyword declares the attribute ormethod is associated with the class as awhole rather than any particular instance of

    that class Thus static members are often called as

    class members

  • 8/14/2019 BasicJava4

    4/24

    Class Attributes

    Are shared among all instances of a class

    public class Count{

    private int serialNumber;public static int counter=0;

    public static int getTotalCount(){

    return counter;

    }

    public Count(){

    counter++;

    serialNumber=counter;

    }

    }

  • 8/14/2019 BasicJava4

    5/24

    The static Keyword

    Static method cannot use this

    They cannot access any non-static fields

    A static method cannot be overridden

  • 8/14/2019 BasicJava4

    6/24

    Static Initializers

    A class can contain code in a static block that doesnot exist within a method body

    Static code block executes only once when the classis loaded

    A static block is used to initialize static attributes

    publicclass Count{

    publicstaticint counter;

    static{

    counter=Integer.getInteger(myCounter).intValue();

    }

    }

    //use java DmyCounter=45 Test to execute

  • 8/14/2019 BasicJava4

    7/24

    The final Keyword

    You cannot subclass a finalclass

    You cannot override a finalmethod

    A finalvariable is a constant A final variable can only be set once, but that

    assignment can occur independently of thedeclaration;

    A blank final instance attribute must be set in everyconstructor

    A blank final method variable must be set in the methodbody before being used

  • 8/14/2019 BasicJava4

    8/24

    Abstract Classes

    Any class with one or more abstract methods

    is called an abstract class Abstract class cant be instantiated

    Abstract classes may have data attributes,

    concrete methods and constructors It is a good practice to make the constructors

    protected

  • 8/14/2019 BasicJava4

    9/24

    Interfaces

    A public interface is a contract between client code

    and the class that implements that interface

    A java interface is a formal declaration of such a

    contract in which all methods contain no

    implementation

    Many unrelated classes can implement the sameinterface

    A class can implement many, unrelated interfaces

  • 8/14/2019 BasicJava4

    10/24

    Uses of Interfaces

    Declaring methods that one or more classes are

    expected to implement

    Determining an objects programming interface

    without revealing the actual body of the class

    Capturing similarities between unrelated classes

    without forcing a class relationship Simulating multiple inheritance by declaring a class

    that implements several interfaces

  • 8/14/2019 BasicJava4

    11/24

    Inner classes

    Allow a class defintion to be placed inside

    another class definition Group classes that logically belong together

    Have access to their enclosing classs scope

  • 8/14/2019 BasicJava4

    12/24

    Properties of Inner classes

    You can use the class name only within thedefined scope except when used in aqualified name. The name of the inner classmust differ from the enclosing class

    The inner class can be defined inside a

    method. Only local variables marked as final,can be accessed by methods within an innerclass

  • 8/14/2019 BasicJava4

    13/24

    Properties of Inner Classes

    The inner class can use both class and

    instance variables of enclosing classes andlocal variables of enclosing blocks

    The inner class can be defined as abstract

    The inner class can have any access mode The inner class can act as an interface

    implemented by another inner class

  • 8/14/2019 BasicJava4

    14/24

    Properties of Inner Classes

    Inner classes that are declared static

    automatically become top-level classes Inner classes cannot declare any static

    members

    An inner class wanting to use a staticmember must be declared static

  • 8/14/2019 BasicJava4

    15/24

    Exceptions

  • 8/14/2019 BasicJava4

    16/24

    Objectives

    Define Exceptions

    Use try, catch, and finally statements

    Describe exception categories

    Identify common exceptions

    Develop programs to handle your ownexceptions

  • 8/14/2019 BasicJava4

    17/24

    Exceptions

    The Exception class defines error conditions thatyour program encounters

    Exception can occur when The file you try to open does not exist The network connection is disrupted Operands being manipulated are out of prescribed ranges The class file you are interested in loading is missing

    An error class defines serious (fatal) errorconditions

  • 8/14/2019 BasicJava4

    18/24

    try and catch statements

    try{

    //code that might throw a particular exception

    }catch(MyException myExcept){

    //code to execute if a MyExceptiontype is thrown

    }

    catch(Exception otherExcept){

    //Code to execute if a general Exception is

    //thrown

    }

  • 8/14/2019 BasicJava4

    19/24

    Call Stack Mechanism

    If an exception is not handled in the current

    try-catch block, it is thrown to the caller of

    that method

    If the exception gets back to the main

    method and is not handled there, the program

    is terminated abnormally

  • 8/14/2019 BasicJava4

    20/24

    finally statement

    try{

    startFaucet();

    waterLawn();

    }catch(BrokenPipeException e){

    logProblem(e);

    }finally{stopFaucet();

    }

  • 8/14/2019 BasicJava4

    21/24

    Exception Categories

    S t a c k O v e r f l o w E r r o r O u t O f M e m o r y E r r o r

    V i r t u a l M a c h i n e E r r o r A W T E r r o r

    E r r o r

    A r i t h m e t i c E x c e p t i o n N u l l P o i n t e r E x c e p t i o n I n d e x O u t O f B o u n d s E x c e p t i o n

    R u n t i m e E x c e p t io n

    E O F E x c e p t i o n F i l e N o t F o u n d E x c e p t i o n

    IO E x c e p t i o n

    E x c e p t io n

    T h r o w a b le

  • 8/14/2019 BasicJava4

    22/24

    Common Exceptions

    ArithmeticException

    NullPointerExceptionNegativeArraySizeException

    ArrayIndexOutOfBoundsException

    SecurityException

  • 8/14/2019 BasicJava4

    23/24

    The Handle or Declare Rule

    Handle the Exception by using the try-catch-finally

    block

    Declare that the code causes an exception by using

    the throws clause

    A method may declare that it throws more than one

    exception You do not need to handle or declare run-time

    exceptions or errors

  • 8/14/2019 BasicJava4

    24/24

    Method Overriding and Exceptions

    Must throw exceptions that are the same class as the

    exceptions being thrown by the overridden method

    May throw exceptions that are subclasses of the

    exceptions being thrown by the overridden method

    If a superclass method throws multiple exceptions,

    the overriding method must throw a proper subsetof exceptions thrown by the overridden method