Download as pdf or txt
Download as pdf or txt
You are on page 1of 46

Packages in java and how to use them

A package as the name suggests is a pack(group) of classes, interfaces and other packages. In java we use
packages to organize our classes and interfaces. We have two types of packages in Java: built-in packages
and the packages we can create (also known as user defined package). In this guide we will learn what are
packages, what are user-defined packages in java and how to use them.

In java we have several built-in packages, for example when we need user input, we import a package like
this:

import java.util.Scanner
Here:
→ java is a top level package
→ util is a sub package
→ and Scanner is a class which is present in the sub package util.

Before we see how to create a user-defined package in java, lets see the advantages of using a package.

Advantages of using a package in Java

These are the reasons why you should use packages in Java:

 Reusability: While developing a project in java, we often feel that there are few things that we are
writing again and again in our code. Using packages, you can create such things in form of classes
inside a package and whenever you need to perform that same task, just import that package and use
the class.
 Better Organization: Again, in large java projects where we have several hundreds of classes, it is
always required to group the similar types of classes in a meaningful package name so that you can
organize your project better and when you need something you can quickly locate it and use it, which
improves the efficiency.
 Name Conflicts: We can define two classes with the same name in different packages so to avoid
name collision, we can use packages

Types of packages in Java

As mentioned in the beginning of this guide that we have two types of packages in java.
1) User defined package: The package we create is called user-defined package.
2) Built-in package: The already defined package like java.io.*, java.lang.* etc are known as built-in
packages.

We have already discussed built-in packages, lets discuss user-defined packages with the help of examples.

Example 1: Java packages

I have created a class Calculator inside a package name letmecalculate. To create a class inside a package,
declare the package name in the first statement in your program. A class can have only one package
declaration.
Calculator.java file created inside a package letmecalculate

package letmecalculate;

public class Calculator {


public int add(int a, int b){
return a+b;
}
public static void main(String args[]){
Calculator obj = new Calculator();
System.out.println(obj.add(10, 20));
}
}
Now lets see how to use this package in another program.

import letmecalculate.Calculator;
public class Demo{
public static void main(String args[]){
Calculator obj = new Calculator();
System.out.println(obj.add(100, 200));
}
}
To use the class Calculator, I have imported the package letmecalculate. In the above program I have
imported the package as letmecalculate.Calculator, this only imports the Calculator class. However if you
have several classes inside package letmecalculate then you can import the package like this, to use all the
classes of this package.

import letmecalculate.*;

Example 2: Creating a class inside package while importing another package

As we have seen that both package declaration and package import should be the first statement in your java
program. Lets see what should be the order when we are creating a class inside a package while importing
another package.

//Declaring a package
package anotherpackage;
//importing a package
import letmecalculate.Calculator;
public class Example{
public static void main(String args[]){
Calculator obj = new Calculator();
System.out.println(obj.add(100, 200));
}
}
So the order in this case should be:
→ package declaration
→ package import

Example 3: Using fully qualified name while importing a class

You can use fully qualified name to avoid the import statement. Lets see an example to understand this:
Calculator.java

package letmecalculate;
public class Calculator {
public int add(int a, int b){
return a+b;
}
public static void main(String args[]){
Calculator obj = new Calculator();
System.out.println(obj.add(10, 20));
}
}
Example.java

//Declaring a package
package anotherpackage;
public class Example{
public static void main(String args[]){
//Using fully qualified name instead of import
letmecalculate.Calculator obj =
new letmecalculate.Calculator();
System.out.println(obj.add(100, 200));
}
}
In the Example class, instead of importing the package, I have used the full qualified name such
as package_name.class_name to create the object of it. You may also want to read: static import in Java

Sub packages in Java

A package inside another package is known as sub package. For example If I create a package inside
letmecalculate package then that will be called sub package.

Lets say I have created another package inside letmecalculate and the sub package name is multiply. So if I
create a class in this subpackage it should have this package declaration in the beginning:

package letmecalculate.multiply;
Multiplication.java

package letmecalculate.multiply;
public class Multiplication {
int product(int a, int b){
return a*b;
}
}
Now if I need to use this Multiplication class I have to either import the package like this:

import letmecalculate.multiply;
or I can use fully qualified name like this:

letmecalculate.multiply.Multiplication obj =
new letmecalculate.multiply.Multiplication();

Points to remember:

1. Sometimes class name conflict may occur. For example: Lets say we have two
packages abcpackage and xyzpackage and both the packages have a class with the same name, let it
be JavaExample.java. Now suppose a class import both these packages like this:

import abcpackage.*;
import xyzpackage.*;
This will throw compilation error. To avoid such errors you need to use the fully qualified name method that
I have shown above. For example

abcpackage.JavaExample obj = new abcpackage.JavaExample();


xyzpackage.JavaExample obj2 = new xyzpackage.JavaExample();
This way you can avoid the import package statements and avoid that name conflict error.

2. I have already discussed this above, let me mention it again here. If we create a class inside a package
while importing another package then the package declaration should be the first statement, followed by
package import. For example:

package abcpackage;
import xyzpackage.*;
3. A class can have only one package declaration but it can have more than one package import statements.
For example:

package abcpackage; //This should be one


import xyzpackage;
import anotherpackage;
import anything;
4. The wild card import like package.* should be used carefully when working with subpackages. For
example: Lets say: we have a package abc and inside that package we have another package foo, now foo is
a subpackage.

classes inside abc are: Example1, Example 2, Example 3


classes inside foo are: Demo1, Demo2

So if I import the package abc using wildcard like this:

import abc.*;
Then it will only import classes Example1, Example2 and Example3 but it will not import the classes of sub
package.

To import the classes of subpackage you need to import like this:

import abc.foo.*;
This will import Demo1 and Demo2 but it will not import the Example1, Example2 and Example3.

So to import all the classes present in package and subpackage, we need to use two import statements like
this:

import abc.*;
import abc.foo.*;
Setting CLASSPATH:

CLASSPATH can be set by any of the following ways:


 CLASSPATH can be set permanently in the environment: In Windows, choose control panel ? System
