Professional Documents
Culture Documents
• Helps
WAY TO toIT create modular programs and
COMPUTER reusable
EDUCATION
DON’T JUST LEARN, BECOME AN EXPERT AND LEAPFROG IN LIFE
Powerful Features
– Operator overloading,
– Multiple inheritance through classes, pointers
and
– Explicit memory allocation.
JAVA COMPILER
(translator
)
JAVA INTERPRETER
(one for each different system)
• Windows:
– Set CLASSPATH=%CLASSPATH%;%JAVA_HOME%\lib\tools.jar;
• UNIX:
– Set CLASSPATH=$CLASSPATH:$JAVA_HOME/lib/tools.jar;
• Windows:
– Set PATH=%PATH%; %JAVA_HOME%\bin
• UNIX:
– Set PATH=$PATH: $JAVA_HOME/bin
var1 100
s
Hello There
IS-A Relationship:
IS-A is a way of saying : This object is a type of that object. Let us see how
the extends keyword is used to achieve inheritance.
With use of the extends keyword the subclasses will be able to inherit all the
properties of the superclass except for the private properties of the superclass.
Mammal IS-A Animal
Animal is the superclass of Mammal class.
Reptile IS-A Animal
Animal is the superclass of Reptile class.
Mammal and Reptile are sub classes of Animal Dog IS-A Mammal
class. Hence : Dog IS-A Animal as well
Dog is the subclass of both Mammal and Animal
WAY TO IT
classes COMPUTER EDUCATION
DON’T JUST LEARN, BECOME AN EXPERT AND LEAPFROG IN LIFE
Overriding the Parent Class Method
If a class inherits a method from its super class, then there is a chance
to override the method provided that it is not marked final.
During compilation only the syntax is checked and hence no error will
be reported.
During execution, JVM figures out the object type and would run the
WAY TOthatIT
method belongs to that particular object COMPUTER EDUCATION
DON’T JUST LEARN, BECOME AN EXPERT AND LEAPFROG IN LIFE
Rules for Overriding the Parent Class Method
•The argument list should be exactly the same as that of the overridden method.
•The return type should be the same or a subtype of the return type declared in the original
overridden method in the super class.
•The access level cannot be more restrictive than the overridden method's access level. For
example: if the super class method is declared public then the overridding method in the sub
class cannot be either private or public. However the access level can be less restrictive than
the overridden method's access level.
•Instance methods can be overridden only if they are inherited by the subclass.
•A method declared final cannot be overridden.
•A method declared static cannot be overridden but can be re-declared.
•If a method cannot be inherited then it cannot be overridden.
•A subclass within the same package as the instance's superclass can override any superclass
method that is not declared private or final.
•A subclass in a different package can only override the non-final methods declared public or
protected.
•An overriding method can throw any uncheck exceptions, regardless of whether the
overridden method throws exceptions or not. However the overriding method should not throw
checked exceptions that are new or broader than the ones declared by the overridden method.
The overriding method can throw narrower or fewer exceptions than the overridden method.
•Constructors cannot be overridden.
Any child class must either override the abstract method or declare itself abstract.
A child class that inherits an abstract method must override it. If they do not, they
must be abstract,and any of their children must override it.
Unless the class that implements the interface is abstract, all the methods of
the interface need to be defined in the class
If a class does not perform all the behaviors of the interface, the
class must declare itself as abstract.
c = a/b;
•An exception can occur for many different reasons, including the
following:
– A user has entered invalid data.
if (b == 0)
System.out.println(“Error”);
else
c = a/b;
•What should the class List do when a wrong index is passed to the
method get(int index)?
– The class List cannot do this as it is a generic class for storing any
kind of object
•One part of the system can detect the exception but does not know
how to handle it
•Another part of the system cannot detect the exception but knows
how to handle it
– These are not exceptions at all, but problems that arise beyond the
control of the user or the programmer.
– Errors are typically ignored in your code because you can rarely do
anything about an error. For example, if a stack overflow occurs, an
error will arise. They are also ignored at the time of compilation
Throwable
Error Exception
IOException RuntimeException
ArrayIndexOutOfBoundsExcept ArithmeticExceptio
ion n
NullPointerException
– When a Java program tries to divide by zero, the runtime will create
and throw an object of ArithmeticException
•The search begins with the method in which the error occurred and
proceeds through the call stack in the reverse order in which the
methods were called
•If the runtime system exhaustively searches all the methods on the
call stack without finding an appropriate exception handler, as
shown in the next figure, the runtime system (and, consequently, the
program) terminates
•If the type of exception that occurred is listed in a catch block, the
exception is passed to the catch block much as an argument is
passed into a method parameter
WAY TO IT COMPUTER EDUCATION
DON’T JUST LEARN, BECOME AN EXPERT AND LEAPFROG IN LIFE
Catching the Exception
A try block can be followed by more
try
than one catch blocks
{
//Protected code
try
} catch(ExceptionName e1)
{
{ //Protected code
//Catch block }catch(ExceptionType1 e1)
} {
//Catch block
try{ }catch(ExceptionType2 e2)
{
c = a/b;
//Catch block
} }catch(ExceptionType3 e3)
catch(ArithmeticException e){ {
System.out.println(“Divide By //Catch block
Zero”); }
WAY } TO IT COMPUTER EDUCATION
DON’T JUST LEARN, BECOME AN EXPERT AND LEAPFROG IN LIFE
throws/throw keyword
•If a method does not handle a checked exception, the method must
declare it using the throws keyword
•A method can declare that it throws more than one exception, in which
case the exceptions are declared in a list separated by commas
•Using a finally block allows you to run any cleanup-type statements that
you want to execute, no matter what happens in the protected code.
•You just need to extend the Exception class to create your own
Exception class. These are considered to be checked
exceptions.
₋ Must also call start( ) using the instance of the sub class, to begin
WAY TO IT execution of the new thread COMPUTER EDUCATION
DON’T JUST LEARN, BECOME AN EXPERT AND LEAPFROG IN LIFE
Creating a Thread
•Step 1: Define a class to implement the Runnable Interface
•Step 6: In the main class, instantiate the above class and invoke
the default constructor
NEWBORN
DEAD
start()
run() returns or
thread interrupted
notify(), notifyAll()
WAITING RUNNABLE
Sleep interval
expires
yield() or end of scheduled
time slice
SLEEPING sleep() wait()
NON-RUNNABLE
ALIVE
•This means that the two operations consist of multiple steps, and the sequences
of steps overlap
WAY TO 6.IT
Thread B: Store result in c; c is now -1. COMPUTER EDUCATION
DON’T JUST LEARN, BECOME AN EXPERT AND LEAPFROG IN LIFE
Synchronization
•When two or more threads need access to a shared resource,
they need some way to ensure that the resource will be used by
only one thread at a time
• Synchronized Methods
• Synchronized Statements
WAY TO IT COMPUTER EDUCATION
DON’T JUST LEARN, BECOME AN EXPERT AND LEAPFROG IN LIFE
Synchronized Methods
•If an object is visible to more than one thread, all reads or writes to
that object's variables are done through synchronized methods