0% found this document useful (0 votes)
19 views

Java Notes6 PDF

The document discusses packages and interfaces in Java. Packages are used to organize related classes and avoid naming collisions. Classes within a package must be accessed through the package name. Interfaces define a contract that other classes can implement, containing only abstract method signatures. Abstract classes can contain both abstract and non-abstract methods, where subclasses must implement any abstract methods.

Uploaded by

Omkar Khedekar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Java Notes6 PDF

The document discusses packages and interfaces in Java. Packages are used to organize related classes and avoid naming collisions. Classes within a package must be accessed through the package name. Interfaces define a contract that other classes can implement, containing only abstract method signatures. Abstract classes can contain both abstract and non-abstract methods, where subclasses must implement any abstract methods.

Uploaded by

Omkar Khedekar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Unit 6

Packages & Interfaces

Prepared By – Asmita Ranade


Packages
Packages in Java provide a mechanism by which related pieces of a program can
be organized as a unit. Package provides a way to name a collection of classes.
Package is a way to partition the namespace.
Classes defined within a package must be accessed through their package name.
When a class is defined within a package, the name of that package is attached to
each class, thus avoiding name collisions with other classes that have the same
name, but are in other packages.
All classes in Java belong to some package. When no package statement is
specified, then the default global package is used. If we omit the package
statement, the classes are put into the default package. Prepared By – Asmita Ranade
Packages
To create a package, include a package command as the first statement in the Java
source file. Any classes declared within that file will belong to the specified
package. The package statement defines a namespace in which the classes are
stored.
Syntax – package pkg;
Here, pkg is the name of the package.
E.g. package NewPackage;
Java uses the file system to manage packages, with each package stored in its own
directory. All the .class files which are required to be a part of the defined package
must be stored in that specific directory. Prepared By – Asmita Ranade
Package Example
package BookPack; class BookDemo
class Book { public static void main(String args[])
{ private String title; { Book bk[]=new Book[3];
private String author; bk[0]= new Book("Angels and Demons", "Dan Brown", 345);
private int price; bk[1]= new Book("Thousand Splendid Suns", "Khaleid Hosseini",300);
Book(String t, String a, int p) //constructor bk[2]= new Book("The Book Thief", "Markus Zusak",400);
{ title = t; for(int i=0;i<bk.length;i++)
author = a; bk[i].display();
price = p; }
} }
void display()
{ System.out.println(“Title = “ + title);
System.out.println(“Author Name = “ + author);
System.out.println(“Price = Rs. “ + price);
System.out.println();
}
}
Call this file as
BookDemo.java and
save it in BookPack
directory.
Prepared By – Asmita Ranade
Importing Packages
When you use a class from another package, you can fully qualify the name of the
class with the name of its package. However, such an approach could easily become
tedious if the classes are deeply nested in a package hierarchy.
‘Import’ statement can be used to use the contents of a package. ‘Import’ statement
allows you to use the members directly without using explicit package qualification.
Syntax – import packagename.classname;
If we want to import the entire contents of a package use an asterisk * for the
classname.
E.g. import BookPack.Book;
import BookPack.*;
Prepared By – Asmita Ranade
Importing Package Example
Account.java – save it in BankAccount folder UseAccount.java – Do not save this in BankAccount folder
package BankAccount; import BankAccount.*;
public class Account class UseAccount
{ int acno; { public static void main(String args[])
String type; { Account ac1 = new Account();
long balance; Account ac2 = new Account(100,"Current",25000);
public Account() ac1.display();
{ acno=1; System.out.println();
type="Savings"; ac2.display();
balance=50000; }
} }
public Account(int a, String t, int b)
{ acno=a;
type=t;
balance=b;
}
public void display()
{ System.out.println("Account No : " + acno);
System.out.println("Account Type : " + type);
System.out.println("Balance : Rs. " +balance);
}
} Prepared By – Asmita Ranade
Importing Packages
Java defines a large number of standard classes that are available to all programs. This
class library is referred to as Java API. Java API is stored in packages. At the top of the
package hierarchy is ‘java’. Descending from ‘java’ are several subpackages like -

Subpackage Description
java.lang Contains a large number of general-purpose classes
java.io Contains I/O classes
java.net Contains classes that support networking