? Advanced ? Environment Variables ? choose “System Variables” (for all the users) or “User
Variables” (only the currently login user) ? choose “Edit” (if CLASSPATH already exists) or “New” ?
Enter “CLASSPATH” as the variable name ? Enter the required directories and JAR files (separated by
semicolons) as the value (e.g., “.;c:\javaproject\classes;d:\tomcat\lib\servlet-api.jar”). Take note that
you need to include the current working directory (denoted by ‘.’) in the CLASSPATH.
To check the current setting of the CLASSPATH, issue the following command:

 > SET CLASSPATH


 CLASSPATH can be set temporarily for that particular CMD shell session by issuing the following
command:
 > SET CLASSPATH=.;c:\javaproject\classes;d:\tomcat\lib\servlet-api.jar
 Instead of using the CLASSPATH environment variable, you can also use the command-line option -
classpath or -cp of the javac and java commands, for example,
 > java –classpath c:\javaproject\classes com.abc.project1.subproject2.MyClass3

Illustration of user-defined packages:


Creating our first package:
File name – ClassOne.java

package package_name;

public class ClassOne {


public void methodClassOne() {
System.out.println("Hello there its ClassOne");
}
}

Creating our second package:


File name – ClassTwo.java
package package_one;

public class ClassTwo {


public void methodClassTwo(){
System.out.println("Hello there i am ClassTwo");
}
}

Making use of both the created packages:


File name – Testing.java
import package_one.ClassTwo;
import package_name.ClassOne;

public class Testing {


public static void main(String[] args){
ClassTwo a = new ClassTwo();
ClassOne b = new ClassOne();
a.methodClassTwo();
b.methodClassOne();
}
}
Output:
Hello there i am ClassTwo
Hello there its ClassOne

Now having a look at the directory structure of both the packages and the testing class file:

Important points:
1. Every class is part of some package.
2. If no package is specified, the classes in the file goes into a special unnamed package (the same
unnamed package for all files).
3. All classes/interfaces in a file are part of the same package. Multiple files can specify the same package
name.
4. If package name is specified, the file must be in a subdirectory called name (i.e., the directory name
must match the package name).
5. We can access public classes in another (named) package using: package-name.class-name
What is Interface in Java with Example

What is an Interface?

An interface is just like Java Class, but it only has static constants and abstract method. Java uses Interface to
implement multiple inheritance. A Java class can implement multiple Java Interfaces. All methods in an
interface are implicitly public and abstract.

Syntax for Declaring Interface

interface {
//methods
}

To use an interface in your class, append the keyword "implements" after your class name followed by the
interface name.

Example for Implementing Interface

class Dog implements Pet


interface RidableAnimal extends Animal, Vehicle

Why is an Interface required?

To understand the concept of Java Interface better, let see an example. The class "Media Player" has two
subclasses: CD player and DVD player. Each having its unique implementation method to play music.

Another class "Combo drive" is inheriting both CD and DVD (see image below). Which play method should
it inherit? This may cause serious design issues. And hence, Java does not allow multiple inheritance.
Now let's take another example of Dog.

Suppose you have a requirement where class "dog" inheriting class "animal" and "Pet" (see image below).
But you cannot extend two classes in Java. So what would you do? The solution is Interface.

The rulebook for interface says,

 An interface is 100% abstract class and has only abstract methods.


 Class can implement any number of interfaces.

Class Dog can extend to class "Animal" and implement interface as "Pet".
Java Interface Example:

Step 1) Copy following code into an editor.

interface Pet{
public void test();
}
class Dog implements Pet{
public void test(){
System.out.println("Interface Method Implemented");
}
public static void main(String args[]){
Pet p = new Dog();
p.test();
}
}

Step 2) Save , Compile & Run the code. Observe the Output.

Difference between Class and Interface

Class Interface

In class, you can instantiate variable and create an In an interface, you can't instantiate variable and
object. create an object.

Class can contain concrete(with implementation) The interface cannot contain concrete(with
methods implementation) methods

The access specifiers used with classes are private, In Interface only one specifier is used- Public.
protected and public.
When to use Interface and Abstract Class?

 Use an abstract class when a template needs to be defined for a group of subclasses
 Use an interface when a role needs to be defined for other classes, regardless of the inheritance tree
of these classes

Must know facts about Interface

 A Java class can implement multiple Java Interfaces. It is necessary that the class must implement all
the methods declared in the interfaces.
 Class should override all the abstract methods declared in the interface
 The interface allows sending a message to an object without concerning which classes it belongs.
 Class needs to provide functionality for the methods declared in the interface.
 All methods in an interface are implicitly public and abstract
 An interface cannot be instantiated
 An interface reference can point to objects of its implementing classes
 An interface can extend from one or many interfaces. Class can extend only one class but implement
any number of interfaces
 An interface cannot implement another Interface. It has to extend another interface if needed.
 An interface which is declared inside another interface is referred as nested interface
 At the time of declaration, interface variable must be initialized. Otherwise, the compiler will throw
an error.
 The class cannot implement two interfaces in java that have methods with same name but different
return type.

Summary:

 The class which implements the interface needs to provide functionality for the methods declared in
the interface
 All methods in an interface are implicitly public and abstract
 An interface cannot be instantiated
 An interface reference can point to objects of its implementing classes
 An interface can extend from one or many interfaces. A class can extend only one class but
implement any number of interfaces
Interface vs Abstract Class in Java: What's the Difference?

What is Interface?

The interface is a blueprint that can be used to implement a class. The interface does not contain any
concrete methods (methods that have code). All the methods of an interface are abstract methods.

An interface cannot be instantiated. However, classes that implement interfaces can be instantiated.
Interfaces never contain instance variables but, they can contain public static final variables (i.e., constant
class variables)

What Is Abstract Class?

A class which has the abstract keyword in its declaration is called abstract class. Abstract classes should
have at least one abstract method. , i.e., methods without a body. It can have multiple concrete methods.

Abstract classes allow you to create blueprints for concrete classes. But the inheriting class should
implement the abstract method.

Abstract classes cannot be instantiated.

Important Reasons For Using Interfaces

 Interfaces are used to achieve abstraction.


 Designed to support dynamic method resolution at run time
 It helps you to achieve loose coupling.
 Allows you to separate the definition of a method from the inheritance hierarchy

