Package Section:: 1.predefined Packages
Package Section:: 1.predefined Packages
EX:
package p1;
package p1.p2.p3;
If we want to use package declaration statement in java files then we have to use the
following two condition.
1.Package declaration statement must be the first statement in java file after the
comment section.
2.Package name must be unique, it must not be sharable and it must not be duplicated.
Q)Is it possible to declare more than one package statement
with in a single Java file?
No, it is not possible to declare more than one package declaration statement with in a
single java file, because, package declaration statement must be first statement, in java
files only one package declaration statement must be provided as first statement.
abc.java
package p1;---> Valid
package p2;---> Invalid
package p3;---> INvalid
--
---
To provide package names, JAVA has given a convention like to include our company
domain name in reverse in package names.
3.Import Section:
The main intention of "import" statement is to make available classes and interfaces of a
particular package into the present JAVA file inorder to use in present java file.
Syntax 1:
import package_Name.*;
--> It able to import all the classes and interfaces of the specified package into the present
java file.
EX:
1) class Test
2) {
3) public static void main(String[] args)
4) {
5) for(;;);
6) }
7) }
Status: No Compilation Error
OUTPUT: No Output, but, JVM will be in infinite loop
Access Modifiers:
There are two types of Access modifiers
1.To define scopes to the programming elements, there are four types of access modifiers.
public, protected, <default> and private.
Where public and <default> are allowed for classes, protected and private are not allowed
for classes.
Note: All public, protected , <default> and private are allowed for inner classes.
Note:Where private and protected access modifiers are defined on the basis of classes
boudaries, so that,they are not applicable for classes, they are applicable for members of
the classes including inner classes.
2.To define some extra nature to the programming elements , we have the following
access modifiers.
static, final, abstract, native, volatile, transient, synchronized, strictfp,.......
Where final, abstract and strictfp are allowed for the classes.
Note: The access modifiers like static, abstract, final and strictfp are allowed for inner
classes.
Note: Where static keyword was defined on the basis of classes origin, so that, it is not
applicable for classes, it is applicable for members of the classes including inner classes.
Where 'class' is a java keyword, it can be used to represent 'Class' object oriented feature.
Where 'Class_Name' is name is an identifier, it can be used to recognize the classes
individually.
Where 'extends' keyword is able to specify a particular super class name in class syntax in
order to get variables and methods from the specified super class to the present java
class.
Note: In class syntax, 'extends' keyword is able to allow only one super class name, it will
not allow more than one super class, because, it will represent multiple inheritance, it is
not possible in Java.
Where 'implements' keyword is able to specify one or more no of interfaces in class syntax
in order to provide implementation for all abstract methods of that interfaces in the
present class.
Note: In class syntax, extends keyword is able to allow only one super class, but,
implements keyword is able to allow more than one interface.
Note: In class syntax, 'extends' and 'implements' keywords are optional, we can write a
class with extends keyword and without implements keyword, we can write a class with
implements keyword and without extends keyword, we can write a class without both
extends and implements keywords, if we want to write both extends and implements
keywords first we have to write extends keyword then only we have to write implements
keyword, we must not interchange extends and implements keywords.
1) class A{ } ----> valid
2) public class A{ }----> valid
3) private class A{ }----> Invalid
4) protected class A{ }----> Invalid
2) Abstract Classes:
Abstract class is a java class, it able to allow zero or more no of concrete methods
and zero or more no of abstract methods.
Note: To declare abstract classes, it is not at all mandatory condition to have at least
one abstract method, we can declare abstract classes with 0 no of abstract methods,
but, if we want to declare a method as an abstract method then the respective class
must be abstract class.
For abstract classes, we are able to create only reference variables; we are unable to
create objects.
abstract class A {
----
}
A a = new A();--> Error
A a = null; No Error
Abstract classes are able to provide more sharability when compared with classes.
Procedure to use Abstract Classes:
1) Declare an abstract class with "abstract".
2) Declare concrete methods and abstract methods in abstract class as per the
requirement.
3) Declare sub class for abstract class.
4) Implement abstract methods in sub class.
5) In main class and in main() method, create object for sub class and declare reference
variable either for abstract class or for sub class.
6) Access abstract class members.
Note: If we declare reference variable for abstract class then we are able to access only
abstract class members, but, if we declare reference variable for sub class then we are
able to access both abstract class members and sub class members.
EX:
1) abstract class A
2) {
3) void m1()
4) {
5) System.out.println("m1-A");
6) }
7) abstract void m2();
8) abstract void m3();
9) }
10) class B extends A
11) {
12) void m2()
13) {
14) System.out.println("m2-B");
15) }
16) void m3()
17) {
18) System.out.println("m3-B");
19) }
20) void m4()
21) {
22) System.out.println("m4-B");
23) }
24) }
25) class Test
26) {
27) public static void main(String[] args)
28) {
29) //A a=new A();--> Error
30) A a = new B();
31) a.m1();
32) a.m2();
33) a.m3();
34) //a.m4();--> Error
35) B b = new B();
36) b.m1();
37) b.m2();
38) b.m3();
39) b.m4();
40) }
41) }
Q) What are the differences between Concrete Classes and
Abstract Classes?
1) Classes are able to allow only concrete methods.
Abstract classes are able to allow both concrete methods and abstract methods.
2) To declare concrete classes, only, 'class' keyword is sufficient.
To declare abstract classes we need to use 'abstract’ keyword along with class
keyword.
3) For classes, we are able to create both reference variables and objects,
For abstract classes, we are able to create only reference variables; we are unable to
create objects
3) Interfaces:
Interface is a java feature, it able to allow zero or more no of abstract methods only.
For interfaces, we are able to declare only reference variables; we are unable to
create objects.
In case of interfaces, by default, all the variables as "public static final".
In case of interfaces, by default, all the methods are "public and abstract".
When compared with classes and abstract classes, interfaces will provide more
sharability.
Procedure to Use interfaces in Java applications:
1) Declare an interface with "interface" keyword.
2) Declare variables and methods in interface as per the requirement.
3) Declare an implementation class for interface by including "implements" keyword.
4) Provide implementation for abstract methods in implementation class which are
declared in interface.
5) In main class, in main() method, create object for implementation class ,but, declare
reference variable either for interface or for implementation class.
6) Access interface members.
Note: If declare reference variable for interface then we are able to access only interface
members, we are unable to access implementation class own members. If we declare
reference variable for implementation class then we are able to access both interface
members and implementation class own members.
Example:
1) interface I
2) {
3) int x=20;// public static final
4) void m1();// public and abstract
5) void m2();// public and abstract
6) void m3();// public and abstract
7) }
8) class A implements I
9) {
10) public void m1()
11) {
12) System.out.println("m1-A");
13) }
14) public void m2()
15) {
16) System.out.println("m2-A");
17) }
18) public void m3()
19) {
20) System.out.println("m3-A");
21) }
22) public void m4()
23) {
24) System.out.println("m4-A");
25) }
26) }
27) class Test
28) {
29) public static void main(String[] args)
30) {
31) //I i=new I();---> Error
32) I i=new A();
33) System.out.println(I.x);
34) System.out.println(i.x);
35) i.m1();
36) i.m2();
37) i.m3();
38) //i.m4();----> Error
39) A a=new A();
40) System.out.println(a.x);
41) System.out.println(A.x);
42) a.m1();
43) a.m2();
44) a.m3();
45) a.m4();
46) }
47) }
To declare abstract class, we have to use "abstract" keyword along with class
keyword.
To declare interface we have to use "interface" keyword explicitly.
3) For classes only, we are able to create both reference variables and objects.
For abstract classes and interfaces, we are able to declare reference variables; we
are unable to create objects.
4) In case of interfaces, by default, all variables are "public static final".
In case of classes and abstract classes, no default cases for variables.
5) In case of interfaces, by default, all methods are "public and abstract".
In case of classes and abstract classes, no default cases for methods.
6) Constructors are allowed in classes and abstract classes.
Constructors are not allowed in interfaces.
7) Classes are able to provide less sharability.
Abstract classes are able to provide moddle level sharability.
Interfaces are able to provide more sharability.
'this' Keyword:
'this' is a Java keyword, it can be used to represent current class object.
In Java applications, we are able to utilize 'this' keyword in the following 4 ways.
1) To refer current class variable
2) To refer current class methods
3) To refer current class constructors
4) To refer current class object
1) To Refer Current Class Variables:
If we want to refer current class variables by using 'this' keyword then we have to use
the following syntax.
this.var_Name
NOTE: In Java applications, if we provide same set of variables at local and at class level
and if we access that variables then JVM will give first priority for local variables, if local
variables are not available then JVM will search for that variables at class level, even at
class level also if that variables are not available then JVM will search at super class level.
At all the above locations, if the specified variables are not available then compiler will
rise an error.
NOTE: In Java applications, if we have same set of variables at local and at class level then
to access class level variables over local variables we have to use 'this' keyword.
EX:
1) class A {
2) int i=10;
3) int j=20;
4) A(int i,int j) {
5) System.out.println(i+" "+j);
6) System.out.println(this.i+" "+this.j);
7) }
8) }
9) class Test {
10) public static void main(String args[]) {
11) A a = new A(30,40);
12) }
13) }
OUTPUT:
30 40
10 20
7) m1();
8) this.m1();
9) }
10) }
11) class Test {
12) public static void main(String args[]) {
13) A a = new A();
14) a.m2();
15) }
16) }
OUTPUT:
m2-A
m1-A
m1-A
22) }
OUTPUT:
A-double-param-con
A-float-param-con
A-int-param-con
A-0-arg-con
EX:
1) class A {
2) A() {
3) System.out.println("A-0-arg-con");
4) this(10);
5) }
6) A(int i) {
7) System.out.println("A-int-param-con");
8) this(22.22f);
9) }
10) A(float f) {
11) System.out.println("A-float-param-con");
12) this(33.3333);
13) }
14) A(double d) {
15) System.out.println("A-double-param-con");
16) }
17) }
18) class Test {
19) public static void main(String args[]) {
20) A a = new A();
21) }
22) }
Status: Compilation Error
'static' keyword:
'static' is a Java keyword, it will improve sharability in Java applications.
In Java applications, static keyword will be utilized in the following four ways.
1) Static variables
2) Static methods
3) Static blocks
4) Static import
1) Static variables:
Static variables are normal Java variables, which will be recognized and executed
exactly at the time of loading the respective class byte code to the memory.
Static variables are normal java variables, they will share their last modified values
to the future objects and to the past objects of the respective class.
In Java applications, static variables will be accessed either by using the respective
class reference variable or by using the respective class name directly.
NOTE: To access static variables we can use the respective class reference variable which
may or may not have reference value. To access static variables it is sufficient to take a
reference variable with null value.
NOTE: If we access any non-static variable by using a reference variable with null value
then JVM will rise an exception like "java.lang.NullPointerException". If we access static
variable by using a reference variable contains null value then JVM will not rise any
exception.
In Java applications, static variables must be declared as class level variables only, they
never be declared as local variables.
In Java applications, static variable values will be stored in method area, not in Stack
memory and not in Heap Memory.
In Java applications, to access current class static variables we can use "this" keyword.
EX:
1) class A {
2) static int i=10;
3) int j = 10;
4) void m1() {
5) //static int k=30;--->error
6) System.out.println("m1-A");
7) System.out.println(this.i);
8) }
9) }
10) class Test {
11) public static void main(String args[]) {
12) A a = new A();
13) System.out.println(a.i);
14) System.out.println(A.i);
15) a.m1();
2) Static Methods:
Static method is a normal java method, it will be recognized and executed the time
when we access that method.
Static methods will be accessed either by using reference variable or by using the
respective class name directly.
Note: In the case of accessing static methods by using reference variables, reference
variable may or may not have object reference value, it is possible to access static
methods with the reference variables having 'null' value. If we access non-static method
by using a reference variable contains null value then JVM will rise an exception like
"java.lang.NullPointerException".
Static methods will allow only static members of the current class, static methods will not
allow non-static members of the current class directly.
Note: If we want to access non-static members of the current class in static methods then
we have to create an object for the current class and we have to use the generated
reference variable.
Static methods are not allowing 'this' keyword in its body but to access current class static
methods we are able to use 'this' keyword.
EX:
1) class A {
2) int I = 10;
3) static int j = 20;
4) static void m1() {
5) System.out.println("m1-A");
6) System.out.println(j);
7) //System.out.println(i);---->error
8) //System.out.println(this.j);----------->error
9) A a = new A();
10) System.out.println(a.i);
11) }
12) void m2() {
13) System.out.println("m2-A");
14) this.m1();
15) }
16) }
17) class Test {
18) public static void main(String[] args) {
19) A a = new A();
20) a.m1();
21) a = null;
22) a.m1();
23) A.m1();
24) }
25) }
OUTPUT:
m1-A
20
10
m1-A
20
10
m1-A
20
10
3) Static Block:
Static Block is a set of instructions, which will be recognized and executed at the time
of loading the respective class bytecode to the memory.
Static blocks are able to allow static members of the current class directly, Static
blocks are not allowing non-static members of the current class directly.
Note: If we want to access non-static members of the current class in static block then we
must create object for the respective class and we have to use the generated reference
variable.
Static blocks are not allowing 'this' keyword in its body.
EX:
1) class A {
2) int i=10;
3) static int j=20;
4) static {
5) System.out.println("SB-A");
6) System.out.println(i);//-------->Error
7) A a = new A();
8) System.out.println(a.i);
9) System.out.println(j);
10) System.out.println(this.j);-------->Error
11) }
12) }
13) class Test {
14) public static void main(String[] args) {
15) A a = new A();
16) }
17) }
OUTPUT:
SB-A
10
20
List:
• List is a direct child interface to Collection interface
• List was provided by JAVA along with its JDK1.2 version
• List is index based, it able to arrange all the elements as per indexing.
• List is able to allow duplicate elements.
• List is following insertion order.
• List is not following Sorting order.
• List is able to allow any number of null values.
• List is able to allow heterogeneous elements.
List interface has provided the following methods common to all of its implementation
classes.
• public void add(int index, Object obj)
It able to add the specified element at the specified index value.
• public Object set(int index, Object obj)
It able to set the specified element at the specified index value.
ArrayList:
• It was provided by JAVA along with JDK 1.2 version.
• It is a direct implementation class to List interface.
• It is index based.
• It allows duplicate elements.
• It follows insertion order.
• It will not follow sorting order.
• It allows heterogeneous elements.
• It allows any number of null values.
• Its internal data structure is "Resizable Array".
• Its initial capacity is 10 elements.
• Its incremental capacity ration is
new_Capacity = (Current_Capacity*3/2)+1
• It is best option for frequent retrieval operations.
• It is not synchronized.
• No method is synchronized method in ArrayList.
• It allows more than one thread to access data.
• It follows parallel execution.
• It will reduce execution time.
• It will improve application performance.
• It will not give guarantee for data consistency.
• It is not thread safe.
• It is not Legacy Collection.
Constructors:
• public ArrayList()
It can be used to create an empty ArrayList object with 10 elements as default capacity
value.
EX: ArrayList al = new ArrayList();
• public ArrayList(int capacity)
It can be used to create an empty ArrayList object with the specified capacity.
EX: ArrayList al = new ArrayList(20);
• public ArrayList(Collection c)
It can be used to create an ArrayList object with all the elements of the specified
Collection object.
Vector:
• It was introduced in JDK1.0 version.
• It is Legacy Collection.
• It is a direct implementation class to List interface.
• It is index based.
• It allows duplicate elements.
• It follows insertion order.
• It will not follow sorting order.
• It allows heterogeneous elements.
• It allows any number of null values.
• Its internal data structure is "Resizable Array".
• Its initial capacity is 10 elements.
• It is best choice for frequent retrieval operations.
• It is not good for frequent insertions and deletion operations.
• Its incremental capacity is double the current capacity.
New_capacity = 2*Current_Capacity
• It is synchronized elemenet.
• All the methods of vector class are synchronized.
• It allows only one thread at a time.
• It follows sequential execution.
• It will increase execution time.
• It will reduce application performance.
Methods:
• public void addElement(Object obj)
It will add the specified element to Vector.
• public Object firstElement()
It will return first element of the Vector.
• public Object lastElement()
It will return last element of the Vector.
LinkedList:
• It was introduced in JDK1.2 version.
• It is not Legacy Collection.
• It is a direct implementation class to List interface.
• It is index based.
• It allows duplicate elements.
• It follows insertion order.
• It is not following sorting order.
• It allows heterogeneous elements.
• It allows null values in any number.
• Its internal data structure is "Double Linked List".;
• It is best choice for frequent insertions and deletions.
• It is not synchronized Collection.
• No method is synchronized in LinkedList.
• It allows more than one thread to access data.
• It will follow parallel execution.
• It will decrease execution time.
• It will improve application performance.
• It is not giving guarantee for data consistency.
• It is not threadsafe.
Constructors:
• public LinkedList()
It will create an empty LinkedList object.
EX: LinkedList ll = new LinkedList();
• public LinkedList(Collection c)
It will create LinkedList object with all the elements of the specified Collection object.
Enumeration:
• It is a Legacy Cursor, it is applicable for only Legacy Collections to retrieve elements
in one by one fashion.
• To retrieve elements from Collections by using Enumeration we have to use the
following steps.
• Create Enumeration Object:
To create Enumeration object we have to use the following method from Legacy
Collections.
public Enumeration elements()
• Retrive Elements from Enumeration:
• Check whether more elements are available or not from Current cursor position by
using the following method.
public boolean hasMoreElements()
• It will return true value if at least next element is existed.
• It will return false value if no element is existed from current cursor position.
• If at least next element is existed then read next element and move cursor to next
position by using the following method.
public Object nextElement()
Iterator:
• Iterator is an interface provided JAVA along with its JDK1.2 version.
• Iterator can be used to retrieve all the elements from Collection objects in one by one
fashion.
• Iterator is applicable for all the Collection interface implementation classes to
retrieve elements.
• Iterator is able to allow both read and remove operations while iterating elements.
• If we want to use Iterator in java applications then we have to use the following
steps.
• Create Iterator Object:
• To create Iterator object we have to use the following method from all Collection
implementation classes.
• public Iterator iterator()
• EX: Iterator it = al.iterator();
ListIterator:
• It is an interface provided by JAVA along with JDK1.2 version.
• It able to allow to read elements in both forward direction and backward direction.
• It able to allow the operations like read, insert, replace and remove while iterating
elements.
• If we want to use ListIterator in java applications then we have to use the following
steps.
• Create ListIterator Object:
• To create ListIterator object we have to use the following method.
• public ListIterator listIterator()
• EX: ListIterator lit = ll.listIterator();
• Retrieve Elements from ListIterator
To retrieve elements from ListIterator in Forward direction then we have to use the
following methods.
public boolean hasNext()
It will check whether next element is existed or not from the current cursor position.
public Object next()
It will return next element and it will move cursor to the next position in forward
direction.
public int nextIndex()
It will return next index value from the current cursor position.
SortedSet:
• It was introduced in JDK1.2 version.
• It is a child interface to Set interface.
• It is not index based.
• It is not allowing duplicate elements.
• It is not following insertion order.
• It follows Sorting order.
• It allows only homogeneous elements.
• It will not allow heterogeneous elements, if we are trying to add heterogeneous
elements then JVM will rise an exception like java.lang.ClasscastException.
• It will not allow null values, if we are trying to add any null value then JVM will rise an
exception like java.lang.NullPointerException.
• It able to allow only Comparable objects by default, if we are trying to add non
comparable objects then JVM will rise an exception like java.lang.ClassCastException.
TreeSet:
• It was introduced in JDK 1.2 version.
• It is not Legacy Collection.
• It has provided implementation for Collection, Set, SortedSet and NavigableSet
interfaces.
• It is not index based.
• It is not allowing duplicate elements.
• It is not following insertion order.
• It follows Sorting order.
• It allows only homogeneous elements.
• It will not allow heterogeneous elements, if we are trying to add heterogeneous
elements then JVM will rise an exception like java.lang.ClasscastException.
• It will not allow null values, if we are trying to add any null value then JVM will rise an
exception like java.lang.NullPointerException.
• It able to allow only Comparable objects by default, if we are trying to add non
comparable objects then JVM will rise an exception like
java.lang.ClassCastException.
Map:
• It was introduced in JDK 1.2 version.
• It is not child interface to Collection Interface.
• It able to arrange all the elements in the form of Key-value pairs.
• In Map, both keys and values are objects.
• Duplicates are not allowed at keys, but values may be duplicated.
• Only one null value is allowed at keys side, but, any number of null values are allowed
at values side.
• Both keys and Values are able to allow heterogeneous elements.
• Insertion order is not followed.
• Sorting order is not followed.
HashMap:
• It was introduced in JDK1.2 version.
• It is not Legacy
• It is an implementation class to Map interface.
• It able to arrange all the elements in the form of Key-value pairs.
• In HashMap, both keys and values are objects.
• Duplicates are not allowed at keys, but values may be duplicated.
• Only one null value is allowed at keys side, but, any number of null values are allowed
at values side.
• Both keys and Values are able to allow heterogeneous elements.
• Insertion order is not followed.
• Sorting order is not followed.
• Its internal data structure is "Hashtable".
• Its initial capacity is 16 elements.
• It is not synchronized
• No method is synchronized in HashMap
• It allows more than one thread to access data.
• It follows parallel execution.
• It will reduce application execution time.
• It will improve application performance.
• It is not giving guarantee for data consistency.
• It is not threadsafe.
SortedMap:
• It was introduced in JDK1.2 version.
• It is a direct child interface to Map interface
• It able to allow elements in the form of Key-Value pairs, where both keys and values
are objects.
• It will not allow duplicate elements at keys side, but, it able to allow duplicate
elements at values side.
• It will not follow insertion order.
• It will follow sorting order.
• It will not allow null values at keys side. If we are trying to add null values at keys side
then JVM will rise an exception like java.lang.NullPointerException.
• It will not allow heterogeneous elements at keys side, if we are trying add
heterogeneous elements then JVM will rise an exception like
java.lang.ClassCastException.
• It able to allow only comparable objects at keys side by default, if we are trying to add
non comparable objects then JVM will rise an exception like
java.lang.ClassCastException.
• If we want to add non comparable objects then we must use Comparator.
TreeMap:
• It was introduced in JDK 1.2 version.
• It is not Legacy.
• It is an implementation class to Map, SoortedMap and NavigableMap interfaces.
• It able to allow elements in the form of Key-Value pairs, where both keys and values
are objects.
• It will not allow duplicate elements at keys side, But, it able to allow duplicate
elements at values side.
• It will not follow insertion order.
• It will follow sorting order.
• It will not allow null values at keys side. If we are trying to add null values at keys side
then JVM will rise an exception like java.lang.NullPointerException.
• It will not allow heterogeneous elements at keys side, if we are trying add
heterogeneous elements then JVM will rise an exception like
java.lang.ClassCastException.
• It able to allow only comparable objects at keys side by default, if we are trying to add
non comparable objects then JVM will rise an exception like
java.lang.ClassCastException.
• If we want to add non comparable objects then we must use Comparator.