Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
85 views

Basic Java 4

The document discusses advanced class features in Java including static variables, methods, and initializers; final classes, methods, and variables; abstract classes and methods; and inner classes. It provides examples of using the static and final keywords and defines abstract classes and interfaces. It also covers properties of inner classes and exceptions handling using try, catch, and finally statements.

Uploaded by

kishored
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
85 views

Basic Java 4

The document discusses advanced class features in Java including static variables, methods, and initializers; final classes, methods, and variables; abstract classes and methods; and inner classes. It provides examples of using the static and final keywords and defines abstract classes and interfaces. It also covers properties of inner classes and exceptions handling using try, catch, and finally statements.

Uploaded by

kishored
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 24

Advanced Class Features

Objectives

 Describe static variables, methods and


initializers
 Describe final classes, methods and variables
 Abstract classes and methods
 Inner classes
 Static and non-static inner classes
 interface
The static Keyword

 The static keyword is used as a modifier on


variables and inner classes
 The static keyword declares the attribute or
method is associated with the class as a
whole rather than any particular instance of
that class
 Thus static members are often called as
“class members”
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;
}
}
The static Keyword

 Static method cannot use “this”


 They cannot access any non-static fields
 A static method cannot be overridden
Static Initializers

 A class can contain code in a static block that does


not exist within a method body
 Static code block executes only once when the class
is loaded
 A static block is used to initialize static attributes
public class Count{
public static int counter;
static{
counter=Integer.getInteger(“myCounter”).intValue();
}
}
//use java –DmyCounter=45 Test to execute
The final Keyword
 You cannot subclass a final class
 You cannot override a final method
 A final variable is a constant
 A final variable can only be set once, but that
assignment can occur independently of the
declaration;
 A blank final instance attribute must be set in every
constructor
 A blank final method variable must be set in the method
body before being used
Abstract Classes

 Any class with one or more abstract methods


is called an abstract class
 Abstract class can’t be instantiated
 Abstract classes may have data attributes,
concrete methods and constructors
 It is a good practice to make the constructors
protected
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 same
interface
 A class can implement many, unrelated interfaces
Uses of Interfaces

 Declaring methods that one or more classes are


expected to implement
 Determining an object’s 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
Inner classes

 Allow a class defintion to be placed inside


another class definition
 Group classes that logically belong together
 Have access to their enclosing class’s scope
Properties of Inner classes

 You can use the class name only within the


defined scope except when used in a
qualified name. The name of the inner class
must 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 inner
class
Properties of Inner Classes

 The inner class can use both class and


instance variables of enclosing classes and
local 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
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 static
member must be declared static
Exceptions
Objectives

 Define Exceptions
 Use try, catch, and finally statements
 Describe exception categories
 Identify common exceptions
 Develop programs to handle your own
exceptions
Exceptions

 The Exception class defines error conditions that


your 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) error
conditions
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
}
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
finally statement

try{
startFaucet();
waterLawn();
}catch(BrokenPipeException e){
logProblem(e);
}finally{
stopFaucet();
}
Exception Categories

T h ro w a b le

E rro r E x c e p tio n

V irtu a lM a c h in e E rro r A W T E rro r R u n tim e E x c e p tio n IO E x c e p tio n

S ta c k O v e rflo w E rro r O u tO fM e m o ry E rro r A rith m e tic E x c e p tio n N u llP o in te rE x c e p tio n In d e x O u tO fB o u n d s E x c e p tio n E O F E x c e p tio n F ile N o tF o u n d E x c e p tio n
Common Exceptions

 ArithmeticException
 NullPointerException
 NegativeArraySizeException
 ArrayIndexOutOfBoundsException
 SecurityException
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
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 subset
of exceptions thrown by the overridden method

You might also like