Important Reasons For Using Abstract Class

 Abstract classes offer default functionality for the subclasses.


 Provides a template for future specific classes
 Helps you to define a common interface for its subclasses
 Abstract class allows code reusability.

Interface Vs. Abstract Class

An abstract class permits you to make functionality that subclasses can implement or override whereas an
interface only permits you to state functionality but not to implement it. A class can extend only one abstract
class while a class can implement multiple interfaces.

Parameters Interface Abstract class

Speed Slow Fast

Multiple
Implement several Interfaces Only one abstract class
Inheritances

Structure Abstract methods Abstract & concrete methods

When to use Future enhancement To avoid independence

Inheritance/ The class can inherit only one Abstract


A Class can implement multiple interfaces
Implementation Class
Parameters Interface Abstract class

While adding new stuff to the interface, it is a In case of Abstract Class, you can take
Default
nightmare to find all the implementors and advantage of the default
Implementation
implement newly defined stuff. implementation.

The interface does not have access modifiers.


Abstract Class can have an access
Access Modifiers Everything defined inside the interface is
modifier.
assumed public modifier.

It is better to use interface when various


It should be used when various
implementations share only method
When to use implementations of the same kind share
signature. Polymorphic hierarchy of value
a common behavior.
types.

Data fields the interface cannot contain data fields. the class can have data fields.

Multiple
A class may implement numerous interfaces. A class inherits only one abstract class.
Inheritance Default

An abstract class can give complete,


An interface is abstract so that it can't provide
Implementation default code which should be
any code.
overridden.

Use of Access You cannot use access modifiers for the You can use an abstract class which
modifiers method, properties, etc. contains access modifiers.

Interfaces help to define the peripheral An abstract class defines the identity of
Usage
abilities of a class. a class.

An abstract class allows you to define


Defined fields No fields can be defined
both fields and constants

An interface can inherit multiple interfaces An abstract class can inherit a class and
Inheritance
but cannot inherit a class. multiple interfaces.

Constructor or An interface cannot declare constructors or An abstract class can declare


destructors destructors. constructors and destructors.

It can extend only one class or one


Limit of Extensions It can extend any number of interfaces.
abstract class at a time.

In an abstract class, the abstract


In an abstract interface keyword, is optional
Abstract keyword keyword is compulsory for declaring a
for declaring a method as an abstract.
method as an abstract.

An interface can have only public abstract An abstract class has protected and
Class type
methods. public abstract methods.
Sample code for Interface and Abstract Class in Java

Following is sample code to create an interface and abstract class in Java

Interface Syntax

interface name{
//methods
}

Java Interface Example:

interface Pet {
public void test();
}
class Dog implements Pet {
public void test() {
System.out.println("Interface Method Implemented");
}
public static void main(String args[]) {
Pet p = new Dog();
p.test();
}
}

Abstract Class Syntax

abstract class name{


// code
}

Abstract class example:

abstract class Shape {


int b = 20;
abstract public void calculateArea();
}

public class Rectangle extends Shape {


public static void main(String args[]) {
Rectangle obj = new Rectangle();
obj.b = 200;
obj.calculateArea();
}
public void calculateArea() {
System.out.println("Area is " + (obj.b * obj.b));
}
}
Stream based I/O (java.io)

Java IO Stream
Java performs I/O through Streams. A Stream is linked to a physical layer by java I/O system to make input
and output operation in java. In general, a stream means continuous flow of data. Streams are clean way to
deal with input/output without having every part of your code understand the physical.
Java encapsulates Stream under java.io package. Java defines two types of streams. They are,

1. Byte Stream : It provides a convenient means for handling input and output of byte.
2. Character Stream : It provides a convenient means for handling input and output of characters.
Character stream uses Unicode and therefore can be internationalized.

Java Byte Stream Classes


Byte stream is defined by using two abstract class at the top of hierarchy, they are InputStream and
OutputStream.

These two abstract classes have several concrete classes that handle various devices such as disk files,
network connection etc.
Some important Byte stream classes.

Stream class Description

BufferedInputStream Used for Buffered Input Stream.

BufferedOutputStream Used for Buffered Output Stream.

DataInputStream Contains method for reading java standard datatype

DataOutputStream An output stream that contain method for writing java standard data
type

FileInputStream Input stream that reads from a file


FileOutputStream Output stream that write to a file.

InputStream Abstract class that describe stream input.

OutputStream Abstract class that describe stream output.

PrintStream Output Stream that contain print() and println() method

These classes define several key methods. Two most important are

1. read() : reads byte of data.


2. write() : Writes byte of data.

Java Character Stream Classes


Character stream is also defined by using two abstract class at the top of hierarchy, they are Reader and
Writer.

These two abstract classes have several concrete classes that handle unicode character.

Some important Charcter stream classes

Stream class Description

BufferedReader Handles buffered input stream.

BufferedWriter Handles buffered output stream.


FileReader Input stream that reads from file.

FileWriter Output stream that writes to file.

InputStreamReader Input stream that translate byte to character

OutputStreamReader Output stream that translate character to byte.

PrintWriter Output Stream that contain print() and println() method.

Reader Abstract class that define character stream input

Writer Abstract class that define character stream output

Reading Console Input


We use the object of BufferedReader class to take inputs from the keyboard.
Reading Characters
read() method is used with BufferedReader object to read characters. As this function returns integer type
value has we need to use typecasting to convert it into char type.
int read() throws IOException
Below is a simple example explaining character input.
class CharRead
{
public static void main( String args[])
{
BufferedReader br = new Bufferedreader(new InputstreamReader(System.in));
char c = (char)br.read(); //Reading character
}
}

Reading Strings in Java


To read string we have to use readLine() function with BufferedReader class's object.
String readLine() throws IOException

Program to take String input from Keyboard in Java

import java.io.*;
class MyInput
{
public static void main(String[] args)
{
String text;
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
text = br.readLine(); //Reading String
System.out.println(text);
}
}

Program to read from a file using BufferedReader class


