Bca Vi Java Unit03
Bca Vi Java Unit03
Compiled
(Strictly from Academic Purpose only)
(by)
Compiled by: Dr. Sandeep Maan, Assoc Prof in Computer Sc, GCG Sec-14, Gurugram.
Reference: The Complete Reference, Seventh Edition, Herbert Schildt, THM 2007.
Note: For academic purpose only. For detailed explanation of contents please consult the above referred book
Page. 1
MDU Syllabus
(BCA-307 : Object Technologies & Programming using Java)
UNIT – III
Packages: Defining Package, CLASSPATH, Package naming, Accessibility of Packages , using
Package Members.
Interfaces: Implementing Interfaces, Interface and Abstract Classes, Extends and Implements
together.
PACKAGES IN JAVA
We know each class must be given a unique name to avoid name collision. But this
may lead to the problem of exhausting of suitable name that describes the purpose
of the class. So, we need a method to choose a class name that is reasonably unique
and do not collide with the names chosen by OTHER PROGRAMMERS. This is
done using a mechanism called package where a chunk of classes is managed as a
group and are exposed only to the members of that group and not from outside. So,
if any other programmer now chooses same name for his class that is not part of
this package no issue of name collusion would ever arise. So, the programmer just
need to have knowledge of members of this chunk called as package. It is just like
a director in windows. In order to create a package first we create a windows
directory (assuming we are using windows operating system) by the name of
package.
Defining a package
It defined the name space in which the class is stored and is must be the first line
of the program. It the package statement is not included in the program then it class
name is put into the default package.
Compiled by: Dr. Sandeep Maan, Assoc Prof in Computer Sc, GCG Sec-14, Gurugram.
Reference: The Complete Reference, Seventh Edition, Herbert Schildt, THM 2007.
Note: For academic purpose only. For detailed explanation of contents please consult the above referred book
Page. 2
Syntax:
package my;
Suppose the class file is stored in the subdirectory (subdir) in directory (mydir) of
windows then we shall use
package mydr.subdir.my;
Now important question become how java run time system know where to look for
the package. There are three scenarios
Compiled by: Dr. Sandeep Maan, Assoc Prof in Computer Sc, GCG Sec-14, Gurugram.
Reference: The Complete Reference, Seventh Edition, Herbert Schildt, THM 2007.
Note: For academic purpose only. For detailed explanation of contents please consult the above referred book
Page. 3
Accessibility of package
Compiled by: Dr. Sandeep Maan, Assoc Prof in Computer Sc, GCG Sec-14, Gurugram.
Reference: The Complete Reference, Seventh Edition, Herbert Schildt, THM 2007.
Note: For academic purpose only. For detailed explanation of contents please consult the above referred book
Page. 4
e.g.
int sum;
sum =a + b ;
System.out.println(sum);
import My.*;
Class Try {
public static void main(String s[]) {
Sum s(1,2);
s.show();
}
Compiled by: Dr. Sandeep Maan, Assoc Prof in Computer Sc, GCG Sec-14, Gurugram.
Reference: The Complete Reference, Seventh Edition, Herbert Schildt, THM 2007.
Note: For academic purpose only. For detailed explanation of contents please consult the above referred book
Page. 5
Interface
They are fully abstract class(es) in JAVA (means their object cannot be created).
They are different from a Class (type) as
Example
interface Area {
float pi=3.14;
Step 02: Implement the interface: Now we define a class that would implement the
above interface.
System.out.println(“Area=” + pi*rad*rad);
}
Compiled by: Dr. Sandeep Maan, Assoc Prof in Computer Sc, GCG Sec-14, Gurugram.
Reference: The Complete Reference, Seventh Edition, Herbert Schildt, THM 2007.
Note: For academic purpose only. For detailed explanation of contents please consult the above referred book
Page. 6
}
We can implement the interface by creating object of its implementing class e.g.
class Test {
a.area(4);
Important Concepts:
1. Abstract Class: An abstract class is one whose object cannot created and
that can only be extended.
2. Polymorphism: Now let us define another class say Try that implements
the same interface.
System.out.println(“Area=” + rad);
//(AND)
class Test {
Compiled by: Dr. Sandeep Maan, Assoc Prof in Computer Sc, GCG Sec-14, Gurugram.
Reference: The Complete Reference, Seventh Edition, Herbert Schildt, THM 2007.
Note: For academic purpose only. For detailed explanation of contents please consult the above referred book
Page. 7
Try t=new Try();
t.area(4);
Here the output will be 4. Here the area method defined in class Test will be called
which is decided at run time.
interface A {
int a=2;
interface B implements A {
And so on.
So we can say in JAVA a derived class can extend only one base class bur it can
simultaneously implement one or more interface e.g.
When we write a program there may be two type of errors viz. logical and physical.
Logical errors are those which are not caught by the compiler and program may
produce unexpected result(s) whereas the physical errors like syntax errors are
caught during the compile time.
etc.
So we can summarize
Error: An Error indicates serious problem that a reasonable application should not
try to catch.
Compiled by: Dr. Sandeep Maan, Assoc Prof in Computer Sc, GCG Sec-14, Gurugram.
Reference: The Complete Reference, Seventh Edition, Herbert Schildt, THM 2007.
Note: For academic purpose only. For detailed explanation of contents please consult the above referred book
Page. 9
Exceptions in JAVA
1. Checked Exception: Exception those are checked at the compile time itself
and notified thereof like
public class My
{
public static void main (String s[])
{
File file=new File(“c:/tmp.txt”);
FileReader fr=new FileReader(file);
}
In this if file “tmp.txt” is not found compiler will notify and program won’t
be compiled itself.
Compiled by: Dr. Sandeep Maan, Assoc Prof in Computer Sc, GCG Sec-14, Gurugram.
Reference: The Complete Reference, Seventh Edition, Herbert Schildt, THM 2007.
Note: For academic purpose only. For detailed explanation of contents please consult the above referred book
Page. 10
Exception Handling
e.g.
public class My {
try {
int x=5;
int y = 0;
System.out.println(x/y);
catch (ArithmeticException e) {
System.out.println(“Divide by zero”);
Compiled by: Dr. Sandeep Maan, Assoc Prof in Computer Sc, GCG Sec-14, Gurugram.
Reference: The Complete Reference, Seventh Edition, Herbert Schildt, THM 2007.
Note: For academic purpose only. For detailed explanation of contents please consult the above referred book
Page. 11
}
We can have multiple catch block that will tackle different type of exceptions
like ArtihmeticException, IOException and so on. Here which catch block will
be executed depends upon the type of exception occurred.
3. Nested try Statement: We may have a try block that is nested inside the
other try block. In such case of an exception occurs for which no catch block
is specified inside inner try block then the catch block of outer try block
would be searched to handle the exception.
4. throw: We may write statement to throw an exception then the statement
after the throw statement in the try block are not executed and control will
be transferred to respective catch block.
e.g.
class My {
try {
………….
……….
Compiled by: Dr. Sandeep Maan, Assoc Prof in Computer Sc, GCG Sec-14, Gurugram.
Reference: The Complete Reference, Seventh Edition, Herbert Schildt, THM 2007.
Note: For academic purpose only. For detailed explanation of contents please consult the above referred book
Page. 12
{
…………………………
e.g.
public class My {
…….
try {
It will cause error because neither the catch block is provided to handle the
exception nor throws has been implemented. So correct implementation would be
public class My {
Compiled by: Dr. Sandeep Maan, Assoc Prof in Computer Sc, GCG Sec-14, Gurugram.
Reference: The Complete Reference, Seventh Edition, Herbert Schildt, THM 2007.
Note: For academic purpose only. For detailed explanation of contents please consult the above referred book
Page. 13
…….
try {
It is a very useful block which can be utilized to implement critical functions like
closing file handle, freeing up space. E.g.
public class My {
try {
……………
} finally {
System.out.println(“Inside finally”);
Compiled by: Dr. Sandeep Maan, Assoc Prof in Computer Sc, GCG Sec-14, Gurugram.
Reference: The Complete Reference, Seventh Edition, Herbert Schildt, THM 2007.
Note: For academic purpose only. For detailed explanation of contents please consult the above referred book
Page. 14
}
1. Exception ()
2. Exception (String msg)
e.g. let us write a program with custom exception to ensue that a variable is not
assigned any value except ‘a’
MyException(String a){
return (a);
class Test {
Compiled by: Dr. Sandeep Maan, Assoc Prof in Computer Sc, GCG Sec-14, Gurugram.
Reference: The Complete Reference, Seventh Edition, Herbert Schildt, THM 2007.
Note: For academic purpose only. For detailed explanation of contents please consult the above referred book
Page. 15
}
t.test(‘b’);
Compiled by: Dr. Sandeep Maan, Assoc Prof in Computer Sc, GCG Sec-14, Gurugram.
Reference: The Complete Reference, Seventh Edition, Herbert Schildt, THM 2007.
Note: For academic purpose only. For detailed explanation of contents please consult the above referred book
Page. 16
Bibliography & Other References
1. Programming in Java, E Balagurusamy.
2. The Complete Reference JAVA, TMH Publication.
3. Beginning JAVA, Ivor Horton, WROX Public.
4. JAVA 2 UNLEASHED, Tech Media Publications.
5. Patrick Naughton and Herbertz Schildt, “Java-2 The Complete Reference”,
1999, TMH.
6. Java Programming by Dr. Ramesh et al., Unique Publications.
Compiled by: Dr. Sandeep Maan, Assoc Prof in Computer Sc, GCG Sec-14, Gurugram.
Reference: The Complete Reference, Seventh Edition, Herbert Schildt, THM 2007.
Note: For academic purpose only. For detailed explanation of contents please consult the above referred book
Page. 17