2. • Package in Java is a mechanism to encapsulate a group of classes, sub
packages and interfaces.
• Packages are used for:
• Preventing naming conflicts. For example there can be two classes
with same name in two packages,
• Makes searching/locating and usage of classes, interfaces,
enumerations and annotations easier
• Providing controlled access: protected and default have package level
access control. Aprotected member is accessible by classes in the
same package and its subclasses. Adefault member (without any
access specifier) is accessible by classes in the same package only.
• Packages that are inside another package are the subpackages. These
are not imported by default, they have to imported explicitly.
3.
4.
5. User-defined packages
These are the packages that are defined by the user. First we create a
directory mypack . Then create the class Simple inside the directory with the first
statement being the package names.
//save as Simple.java
package mypack;
class SimplePackage{
public void display(){
System.out.println("Welcome to package");
}
}
6. import mypack.SimplePackage;
public class PackageUse
{
public static void main(String args[])
{
SimplePackage p=new SimplePackage();
p.display();
}
}
To compile java package:
javac -d . Simple.java / / -d specifies the destination where to putthe
generated class file, to keep the package within the same directory use . (dot).
To Compile: javac -d . Simple.java
To Run: java mypack.Simple
7.
8. • It is a collection of abstract methods. Aclass implements an interface, thereby
inheriting the abstract methods of the interface.
• Interface cannot be instantiated.
• An interface does not contain any constructors.
• All of the methods in an interface are abstract.
• An interface is not extended by a class; it is implemented by a class.
• An interface can extend multiple interfaces.
• Interface methods do not have a body - the body is provided by the
"implement" class
• On implementation of an interface, you must override all of its methods
• Interface methods are by default abstract and public
• Interface attributes are by default public, static and final
• To achieve security - hide certain details and only show the important details
of an object
• "multiple inheritance" can be achieved with interfaces, because the class
can implement multiple interfaces.
INTERFACES IN JA
V
A
9. by default.
interface <interface_name>{
/ / declare constant fields
/ / declare methods thatabstract
}
The Java compiler adds public and abstract keywords before the interface
method. Moreover, it adds public, static and final keywords before data
members.
10. a class extends another class, an interface extends another interface, but
a class implements an interface.
Aclass can extend only one class, but implement many interfaces..
11. interface In1
{
/ / public, static andfinal final int a =
10;
/ / public and abstract void
display();
}
class InterfaceTestClass implements In1
{
/ / Implementing the capabilities of interface.
public void display()
{
System.out.println(“Kristu Jayanti College");
}
public static void main (String[] args)
{
InterfaceTestClass t = new InterfaceTestClass(); t.display();
System.out.println(a);
}
12. • Aclass can implement more than one interface at a time.
• Aclass can extend only one class, but implement many interfaces.
• An interface can extend another interface, in a similar wayas a class can
extend another class.
• If a class implements multiple interfaces, or an interface extends multiple
interfaces, it is known as multiple inheritance.
13. interface FirstInterface {
public void myMethod(); / / interface method
}
interface SecondInterface {
public void myOtherMethod(); / / interface method
}
class DemoClass implements FirstInterface, SecondInterface {
public void myMethod() {
System.out.println("Some text from irst interface..");
}
public void myOtherMethod() { System.out.println("Some
text from second interface...");
}
}
class MulInterfaceTest1 {
public static void main(String[] args) {
DemoClass myObj = new DemoClass();
myObj.myMethod();
myObj.myOtherMethod();
}
14. • Multiple inheritance is not supported through class in java, but it is possible by an
interface, why?
• multiple inheritance is not supported in the case of class because of ambiguity. However,
it is supported in case of an interface because there is no ambiguity. It is because its
implementation is provided by the implementation class.
interface Printable{
void print();
}
interface Showable{
void print();
}
class TestInterface3 implementss Printable, Showable{
public void print(){System.out.println("Hello");}
public static void main(String args[]){
TestInterface3 obj = new TestInterface3();
obj.print();
} }
15. interface Printable{
void print();
}
interface Showable extends Printable{
void show();
}
class InterfaceInheritance implements Showable{
public void print(){System.out.println("Hello");}
public void show(){System.out.println("Welcome");}
public static void main(String args[]){
InterfaceInheritance obj = new InterfaceInheritance();
obj.print();
obj.show();
}
}
INTERFACE INHERITANCE
16. Provides classes that are fundamental to the design of the Java programming
language.
Object, the ultimate superclass of all classes in Java
• Thread, the class that controls each thread in a multithreaded program
• Throwable, the superclass of all error and exception classes in Java
• Classes that encapsulate the primitive data types in Java
• Classes for accessing system resources and other low-level entities
• Math, a class that provides standard mathematical methods
• String, the class that is used to represent strings
Java.lang Package In Java
17. • Wrapper classes are those whose objects wraps a primitive data type within
them.
• In the java.lang package java provides a separate class for each of the
primitive data types namely Byte, Character, Double, Integer, Float, Long,
Short.
• At the time of instantiation, these classes accept a primitive datatype
directly, and wrap a primitive value into a wrapper class object.
• They convert primitive data types into objects.
18.
19. • It contains the utility classes (a string tokenizer, a random-number generator, and a bit
array).
• AbstractCollection: This class provides a skeletal implementation of the Collection
interface, to minimize the effort required to implement this interface.
• Arrays: This class contains various methods for manipulating arrays (such as sorting and
searching).
• Calendar: The Calendar class is an abstract class that provides methods for converting
between a specific instant in time and a set of calendar fields such as YEAR, MONTH,
DAY_OF_MONTH, HOUR,
• Currency: Represents a currency.
• Date: The class Date represents a specific instant in time, with millisecond precision.
• Random: An instance of this class is used to generate a stream of pseudorandom
numbers.
• Scanner: Asimple text scanner which can parse primitive types and strings using regular
expressions.
• String Tokenizer: The string tokenizer class allows an application to break a string into
tokens.
• Treeset: ANavigableSet implementation based on a TreeMap.
java.util PACKAGE
20. • This package provides for system input and output through data streams,
serialization and the file system.
• Java uses the concept of a stream to make I/O operation fast. The java.io
package contains all the classes required for input and output operations.
3 streams are created automatically.All these streams are attached with the
console.
1) System.out: standard output stream
2) System.in: standard input stream
3) System.err: standard error stream
JAVA.IOPACKAGEIN JA
V
A
21. Java.io.FileInputStream Class in Java
FileInputStream is useful to read data from a file in the form of sequence of
bytes.For reading streams of characters, consider using FileReader.
Constructor and Description
FileInputStream(File file) :Creates an input file stream to read from the
specified File object.
FileInputStream(FileDescriptor fdobj) :Creates an input file stream to read
from the specified file descriptor.
FileInputStream(String name) :Creates an input file stream to read from a file
with the specified name.
Important Methods:
int read() :Reads a byte of data from this input stream
int read(byte[] b) :Reads up to b.length bytes of data from this input stream into
an array of bytes.
int read(byte[] b, int off, int len) : Reads up to len bytes of data from this input
stream into an array of bytes.
void close() :Closes this file input stream and releases any system resources
associated with the stream.
22. • FileOutputStream is meant for writing streams of raw bytes
• It can be used to create text files.Afile represents storage of data on a
second storage media like a hard disk or CD. Whether or not a file is
available or may be created
void close() :Closes this file output stream and releases any system resources
associated with this stream.
protected void finalize() :Cleans up the connection to the file, and ensures
that the close method of this file output stream is called when there are no
more references to this stream.
void write(byte[] b) :Writes b.length bytes from the specified byte array to
this file output stream.
void write(byte[] b, int off, int len) :Writes len bytes from the specified
byte array starting at offset off to this file output stream.
void write(int b) :Writes the specified byte to this file output stream.