import java. Io *;
class ReadTest
{
public static void main(String[] args)
{
try
{
File fl = new File("d:/myfile.txt");
BufferedReader br = new BufferedReader(new FileReader(fl)) ;
String str;
while ((str=br.readLine())!=null)
{
System.out.println(str);
}
br.close();
fl.close();
}
catch(IOException e) {
e.printStackTrace();
}
}
}

Program to write to a File using FileWriter class


import java. Io *;
class WriteTest
{
public static void main(String[] args)
{
try
{
File fl = new File("d:/myfile.txt");
String str="Write this string to my file";
FileWriter fw = new FileWriter(fl) ;
fw.write(str);
fw.close();
fl.close();
}
catch (IOException e)
{ e.printStackTrace(); }
}
}

Java - RandomAccessFile

This class is used for reading and writing to random access file. A random access file behaves like a
large array of bytes. There is a cursor implied to the array called file pointer, by moving the cursor we do the
read write operations. If end-of-file is reached before the desired number of byte has been read than
EOFException is thrown. It is a type of IOException.

Constructor

Constructor Description

RandomAccessFile(File Creates a random access file stream to read from, and


file, String mode) optionally to write to, the file specified by the File
argument.

RandomAccessFile(String name, Creates a random access file stream to read from, and
String mode) optionally to write to, a file with the specified name.

Method

Modifier and Method Method


Type

void close() It closes this random access file stream and releases any
system resources associated with the stream.

FileChannel getChannel() It returns the unique FileChannel object associated with


this file.

int readInt() It reads a signed 32-bit integer from this file.

String readUTF() It reads in a string from this file.

void seek(long pos) It sets the file-pointer offset, measured from the
beginning of this file, at which the next read or write
occurs.
void writeDouble(double It converts the double argument to a long using the
v) doubleToLongBits method in class Double, and then
writes that long value to the file as an eight-byte quantity,
high byte first.

void writeFloat(float v) It converts the float argument to an int using the


floatToIntBits method in class Float, and then writes that
int value to the file as a four-byte quantity, high byte first.

void write(int b) It writes the specified byte to this file.

int read() It reads a byte of data from this file.

long length() It returns the length of this file.

void seek(long pos) It sets the file-pointer offset, measured from the
beginning of this file, at which the next read or write
occurs.

Example

1. import java.io.IOException;
2. import java.io.RandomAccessFile;
3.
4. public class RandomAccessFileExample {
5. static final String FILEPATH ="myFile.TXT";
6. public static void main(String[] args) {
7. try {
8. System.out.println(new String(readFromFile(FILEPATH, 0, 18)));
9. writeToFile(FILEPATH, "I love my country and my people", 31);
10. } catch (IOException e) {
11. e.printStackTrace();
12. }
13. }
14. private static byte[] readFromFile(String filePath, int position, int size)
15. throws IOException {
16. RandomAccessFile file = new RandomAccessFile(filePath, "r");
17. file.seek(position);
18. byte[] bytes = new byte[size];
19. file.read(bytes);
20. file.close();
21. return bytes;
22. }
23. private static void writeToFile(String filePath, String data, int position)
24. throws IOException {
25. RandomAccessFile file = new RandomAccessFile(filePath, "rw");
26. file.seek(position);
27. file.write(data.getBytes());
28. file.close();
29. }
30. }

The myFile.TXT contains text "This class is used for reading and writing to random access file."

after running the program it will contains

This class is used for reading I love my country and my peoplele.

Java Console Class

The Java Console class is be used to get input from console. It provides methods to read texts and
passwords.

If you read password using Console class, it will not be displayed to the user.

The java.io.Console class is attached with system console internally. The Console class is introduced since
1.5.

Let's see a simple example to read text from console.

1. String text=System.console().readLine();
2. System.out.println("Text is: "+text);

Java Console class declaration

Let's see the declaration for Java.io.Console class:

1. public final class Console extends Object implements Flushable

Java Console class methods

Method Description

Reader reader() It is used to retrieve the reader object associated with


the console

String readLine() It is used to read a single line of text from the console.

String readLine(String fmt, Object... It provides a formatted prompt then reads the single line
args) of text from the console.

char[] readPassword() It is used to read password that is not being displayed on


the console.

char[] readPassword(String fmt, Object... It provides a formatted prompt then reads the password
args) that is not being displayed on the console.

Console format(String fmt, Object... It is used to write a formatted string to the console
args) output stream.

Console printf(String format, Object... It is used to write a string to the console output stream.
args)

PrintWriter writer() It is used to retrieve the PrintWriter object associated


with the console.

void flush() It is used to flushes the console.

How to get the object of Console

System class provides a static method console() that returns the singleton instance of Console class.

1. public static Console console(){}

Let's see the code to get the instance of Console class.

1. Console c=System.console();

Java Console Example

1. import java.io.Console;
2. class ReadStringTest{
3. public static void main(String args[]){
4. Console c=System.console();
5. System.out.println("Enter your name: ");
6. String n=c.readLine();
7. System.out.println("Welcome "+n);
8. }
9. }

Output

Enter your name: Nakul Jain


Welcome Nakul Jain
Java Console Example to read password

1. import java.io.Console;
2. class ReadPasswordTest{
3. public static void main(String args[]){
4. Console c=System.console();
5. System.out.println("Enter password: ");
6. char[] ch=c.readPassword();
7. String pass=String.valueOf(ch);//converting char array into string
8. System.out.println("Password is: "+pass);
9. }
10. }

Output

Enter password:
Password is: 123
Serialization and Deserialization in Java
1. Serialization
2. Serializable Interface
3. Example of Serialization
4. Example of Deserialization
5. Serialization with Inheritance
6. Externalizable interface
7. Serialization and static data member

Serialization in Java is a mechanism of writing the state of an object into a byte-stream. It is mainly used in
Hibernate, RMI, JPA, EJB and JMS technologies.

The reverse operation of serialization is called deserialization where byte-stream is converted into an object.
The serialization and deserialization process is platform-independent, it means you can serialize an object in
a platform and deserialize in different platform.

For serializing the object, we call the writeObject() method ObjectOutputStream, and for deserialization we
call the readObject() method of ObjectInputStream class.

We must have to implement the Serializable interface for serializing the object.

Advantages of Java Serialization

It is mainly used to travel object's state on the network (which is known as marshaling).

java.io.Serializable interface