java.lang contains various classes like System. java.lang package is unique because it
is imported automatically into every Java program. So, we need not explicitly import
java.lang package.
Prepared By – Asmita Ranade
Abstraction
In OOP, Abstraction is defined as hiding the unnecessary details from the user and
to focus on essential details. In Java, Abstraction can be achieved using
‘Abstract’ classes and methods.
A class in Java which is declared with ‘abstract’ keyword is an Abstract class. It
may have both abstract and non-abstract methods. We need to extend the abstract
class and implement its methods. It cannot be instantiated.
Syntax – abstract class classname
{ //abstract or non-abstract methods
}
Prepared By – Asmita Ranade
Abstract Methods
A method declared using the ‘abstract’ keyword within an abstract class and does
not have a definition is called an abstract method.
Abstract method does not have a method body. It may have zero or more
arguments.
Syntax – abstract returntype methodname ( [args list] );
In superclass, an abstract method can be declared which is then implemented in
subclass.
If a non-abstract class extends an abstract class, then the class must implement all
the abstract methods of that abstract class. If not, then the class should be declared
as abstract as well. Prepared By – Asmita Ranade
Abstract Class and Method Example
abstract class SumNumbers
{ public abstract int SumTwo(int n1, int n2); Abstract Methods
public abstract int SumThree(int n1, int n2, int n3);
public void display() Non-abstract method
{ System.out.println("Method of abstract class SumNumbers");
}
}
class DemoAbstract extends SumNumbers
{ public int SumTwo(int num1, int num2)
{ return (num1+num2);
}
public int SumThree(int num1, int num2, int num3)
{ return (num1+num2+num3);
}
Imp Statement
public static void main(String args[])
{ SumNumbers s = new DemoAbstract();
System.out.println("Sum of 2 numbers 10 and 56 = " + s.SumTwo(10,56));
System.out.println("Sum of 3 numbers 11, 45 and 63 = " + s.SumThree(11,45,63));
s.display();
} Prepared By – Asmita Ranade
}
Abstract Class and Method Example
abstract class Shape Abstract Class & class DemoShape
{ abstract void area(); Method { public static void main(String args[])
} {
class Rectangle extends Shape Shape sh= new Rectangle(12,15); Imp Statements
{ int l,b; sh.area();
Rectangle(int len, int brd) sh = new Square(5);
{l=len; sh.area();
b=brd; }
} }
public void area()
{ System.out.println("Rectangle Area = " + (l*b));
}
}
class Square extends Shape
{ int s;
Square(int side)
{ s=side;
}
public void area()
{ System.out.println("Square Area = " + (s*s));
}
} Prepared By – Asmita Ranade
Interfaces
An interface is a collection of abstract methods. It is syntactically similar to
classes but it lacks instance variables.
Once an interface is defined, any number of classes can implement an interface.
Also, one class can implement any number of interfaces.
To implement an interface, a class must create the complete set of methods defined
by the interface.
In an interface,
Syntax – access interface interfacename methods are implicitly
public. Variables
{ returntype method1(parameter list); declared in an interface
returntype method2 (parameter list); are implicitly public,
final, static and must be
type variable1 = value; initialised.
}
Prepared By – Asmita Ranade
Implementing Interfaces
To implement an interface, include the ‘implements’ keyword in class definition.
Syntax – class classname implements interfacename
{ class body
}

Abstract Class Vs. Interfaces


Abstract Class Interface
1. Can only have abstract and default
1. Can have non-abstract methods.
methods.
2. A class can implement more than one
2. A class can extend only one abstract class.
interface.
3. Can have final, non-final, static and non-
3. Can only have static and final fields.
static variables.
4. Declared using ‘abstract’ keyword. 4. Declared using ‘interface’ keyword.
Prepared By – Asmita Ranade
Interface Example
FileName - Series.java FileName - DemoSeries.java
public interface Series class DemoSeries
{ int getNext(); { public static void main(String args[])
void reset(int x); {DemoByTwo ob = new DemoByTwo();
} for(int i=0;i<5;i++)
System.out.println("Next value = " + ob.getNext());
FileName - DemoByTwo.java System.out.println();
class DemoByTwo implements Series System.out.println("Resetting the series at 100 -");
{ int start; ob.reset(100);
DemoByTwo() for(int i=0;i<5;i++)
{ start=0; System.out.println("Next value = " + ob.getNext());
} }}
public int getNext()
{ start+=2;
return start;
}
public void reset(int x)
{ start=x;
}
} Prepared By – Asmita Ranade

You might also like