Presentation Java Basics 1530977673 47076
Presentation Java Basics 1530977673 47076
Pari Ramalingam
– 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.
Animal is the superclass of Reptile class. Reptile IS-A Animal
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
classes
WAY TO IT 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 TO
method that IT
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.
If you want a class to contain a particular method but you want the actual
implementation of that method to be determined by child classes, you can declare
the method in the parent class as abstract.
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
•A method can declare that it throws more than one exception, in which
case the exceptions are declared in a list separated by commas
InsufficientFundsException
WAY TO IT COMPUTER EDUCATION
DON’T JUST LEARN, BECOME AN EXPERT AND LEAPFROG IN LIFE
finally keyword
•The finally keyword is used to create a block of code that follows a try block. A
finally block of code always executes, whether or not an exception has occurred.
•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.
•Step 6: In the main class, instantiate the above class and invoke
the default constructor
By Extending the
By Implementing
Thread Class
Runnable Interface
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
• Synchronized Methods
• Synchronized Statements
•If an object is visible to more than one thread, all reads or writes to
that object's variables are done through synchronized methods
• Synchronizing constructors doesn't make sense, because only the thread that creates
an object should have access to it while it is being constructed