Serializable is a marker interface (has no data member and method). It is used to "mark" Java classes so that
the objects of these classes may get a certain capability. The Cloneable and Remote are also marker
interfaces.

It must be implemented by the class whose object you want to persist.


The String class and all the wrapper classes implement the java.io.Serializable interface by default.

Let's see the example given below:

1. import java.io.Serializable;
2. public class Student implements Serializable{
3. int id;
4. String name;
5. public Student(int id, String name) {
6. this.id = id;
7. this.name = name;
8. }
9. }

In the above example, Student class implements Serializable interface. Now its objects can be converted into
stream.

ObjectOutputStream class

The ObjectOutputStream class is used to write primitive data types, and Java objects to an OutputStream.
Only objects that support the java.io.Serializable interface can be written to streams.

Constructor

1) public ObjectOutputStream(OutputStream out) creates an ObjectOutputStream that writes to the


throws IOException {} specified OutputStream.

Important Methods

Method Description

1) public final void writeObject(Object obj) throws writes the specified object to the
IOException {} ObjectOutputStream.

2) public void flush() throws IOException {} flushes the current output stream.

3) public void close() throws IOException {} closes the current output stream.
ObjectInputStream class

An ObjectInputStream deserializes objects and primitive data written using an ObjectOutputStream.

Constructor

1) public ObjectInputStream(InputStream in) creates an ObjectInputStream that reads from


throws IOException {} the specified InputStream.

Important Methods

Method Description

1) public final Object readObject() throws reads an object from the input stream.
IOException, ClassNotFoundException{}

2) public void close() throws IOException {} closes ObjectInputStream.

Example of Java Serialization

In this example, we are going to serialize the object of Student class. The writeObject() method of
ObjectOutputStream class provides the functionality to serialize the object. We are saving the state of the
object in the file named f.txt.

1. import java.io.*;
2. class Persist{
3. public static void main(String args[]){
4. try{
5. //Creating the object
6. Student s1 =new Student(211,"ravi");
7. //Creating stream and writing the object
8. FileOutputStream fout=new FileOutputStream("f.txt");
9. ObjectOutputStream out=new ObjectOutputStream(fout);
10. out.writeObject(s1);
11. out.flush();
12. //closing the stream
13. out.close();
14. System.out.println("success");
15. }catch(Exception e){System.out.println(e);}
16. }
17. }
success
Example of Java Deserialization

Deserialization is the process of reconstructing the object from the serialized state. It is the reverse operation
of serialization. Let's see an example where we are reading the data from a deserialized object.

1. import java.io.*;
2. class Depersist{
3. public static void main(String args[]){
4. try{
5. //Creating stream to read the object
6. ObjectInputStream in=new ObjectInputStream(new FileInputStream("f.txt"));
7. Student s=(Student)in.readObject();
8. //printing the data of the serialized object
9. System.out.println(s.id+" "+s.name);
10. //closing the stream
11. in.close();
12. }catch(Exception e){System.out.println(e);}
13. }
14. }
211 Gabber

Java Serialization with Inheritance (IS-A Relationship)

If a class implements serializable then all its sub classes will also be serializable. Let's see the example given
below:

1. import java.io.Serializable;
2. class Person implements Serializable{
3. int id;
4. String name;
5. Person(int id, String name) {
6. this.id = id;
7. this.name = name;
8. }
9. }
1. class Student extends Person{
2. String course;
3. int fee;
4. public Student(int id, String name, String course, int fee) {
5. super(id,name);
6. this.course=course;
7. this.fee=fee;
8. }
9. }
Now you can serialize the Student class object that extends the Person class which is Serializable. Parent
class properties are inherited to subclasses so if parent class is Serializable, subclass would also be.

Java Serialization with Aggregation (HAS-A Relationship)

If a class has a reference to another class, all the references must be Serializable otherwise serialization
process will not be performed. In such case, NotSerializableException is thrown at runtime.

1. class Address{
2. String addressLine,city,state;
3. public Address(String addressLine, String city, String state) {
4. this.addressLine=addressLine;
5. this.city=city;
6. this.state=state;
7. }
8. }
1. import java.io.Serializable;
2. public class Student implements Serializable{
3. int id;
4. String name;
5. Address address;//HAS-A
6. public Student(int id, String name) {
7. this.id = id;
8. this.name = name;
9. }
10. }

Since Address is not Serializable, you can not serialize the instance of Student class.

Note: All the objects within an object must be Serializable.

Java Serialization with the static data member

If there is any static data member in a class, it will not be serialized because static is the part of class not
object.

1. class Employee implements Serializable{


2. int id;
3. String name;
4. static String company="SSS IT Pvt Ltd";//it won't be serialized
5. public Student(int id, String name) {
6. this.id = id;
7. this.name = name;
8. }
9. }

Java Serialization with array or collection

Rule: In case of array or collection, all the objects of array or collection must be serializable. If any object is
not serialiizable, serialization will be failed.

Externalizable in java

The Externalizable interface provides the facility of writing the state of an object into a byte stream in
compress format. It is not a marker interface.

The Externalizable interface provides two methods:

o public void writeExternal(ObjectOutput out) throws IOException


o public void readExternal(ObjectInput in) throws IOException

Java Transient Keyword

If you don't want to serialize any data member of a class, you can mark it as transient.

1. class Employee implements Serializable{


2. transient int id;
3. String name;
4. public Student(int id, String name) {
5. this.id = id;
6. this.name = name;
7. }
8. }

Now, id will not be serialized, so when you deserialize the object after serialization, you will not get the
value of id. It will return default value always. In such case, it will return 0 because the data type of id is an
integer.

SerialVersionUID

The serialization process at runtime associates an id with each Serializable class which is known as
SerialVersionUID. It is used to verify the sender and receiver of the serialized object. The sender and
receiver must be the same. To verify it, SerialVersionUID is used. The sender and receiver must have the
same SerialVersionUID, otherwise, InvalidClassException will be thrown when you deserialize the object.
We can also declare our own SerialVersionUID in the Serializable class. To do so, you need to create a field
SerialVersionUID and assign a value to it. It must be of the long type with static and final. It is suggested to
explicitly declare the serialVersionUID field in the class and have it private also. For example:
1. private static final long serialVersionUID=1L;

Now, the Serializable class will look like this:

1. import java.io.Serializable;
2. class Employee implements Serializable{
3. private static final long serialVersionUID=1L;
4. int id;
5. String name;
6. public Student(int id, String name) {
7. this.id = id;
8. this.name = name;
9. }
10. }
Java Enumerations
Enumerations was added to Java language in JDK5. Enumeration means a list of named constant. In Java,
enumeration defines a class type. An Enumeration can have constructors, methods and instance variables. It
is created using enum keyword. Each enumeration constant is public, static and final by default. Even
though enumeration defines a class type and have constructors, you do not instantiate an enum using new.
Enumeration variables are used and declared in much a same way as you do a primitive variable.

How to Define and Use an Enumeration

1. An enumeration can be defined simply by creating a list of enum variable. Let us take an example for
list of Subject variable, with different subjects in the list.

//Enumeration defined
enum Subject
{
Java, Cpp, C, Dbms
}

1. Identifiers Java, Cpp, C and Dbms are called enumeration constants. These are public, static and
final by default.
2. Variables of Enumeration can be defined directly without any new keyword.

Subject sub;

3. Variables of Enumeration type can have only enumeration constants as value. We define an enum
variable as enum_variable = enum_type.enum_constant;

sub = Subject.Java;

4. Two enumeration constants can be compared for equality by using the = = relational operator.

Example:
if(sub == Subject.Java) {
...
}

Example of Enumeration
enum WeekDays
{ sun, mon, tues, wed, thurs, fri, sat }

class Test
{
public static void main(String args[])
{
WeekDays wk; //wk is an enumeration variable of type WeekDays
wk = WeekDays.sun; //wk can be assigned only the constants defined under enum type Weekdays
System.out.println("Today is "+wk);
}
}

Today is sun

Example of Enumeration using switch statement


enum Restaurants {
dominos, kfc, pizzahut, paninos, burgerking
}
class Test {
public static void main(String args[])
{
Restaurants r;
r = Restaurants.paninos;
switch(r) { //The name of the enumertion constants are used without their enumeration
type name i.e only r, not Restaurants.r
case dominos: //only constants defined under enum Restaurants can be used
System.out.println("I AM " + r.dominos);
break;
case kfc:
System.out.println("I AM " + r.kfc);
break;
case pizzahut:
System.out.println("I AM " + r.pizzahut);
break;
case paninos:
System.out.println("I AM " + r.paninos);
break;
case burgerking:
System.out.println("I AM " + r.burgerking);
break;
}
}
}

I AM paninos
Values() and ValueOf() method
All the enumerations predefined methods values() and valueOf(). values() method returns an array of enum-
type containing all the enumeration constants in it. Its general form is,
public static enum-type[ ] values()
valueOf() method is used to return the enumeration constant whose value is equal to the string passed in as
argument while calling this method. It's general form is,
public static enum-type valueOf (String str)

Example of enumeration using values() and valueOf() methods:

enum Restaurants {
dominos, kfc, pizzahut, paninos, burgerking
}
class Test {
public static void main(String args[])
{
Restaurants r;
System.out.println("All constants of enum type Restaurants are:");
Restaurants rArray[] = Restaurants.values(); //returns an array of constants of type Restaurants
for(Restaurants a : rArray) //using foreach loop
System.out.println(a);

r = Restaurants.valueOf("dominos");
System.out.println("I AM " + r);
}
}

All constants of enum type Restaurants are:


dominos
kfc
pizzahut
paninos
burgerking
I AM dominos
Points to remember about Enumerations

1. Enumerations are of class type, and have all the capabilities that a Java class has.
2. Enumerations can have Constructors, instance Variables, methods and can even implement
Interfaces.
3. Enumerations are not instantiated using new keyword.
4. All Enumerations by default inherit java.lang.Enum class.

Enumeration with Constructor, instance variable and Method


enum Student
{
John(11), Bella(10), Sam(13), Viraaj(9);
private int age; //variable defined in enum Student
int getage { return age; } //method defined in enum Student
public Student(int age) //constructor defined in enum Student
{
this.age= age;
}
}

class EnumDemo
{
public static void main( String args[] )
{
Student S;
System.out.println("Age of Viraaj is " +Student.Viraaj.getage()+ "years");
}
}

Age of Viraaj is 9 years


In this example as soon as we declare an enum variable(Student S), the constructor is called once, and it
initializes age for every enumeration constant with values specified with them in parenthesis.
auto boxing

Autoboxing and Unboxing:

The automatic conversion of primitive data types into its equivalent Wrapper type is known as boxing and
opposite operation is known as unboxing. This is the new feature of Java5. So java programmer doesn't need
to write the conversion code.

Advantage of Autoboxing and Unboxing:

No need of conversion between primitives and Wrappers manually so less coding is required.

Simple Example of Autoboxing in java:

1.
2. class BoxingExample1{
3. public static void main(String args[]){
4. int a=50;
5. Integer a2=new Integer(a);//Boxing
6.
7. Integer a3=5;//Boxing
8.
9. System.out.println(a2+" "+a3);
10. }
11. }
12.
Output:50 5

Simple Example of Unboxing in java:

The automatic conversion of wrapper class type into corresponding primitive type, is known as Unboxing.
Let's see the example of unboxing:

1.
2. class UnboxingExample1{
3. public static void main(String args[]){
4. Integer i=new Integer(50);
5. int a=i;
6.
7. System.out.println(a);
8. }
9. }

Output:

50
Autoboxing and Unboxing with comparison operators

Autoboxing can be performed with comparison operators. Let's see the example of boxing with comparison
operator:

1.
2. class UnboxingExample2{
3. public static void main(String args[]){
4. Integer i=new Integer(50);
5.
6. if(i<100){ //unboxing internally
7. System.out.println(i);
8. }
9. }
10. }
11.
Output:50

Autoboxing and Unboxing with method overloading

In method overloading, boxing and unboxing can be performed. There are some rules for method overloading
with boxing:
o Widening beats boxing
o Widening beats varargs
o Boxing beats varargs

1) Example of Autoboxing where widening beats boxing

If there is possibility of widening and boxing, widening beats boxing.

1.
2. class Boxing1{
3. static void m(int i){System.out.println("int");}
4. static void m(Integer i){System.out.println("Integer");}
5.
6. public static void main(String args[]){
7. short s=30;
8. m(s);
9. }
10. }
11.
Output:int

2) Example of Autoboxing where widening beats varargs

If there is possibility of widening and varargs, widening beats var-args.

1.
2. class Boxing2{
3. static void m(int i, int i2){System.out.println("int int");}
4. static void m(Integer... i){System.out.println("Integer...");}
5.
6. public static void main(String args[]){
7. short s1=30,s2=40;
8. m(s1,s2);
9. }
10. }

Output:int int

3) Example of Autoboxing where boxing beats varargs

Let's see the program where boxing beats variable argument:

1.
2. class Boxing3{
3. static void m(Integer i){System.out.println("Integer");}
4. static void m(Integer... i){System.out.println("Integer...");}
5.
6. public static void main(String args[]){
7. int a=30;
8. m(a);
9. }
10. }
Output:Integer

Method overloading with Widening and Boxing

Widening and Boxing can't be performed as given below:

1.
2. class Boxing4{
3. static void m(Long l){System.out.println("Long");}
4.
5. public static void main(String args[]){
6. int a=30;
7. m(a);
8. }
9. }

Output:Compile Time Error


Generics in Java

The Java Generics programming is introduced in J2SE 5 to deal with type-safe objects. It makes the code
stable by detecting the bugs at compile time.

Before generics, we can store any type of objects in the collection, i.e., non-generic. Now generics force the
java programmer to store a specific type of objects.

Advantage of Java Generics

There are mainly 3 advantages of generics. They are as follows:

1) Type-safety: We can hold only a single type of objects in generics. It doesn?t allow to store other objects.

Without Generics, we can store any type of objects.

1. List list = new ArrayList();


2. list.add(10);
3. list.add("10");
4. With Generics, it is required to specify the type of object we need to store.
5. List<Integer> list = new ArrayList<Integer>();
6. list.add(10);
7. list.add("10");// compile-time error

2) Type casting is not required: There is no need to typecast the object.

Before Generics, we need to type cast.

1. List list = new ArrayList();


2. list.add("hello");
3. String s = (String) list.get(0);//typecasting
4. After Generics, we don't need to typecast the object.
5. List<String> list = new ArrayList<String>();
6. list.add("hello");
7. String s = list.get(0);

3) Compile-Time Checking: It is checked at compile time so problem will not occur at runtime. The good
programming strategy says it is far better to handle the problem at compile time than runtime.

1. List<String> list = new ArrayList<String>();


2. list.add("hello");
3. list.add(32);//Compile Time Error

Syntax to use generic collection

1. ClassOrInterface<Type>

Example to use Generics in java

2. ArrayList<String>
Full Example of Generics in Java

Here, we are using the ArrayList class, but you can use any collection class such as ArrayList, LinkedList,
HashSet, TreeSet, HashMap, Comparator etc.

1. import java.util.*;
2. class TestGenerics1{
3. public static void main(String args[]){
4. ArrayList<String> list=new ArrayList<String>();
5. list.add("rahul");
6. list.add("jai");
7. //list.add(32);//compile time error
8.
9. String s=list.get(1);//type casting is not required
10. System.out.println("element is: "+s);
11.
12. Iterator<String> itr=list.iterator();
13. while(itr.hasNext()){
14. System.out.println(itr.next());
15. }
16. }
17. }

1. import java.util.*;
2. class TestGenerics1{
3. public static void main(String args[]){
4. ArrayList<String> list=new ArrayList<String>();
5. list.add("rahul");
6. list.add("jai");
7. //list.add(32);//compile time error
8.
9. String s=list.get(1);//type casting is not required
10. System.out.println("element is: "+s);
11.
12. Iterator<String> itr=list.iterator();
13. while(itr.hasNext()){
14. System.out.println(itr.next());
15. }
16. }
17. }

Output:

element is: jai


rahul
jai
Example of Java Generics using Map

Now we are going to use map elements using generics. Here, we need to pass key and value. Let us
understand it by a simple example:

1. import java.util.*;
2. class TestGenerics2{
3. public static void main(String args[]){
4. Map<Integer,String> map=new HashMap<Integer,String>();
5. map.put(1,"vijay");
6. map.put(4,"umesh");
7. map.put(2,"ankit");
8.
9. //Now use Map.Entry for Set and Iterator
10. Set<Map.Entry<Integer,String>> set=map.entrySet();
11.
12. Iterator<Map.Entry<Integer,String>> itr=set.iterator();
13. while(itr.hasNext()){
14. Map.Entry e=itr.next();//no need to typecast
15. System.out.println(e.getKey()+" "+e.getValue());
16. }
17.
18. }}

Output

1 vijay
2 ankit
4 umesh

Generic class

A class that can refer to any type is known as a generic class. Here, we are using the T type parameter to
create the generic class of specific type.

Let's see a simple example to create and use the generic class.

Creating a generic class:

1. class MyGen<T>{
2. T obj;
3. void add(T obj){this.obj=obj;}
4. T get(){return obj;}
5. }

The T type indicates that it can refer to any type (like String, Integer, and Employee). The type you specify
for the class will be used to store and retrieve the data.

Using generic class:

Let's see the code to use the generic class.

1. class TestGenerics3{
2. public static void main(String args[]){
3. MyGen<Integer> m=new MyGen<Integer>();
4. m.add(2);
5. //m.add("vivek");//Compile time error
6. System.out.println(m.get());
7. }}

Output

Type Parameters

The type parameters naming conventions are important to learn generics thoroughly. The common type
parameters are as follows:

1. T - Type
2. E - Element
3. K - Key
4. N - Number
5. V - Value

Generic Method

Like the generic class, we can create a generic method that can accept any type of arguments. Here, the
scope of arguments is limited to the method where it is declared. It allows static as well as non-static
methods.

Let's see a simple example of java generic method to print array elements. We are using here E to denote the
element.

1. public class TestGenerics4{


2.
3. public static < E > void printArray(E[] elements) {
4. for ( E element : elements){
5. System.out.println(element );
6. }
7. System.out.println();
8. }
9. public static void main( String args[] ) {
10. Integer[] intArray = { 10, 20, 30, 40, 50 };
11. Character[] charArray = { 'J', 'A', 'V', 'A', 'T','P','O','I','N','T' };
12.
13. System.out.println( "Printing Integer Array" );
14. printArray( intArray );
15.
16. System.out.println( "Printing Character Array" );
17. printArray( charArray );
18. }
19. }

Output

Printing Integer Array


10
20
30
40
50

Printing Character Array


J
A
V
A
T
P
O
I
N
T

Wildcard in Java Generics

The ? (question mark) symbol represents the wildcard element. It means any type. If we write <? extends
Number>, it means any child class of Number, e.g., Integer, Float, and double. Now we can call the method
of Number class through any child class object.

We can use a wildcard as a type of a parameter, field, return type, or local variable. However, it is not
allowed to use a wildcard as a type argument for a generic method invocation, a generic class instance
creation, or a supertype.

Let's understand it by the example given below:

1. import java.util.*;
2. abstract class Shape{
3. abstract void draw();
4. }
5. class Rectangle extends Shape{
6. void draw(){System.out.println("drawing rectangle");}
7. }
8. class Circle extends Shape{
9. void draw(){System.out.println("drawing circle");}
10. }
11. class GenericTest{
12. //creating a method that accepts only child class of Shape
13. public static void drawShapes(List<? extends Shape> lists){
14. for(Shape s:lists){
15. s.draw();//calling method of Shape class by child class instance
16. }
17. }
18. public static void main(String args[]){
19. List<Rectangle> list1=new ArrayList<Rectangle>();
20. list1.add(new Rectangle());
21.
22. List<Circle> list2=new ArrayList<Circle>();
23. list2.add(new Circle());
24. list2.add(new Circle());
25.
26. drawShapes(list1);
27. drawShapes(list2);
28. }}

Output
drawing rectangle
drawing circle
drawing circle

Upper Bounded Wildcards

The purpose of upper bounded wildcards is to decrease the restrictions on a variable. It restricts the unknown
type to be a specific type or a subtype of that type. It is used by declaring wildcard character ("?") followed
by the extends (in case of, class) or implements (in case of, interface) keyword, followed by its upper bound.

Syntax

1. List<? extends Number>

Here,

? is a wildcard character.

extends, is a keyword.

Number, is a class present in java.lang package

Suppose, we want to write the method for the list of Number and its subtypes (like Integer, Double).
Using List<? extends Number> is suitable for a list of type Number or any of its subclasses
whereas List<Number> works with the list of type Number only. So, List<? extends Number> is less
restrictive than List<Number>.

Example of Upper Bound Wildcard

In this example, we are using the upper bound wildcards to write the method for List<Integer> and
List<Double>.

1. import java.util.ArrayList;
2.
3. public class UpperBoundWildcard {
4.
5.
6. private static Double add(ArrayList<? extends Number> num) {
7.
8. double sum=0.0;
9.
10. for(Number n:num)
11. {
12. sum = sum+n.doubleValue();
13. }
14.
15. return sum;
16. }
17.
18. public static void main(String[] args) {
19.
20. ArrayList<Integer> l1=new ArrayList<Integer>();
21. l1.add(10);
22. l1.add(20);
23. System.out.println("displaying the sum= "+add(l1));
24.
25. ArrayList<Double> l2=new ArrayList<Double>();
26. l2.add(30.0);
27. l2.add(40.0);
28. System.out.println("displaying the sum= "+add(l2));
29.
30.
31. }
32.
33. }

Output

displaying the sum= 30.0


displaying the sum= 70.0

Unbounded Wildcards

The unbounded wildcard type represents the list of an unknown type such as List<?>. This approach can be
useful in the following scenarios: -

o When the given method is implemented by using the functionality provided in the Object class.
o When the generic class contains the methods that don't depend on the type parameter.

Example of Unbounded Wildcards

1. import java.util.Arrays;
2. import java.util.List;
3.
4. public class UnboundedWildcard {
5.
6. public static void display(List<?> list)
7. {
8.
9. for(Object o:list)
10. {
11. System.out.println(o);
12. }
13.
14. }
15.
16.
17. public static void main(String[] args) {
18.
19. List<Integer> l1=Arrays.asList(1,2,3);
20. System.out.println("displaying the Integer values");
21. display(l1);
22. List<String> l2=Arrays.asList("One","Two","Three");
23. System.out.println("displaying the String values");
24. display(l2);
25. }
26.
27. }

Output

displaying the Integer values


1
2
3
displaying the String values
One
Two
Three

Lower Bounded Wildcards

The purpose of lower bounded wildcards is to restrict the unknown type to be a specific type or a supertype
of that type. It is used by declaring wildcard character ("?") followed by the super keyword, followed by its
lower bound.

Syntax

1. List<? super Integer>

Here,

? is a wildcard character.

super, is a keyword.

Integer, is a wrapper class.

Suppose, we want to write the method for the list of Integer and its supertype (like Number, Object).
Using List<? super Integer> is suitable for a list of type Integer or any of its superclasses
whereas List<Integer> works with the list of type Integer only. So, List<? super Integer> is less restrictive
than List<Integer>.

Example of Lower Bound Wildcard

In this example, we are using the lower bound wildcards to write the method for List<Integer> and
List<Number>.

1. import java.util.Arrays;
2. import java.util.List;
3.
4. public class LowerBoundWildcard {
5.
6. public static void addNumbers(List<? super Integer> list) {
7.
8. for(Object n:list)
9. {
10. System.out.println(n);
11. }
12.
13.
14.
15. }
16. public static void main(String[] args) {
17.
18. List<Integer> l1=Arrays.asList(1,2,3);
19. System.out.println("displaying the Integer values");
20. addNumbers(l1);
21.
22. List<Number> l2=Arrays.asList(1.0,2.0,3.0);
23. System.out.println("displaying the Number values");
24. addNumbers(l2);
25. }
26.
27. }

Output

displaying the Integer values


1
2
3
displaying the Number values
1.0
2.0
3.0

You might also like