You are on page 1of 41

TRAINING REPORT

ON
CORE JAVA

Computer Science & Engineering

Under the supervision of


ER. AVINASH SRIVASTAVA

Submitted By
KAPIL KUMAR VERMA,

Submitted To
GOVERNMENT LEATHER INSITITUTE,
AGRA-06
(2018-2019)
Page | 1
Acknowledgement
The Training opportunity I had with D.S.C.S Pvt. Ltd. was a great chance for
learning and professional development. Therefore, I consider myself as a very
lucky individual as I was provided with an opportunity to be a part of it. I am
also grateful for having a chance to meet so many wonderful people and
professionals who led me though this training period.
Bearing in mind previous I am using this opportunity to express my
deepest gratitude and special thanks to the HR of D.S.C.S India who in spite of
being extraordinarily busy with her/his duties, took time out to hear, guide and
keep me on the correct path and allowing me to carry out my project at their
esteemed organization and extending during the training.
Above all no words can express my feelings to my parents, friends all
those persons who supported me during my project.
I am also thankful to all the respondents whose cooperation & support has
helped me a lot in collecting necessary information.

Page | 2
DECLARATION
I sincerely declare that I am the sole writer of this report. The details of training
and experience contain in this report describe my involvement as a trainee in the
field of Computer Science & Engineering. All the information contains in this
report is certain and correct to the knowledge of the author.

Page | 3
)(

()
Page | 4
Preface
I have made this report file on the topic Core Java; I have tried my best to
elucidate all the relevant detail to the topic to be included in the report. While in
the beginning I have tried to give a general view about this topic.
My efforts and wholehearted co-corporation of each and everyone has ended on
a successful note. I express my sincere gratitude to Mr.Avinash Srivatava who
assisting me throughout the preparation of this topic. I thank him for providing
me the reinforcement, confidence and most importantly the track for the topic
whenever I needed it.

Page | 5
Contents
Topic Name Page No
01. Introduction of Java………………………………… 07
02. Feature of java……………………………………… 08
03. Java and C……………………………………….…. 11
04. Java and C++…………………………………….…. 11
05. Object Oriented Principles……………………….…. 12
06. Classes and Objects…………………………….…… 14
07. Data Types……………………………………..……. 15
08. Constructor.....................................................…...... 18
09. Array…………………………………………...……. 22
10. Polymorphism ………………………...……………. 25
11. Inheritance………………………………………….. 26
12. Interface……………………………………………... 30
13. Nested Class………………………………………… 31
14. Java Package………………………………………… 33
15. Wrapper Classes…………………………………….. 38
16. Conclusion……………………………………... 40

Page | 6
INTERODUCTION OF JAVA

Java is a programming language and environment invented by James Gosling and


others in 1991. Java was originally named Oak and was developed as a part of the
Green project at the Sun Company. Java is an object-oriented programming
language with a built-in application programming interface (API) that can handle
graphics and user interfaces and that can be used to create applications or applets.
Because of its rich set of API's, similar to Macintosh and Windows, and its
platform independence, Java can also be thought of as a platform in itself. Java
also has standard libraries for doing mathematics.
Java is the same as C and C++. One major difference is that Java does not have
pointers. However, the biggest difference is that you must write object oriented
code in Java. Procedural pieces of code can only be embedded in objects. In the
following we assume that the reader has some familiarity with a programming
language.
In particular, some familiarity with the syntax of C/C++ is useful. In Java we
distinguish between applications, which are programs that perform the same
functions as those written in other programming languages, and applets, which
are programs that can be embedded in a Web page and accessed over the
Internet. Our initial focus will be on writing applications. When a program is
compiled, a byte code is produced that can be read and executed by any platform
that can run Java.

Page | 7
Features of JAVA

1.Platform Independence:-
The concept of Write-once-run-anywhere (known as the Platform
independent) is one of the important key feature of java language that
makes java as the most powerful language. Not even a single language is
idle to this feature but java is closer to this feature. The programs written
on one platform can run on any platform provided the platform must have
the JVM.

2. Simple:-
There are various features that makes the java as a simple language.
Programs are easy to write and debug because java does not use the
pointers explicitly. It is much harder to write the java programs that can
crash the system but we can not say about the other programming
languages. Java provides the bug free system due to the strong memory
management. It also has the automatic memory allocation and reallocation
system.

3. Object Oriented:-
To be an Object Oriented language, any language must follow at least the
four characteristics.
a.) Inheritance: It is the process of creating the new classes and using the
behavior of the existing classes by extending them just to reuse the existing code
and adding the additional features as needed.
b.) Encapsulation: It is the mechanism of combining the information and
providing the abstraction.
c.) Polymorphism: As the name suggest one name multiple form,
Polymorphism is the way of providing the different functionality by the
functions having the same name based on the signatures of the methods.
d.) Dynamic binding: Sometimes we don't have the knowledge of objects
about their specific types while writing our code. It is the way of providing the
maximum functionality to a program about the specific type at runtime.

4. Robust:-
Page | 8
Java has the strong memory allocation and automatic garbage collection
mechanism. It provides the powerful exception handling and type checking
mechanism as compare to other programming languages. Compiler checks
the program whether there any error and interpreter checks any run time
error and makes the system secure from crash. All of the above features
makes the java language robust.

5. Distributed:-
The widely used protocols like HTTP and FTP are developed in java.
Internet programmers can call functions on these protocols and can get
access the files from any remote machine on the internet rather than
writing codes on their local system. .

6. Portable:-
The feature Write-once-run-anywhere makes the java language portable
provided that the system must have interpreter for the JVM. Java also have
the standard data size irrespective of operating system or the processor.
These features make the java as a portable language.

7. Performance:-
Java uses native code usage, and lightweight process called threads. In the
beginning interpretation of byte coderesulted the performance slow but the
advance version of JVM uses the adaptive and just in time compilation
technique that improves the performance.

8. Multithreaded:
Java is a Multithreaded programming language. Multithreading means a
single program having different threads executing independently at the
same time. heads execute instructions according to the program code in a
process or a program. Multithreading works the similar way as multiple
processes run on one computer. Multithreading programming is a very
interesting concept in Java. In multithreaded programs not even a single
thread disturbs the execution of other thread. Threads are obtained from
the pool of available ready to run threads and they run on the system
CPUs.

9. Interpreted & Compiler:-

Page | 9
We all know that Java is an interpreted language as well. With an
interpreted language such as Java, programs run directly from the source
code. The interpreter program reads the source code and translates it on the
fly into computations. Thus, Java as an interpreted language depends on an
interpreter program. The versatility of being platform independent makes
Java to outshine from other languages. The source code to be written and
distributed is platform independent. Another advantage of Java as an
interpreted language is its error debugging quality. Due to this any error
occurring in the program gets traced. This is how it is different to work
with Java.

10. Architecture Neutral:-


The term architectural neutral seems to be weird, but yes Java is an
architectural neutral language as well. The growing popularity of networks
makes developers think distributed. In the world of network it is essential
that the applications must be able to migrate easily to different computer
systems. Not only to computer systems but to a wide variety of hardware
architecture and operating system architectures as well. The Java compiler
does this by generating byte code instructions, to be easily interpreted on
any machine and to be easily translated into native machine code on the
fly.
.

Page | 10
Java and C
 Java does not include keywords like goto ,sizeof and typedef.
 Java does not contain data types like struct, union and enum.
 Java does not define the type modifiers keywords like auto, register,
signed.
 Java does not support an explicit pointer type.
 Java does not have preprocessor.
 Java does not support variables arguments to functions.
 Java adds many features required for Object-Oriented Programming.

4.Java and C++


 Java does not support operator overloading.
 Java does not support multiple inheritances.
 Java does not support global variables.
 Java has replaced the destructor function with a finalize() function.
 Java is a true object-oriented language while C++ is basically C with
object-oriented extension.
 Java does not provide the access to the pointers.

Page | 11
Object Oriented Principles
The object oriented programming languages provide mechanisms that help you
implement the object-oriented model.

1) Data Hiding and Encapsulation:-


One of the important object-oriented techniques is hiding the data within the
class and making it available only through the methods. This technique is known
as encapsulation because it seals the data (and internal methods) safely inside
the "capsule" of the class, where it can be accessed only by trusted users (i.e., by
the methods of the class). The most important reason is to hide the internal
implementation details of your class. If you prevent programmers from relying
on those details, you can safely modify the implementation without worrying
that you will break existing code that uses the class. Another reason for
encapsulation is to protect your class against accidental or willful stupidity. A
class often contains a number of interdependent fields that must be in a
consistent state. If you allow a programmer (including yourself) to manipulate
those fields directly, he may change one field without changing important
related fields, thus leaving the class in an inconsistent state. If, instead, he has to
call a method to change the field, that method can be sure to do everything
necessary to keep the state consistent. Similarly, if a class defines certain
methods for internal use only, hiding these methods prevents users of the class
from calling them. When all the data for a class is hidden, the methods define
the only possible operations that can be performed on objects of that class. Once
you have carefully tested and debugged your methods, you can be confident that
the class will work as expected. On the other hand, if all the fields of the class
can be directly manipulated, the number of possibilities you have to test
becomes unmanageable. There are other reasons to hide fields and methods of a
class, as well:
 Internal fields and methods that are visible outside the class just clutter up
the API. Keeping visible fields to a minimum keeps your class tidy and
therefore easier to use and understand.
 If a field or method is visible to the users of your class, you have to
document it. Save yourself time and effort by hiding it instead.

2) Inheritance:-
Inheritance is the process by which one object acquires the properties of another
object. This is important because it supports the concept of hierarchical
Page | 12
classification. Most knowledge is made by hierarchical classification.
Inheritance is a compile-time mechanism in Java that allows you to extend a
class (called the base class or super class) with another class (called the derived
class or subclass).
In Java, inheritance is used for two purposes:
 Class inheritance - create a new class as an extension of another class,
primarily for the purpose of code reuse. That is, the derived class inherits
the methods of the base class.
 Interface inheritance - create a new class to implement the methods
defined as part of an interface for the purpose of sub typing. That is a class
that implements an interface “conforms” to the interface.

In Java, unlike C++, these two types of inheritance are made distinct by using
different language syntax. For class inheritance, Java uses the keyword extends
and for interface inheritance Java uses the keyword implements. In Java, unlike
C++, only single class inheritance is supported. I.e., for a given class, there is
only one super class.

3) Polymorphism:-
Polymorphism is the capability of an action or method to do different things
based on the object that it is acting upon. This is the third basic principle of
object-oriented programming.
Two types of polymorphism:
 Overloading and Overriding.
 Dynamic method binding

Page | 13
6. Classes and Objects
A class defines user defined objects and their characteristics. Any concept need
to implement in a java program is encapsulated within a class. A class defines
the attributes and methods of objects of the same type sharing common
characteristics. The main components of classes are:
 Data members (attributes)
 Methods

Classes contain statements that include the declaration of data members, which
specify the type of data to be stored. Methods of class contain a set of executable
statements that gives a desired output.
Creating Classes in Java:-

Code:
classClassName
{
//Declaration of data members
//Declaration of methods
}

Creating Objects of Classes:-


An object is an instance of class and has a unique identity. The identity of an
object distinguishes it from other objects. While object has a unique identity, a
class is an abstraction of the common properties of various objects.
Declaration: Declares a variable that holds the reference to the object.
class_nameobject_name;
Instantiation or creation: creates an object of the specified class. When you
declare an object, memory is not allocated to it. Therefore you cannot store data
in the data members of the object. New operator is used to allocate memory to
an object.
object_name= new_class_name();

Page | 14
7. Data Types

Variables are nothing but reserved memory locations to store values. This means
that when you create a variable you reserve some space in the memory.
Based on the data type of a variable, the operating system allocates memory and
decides what can be stored in the reserved memory. Therefore, by assigning
different data types to variables, you can store integers, decimals, or characters
in these variables.
There are two data types available in Java:
 Primitive Data types
 Reference/Object Data types

Primitive Data types:-There are eight primitive data types supported by


Java. Primitive data types are predefined by the language and named by a
keyword. Let us now look into the eight primitive data types in detail.

Byte:
o Byte data type is an 8-bit signed two's complement integer

o Minimum value is -128 (-2^7)

o Maximum value is 127 (inclusive)(2^7 -1)

o Default value is 0
o Byte datatype is used to save space in large arrays, mainly in place of
integers, since a byte is four times smaller than an integer

o Example: byte a = 100 , byte b = -50

Short:
o Short datatype is a 16-bit signed two's complement integer

o Minimum value is -32,768 (-2^15)

o Maximum value is 32,767 (inclusive) (2^15 -1)


o Short datatype can also be used to save memory as byte data type. A
short is 2 times smaller than an integer
Page | 15
o Default value is 0
o Example: short s = 10000, short r = -20000

Int:
o Int datatype is a 32-bit signed two's complement integer

o Minimum value is - 2,147,483,648 (-2^31)

o Maximum value is 2,147,483,647(inclusive) (2^31 -1)

o Integer is generally used as the default data type for integral values
unless there is a concern about memory.

o The default value is 0

o Example: int a = 100000, int b = -200000

Long:
o Long datatype is a 64-bit signed two's complement integer

o Minimum value is -9,223,372,036,854,775,808 (-2^63)

o Maximum value is 9,223,372,036,854,775,807 (inclusive) (2^63 -1)

o This type is used when a wider range than int is needed

o Default value is 0L

o Example: long a = 100000L, long b = -200000L

Float:
o Float datatype is a single-precision 32-bit IEEE 754 floating point
o Float is mainly used to save memory in large arrays of floating point
numbers
o Default value is 0.0f
o Float datatype is never used for precise values such as currency
o Example: float f1 = 234.5f
Page | 16
Double:
o double datatype is a double-precision 64-bit IEEE 754 floating point

o This datatype is generally used as the default data type for decimal
values, generally the default choice

o Double datatype should never be used for precise values such as


currency

o Default value is 0.0d


o Example: double d1 = 123.4
Boolean:
o boolean datatype represents one bit of information
o There are only two possible values: true and false
o This datatype is used for simple flags that track true/false conditions
o Default value is false
o Example: boolean one = true
Char:
o char datatype is a single 16-bit Unicode character
o Minimum value is '\u0000' (or 0)
o Maximum value is '\uffff' (or 65,535 inclusive)
o Char datatype is used to store any character
o Example: char letterA ='A'

Reference Data types:-


o Reference variables are created using defined constructors of the
classes. They are used to access objects. These variables are declared
to be of a specific type that cannot be changed. For example,
Employee, Puppy, etc.
o Class objects and various type of array variables come under
reference datatype.
o Default value of any reference variable is null.
o A reference variable can be used to refer any object of the declared
type or any compatible type.
o Example: Animal animal = new Animal("giraffe");

Page | 17
8. Constructor
Constructor is the method of special type because it has got the same name of
the class and it is not having any return type.
More over there is no need to call the constructer manually because constructer
gets automatically call while we create an object of the class.
The primary use of the constructer is for initialization.
Types of constructor:
 Non-parameterized constructor
 Parameterized constructor
 Default constructor
Non-parameterized constructor Example:-
classCustmer
{
String name;
int salary;
Custmer()
{
name="rahulsingh";
salary=30000;
}
void display()
{
System.out.println("Hello;my name is "+ name);
System.out.println("My salary"+ salary);
}
public static void main(String args[])
{
Custmerobj = new Custmer();
obj.display();
}
}

Page | 18
Parameterized Constructor:-
Are those constructor those take the parameter. Parameterized constructer
basically use in that time while we pass information at the time of object
creation.
1. In java static thing and function always stored in the class area and in the
method area.
2. Function and function variable (local variable ,stack variable , automatic
variable ) always get execute in the stack variable.
3. In java every object (instance ) and object variable ( instance variable)
always created in the heap and heap variable.
Program for parameterized constructor:-
class Customer
{
String name;
int age;
Customer(String n, int a)
}
void display()
{
System.out.println(“hello my name is ”+name);
System.out.println(“and my age is”+age);
}
static public void main(String args[])
{
Customer obj1 = new Customer(“Rahul singh”,23);
Customer obj2 = new Customer(“kapil”,22);
obj1.display();
obj2.display();
}
}

“This”Keyword :-

Page | 19
This is the pre-define keyboard with the help of that we refer the instance
variable.
name=name (where name can be any name)
this.name = name

Note:-
i. This can be used to refer current class instance variable.
ii. Instance variable are within class or outside the object.
iii. Constructer always refers address of the obj.
iv. This can be use to invoke current class method.

Default constructor:-When we do not create any constructer in java that


time one constructor is automatically created that constructer is known as
default constructer .
Access Specifier / Access Modifier:
In java access specifier is basically use to define the scope of the type and
type member .
In java there are four Access Specifier
i. public access specifier
ii. private access specifier
iii. protected access specifier
iv. default access specifier
(i) public:–
public thing can be accessible anywhere in the java universe.
(ii) private: –
private thing can only be accessible where they are declare ,it
means if we created one private variable in class in that case
that variable can only accessible inside the class only.
(iii) protected-
protected thing can be accessible anywhere within the same
package and in the child classes.

Page | 20
that child classes can be anywhere , it means child class can be
inside the package or outside the package.
(iv) Default:- it is almost similar to the protected access specifier because
default thing is also accessible anywhere only within the same package
but they can’t be accessible outside the package in any situation.

Note- (1).we can only use public and default access specifier before the class
, it means we can use private and protected before the class .
(2).In java class name and file name can be different but we always compile
the program with the file name and execute with the class name.
(3).In java if our class is public in that case file name and class name must be
same.
Non access specifier:-Non access specifier or defined how the data
member (variable and method )are used in other classes .
Commonly use Non access specifier is as follow.
1. static
2. final
3. synchronized
4. abstract
5. Native etc.
Static- Static keyword can be use before the variable, function, static block
and static nested class (inner class).
We cannot use the static keyword before the outer class.
Static Variable and static function:-
There is no need to create an
object of the class to access the static variable and static method.

Page | 21
9. Array
Array is a special kind of variable because in that we can store the more than
one value of similar data type.
1. Array in fix in size
2. Array is reference type.

Types of Array
In java array is in 2 categories.
1. Single dimensional array
2. Multi dimensional array
Three phase of array-
1. Declaration of an array-
int arr[]; []int arr; //wrong
int []arr; Int arr[5]; //wrong
int[] arr;
In java at the of declaration we cannot give the size
2. Instantiation of an array / construction of an array [second
type to array declaration]-
int arr[] = new int[5]; //must
int arr[] = new int[]; // wrong
at the time of construction of array we have to give the size .
3. Initialization (populating)-
int arr[] = {10,20,30,40};
Multi Dimensional Array:- in multi dimension array data store in row and
column.(also known as matrix form)
Syntax:- datatype [][]arr;
datatype[][] arr;
datatype arr[][];
datatype []arr[];
Copy Arrays:- the system class has an arraycopy method that we can use too
efficiently copy data from array into another .
Syntax:-
Page | 22
public static void arraycopy(Object src, intPos, Object dest, intdeskPos , int
length)
String Handling -
string in java are not null terminated arrays of characters . instead
, they are instances of the java.lang.String class. Internally, a string incapsulates
an array of characters .A String is a immutable ,which means that after you
assigned your String object a value , it can not change any more during its life
time.
Note:- string is a class, not a datatype in java.
String creation:- there are two ways to create an String object.
1. Implicitly
2. Explicitly
1. Implicitly:-when you use a String literal , like " Hello World" ,java
automatically creates a String object for you.
String s = "Hello world !"
2. Explicitly:-when we use the new operator to instanciate a String object.
String s = new String("Hello world !");
Important String Operations:-
1. String length:- use the length method from the String class
2. Character array to String conversion-
3. String to array conversions-
Example:-
Class StringArray
{
public static void main(String args[])
{
String s = "hello world";
chararr[] = s.toCharArrayI();
for(int i=0 ; i<arr.length ; i++)
{
System.out.println(arr[i]);
System.out.println();
}
Page | 23
}
}
4. String lower case to upper case:-
Example:-
class Text
{
public static void main(String args[])
{
String s = "Hello World !";
String supper = s.toUpperCase();
String slower = s.toLowerCase();
System.out.println(supper);
System.out.println(slower);
}
}
5.Concatenate to String ( addition of 2 string):-
String s1 = "Hello"; String s2 = "world";
String s3 = "s1.concat(s2)"; // or s1+s2
6. Comparison of two string –
Example:-
Class Ravi
{
public static void main(String...args)
{
String a = "Text";
String b = "Text";
If (a. Equals (b){
System.out.println("a is equal to b");
}
else
{
System.out.println("a is not equal to b");
}
Page | 24
10. Polymorphism
Polymorphism is an important feature of the object oriented programming
language
This word is inherited form two Latin word –
1. Poly-which means many.
2. Morph- which means forms.

With the help of the polymorphism we provide the multiple interface for a single
function.
Types of polymorphism-
1. Static polymorphism(compile time)
2. Dynamic polymorphism ( at run time )
Static polymorphism always happens with reference where as dynamic
polymorphism always happen with object.Reference always created at time of
compilation where as object always is created at run time.

Function overloading/method overloading:-


When we have more than function with the same name but with different
signature that process is known as function overloading.

Types of signature-
1. Number of parameter-
void sum(int , int )
void sum(int,int,int)
2. Data type of parameter-
void sum(int,int)
void sum(int,float)

3. Order of the parameter-


void sum(int,float)
void sum(float,int)

Page | 25
11. Inheritance
Inheritance is an feature of an object oriented programming language
.Inheritance enables a class to inherit the variable and function from the another
class.
The class that inherit the variables and function from the another class is known
as the sub-class or derived class or child class. Whereas the from which the sub
class the inherit the data is known as the super class or base class or parent class.

Types of inheritance-
There are various types of inheritance we have in object oriented programming
language .
1. Single level inheritance:-
When a class is inherited from the single super class the process is known as
single level inheritance.

Class A

Method:-
Class B
Class A
{
}
class B extends class A
{
}
class C extends class A
{
}

Extend is a keyword in java with the help of that we do the inheritance.


Page | 26
Multi level inheritance:-
inheritance: in the case of multilevel inheritance a class is
inherit the data from another sub class.

class a

class b

class c
Method:-
Class a
{
}
Class extends class a
{
}
Class b extends class b
{
}
Multiple inheritance-
When one class is inherited from the more than one class that process is known
is multiple inheritances.
// in java one class cannot inherit the multiple classes simultaneously.
simultaneously
Class A Class b

Class c

// not possible in java


Class C extends A,B
{
} // wrong declaration.
Page | 27
Hierarchical inheritance:-
In the Hierarchical inheritance more than one class derived only single
base class.

Abstract Keyword:-
Abstract is a keyword it can be use with class, with function but we
can not use the abstract keyword before the variable.
Abstract class can only be inherited they can not be instantiated (it
means we can only inherited abstract class we can not create an
object of it).
Abstract method are those method , these are not having the body ,
they must be followed by semi colon .
Abstract class can have the partial information it means abstract
class can have both method.
1. Abstract method(without body)
2. Non-abstract method (with body)
when a class inherit the abstract class in that case concrete class has to define the
body of the class on abstract method of the abstract class
Final Keyword:-
1. the final keyword can use with fuction , with variable and with class.
2. final class can not be inherited . it can only be instantiated (creating
object).
3. We can not override the final method .
4. We can not change the final variable.
final class Xyz
{
}
Class Text extends Xyz
{
}

//Wrong because Class can not be inherited

Page | 28
Final variable:-to create constant variable
Final method:-prevent method overriding
Final class:- prevent inheritance
Super function:-
Super function always calls parent constructer only.
The constructer first statement always in the super function whether we
write it or not.
In java we do not inherit any class in that time one class automatically
inherited that is object.

finalize method():-
it is the method that the garbage collector always calls just before the
deletion destroying the object which is eligible for garbage collection so as
to perform clean up activity.

Method overrides/ functions overriding:-


function overriding is only possible in a case of inheritance and in the case
of interface implementation . it means function overriding is not possible
in the case of the same class.
When we have the as it is function in the super class as well as in the sub
class that process is known as function overriding. It means in the case of
function overriding function name and signature of the function must be
same in super class as well as in the subclass.
In the case of the function overriding function always check with
reference and call with object.
Super keyword:-
With the help of the super keyword we can access the parent class variable
and parent class method from the child class.
We can not use this keyword and super keyword in the static area because
static area is related with the class and these keyword is related with the
instance.

Page | 29
12. Interface

Interface is the 100 percent abstract class because it can only contain the
abstract method . it means we can not have the method in the interface
those have the body .Interface can contain only two things
1. Abstract method Class Extends Class
2. Public static Interface Extends Interface
Class Implements interface
final variable To create an interface we use interface keyword
Interface enter
{
Public static final variable;
Public abstract method();
}
o In java all constant (public static final variable) always comes in the
capital letter.
o If a class want to use an interface in that case we use the implements
keyword.
o In java one class can inherit only one class but it can implements
more than one interface .
o If a class is implementing an interface in that case class has to define
the body of the all method.
Interface implements class // wrong

Syntax- Interface inter


{
}
Interface inter2
{
}
Class Tester implements inter1,inter2
{
}
Page | 30
13. Nested Classes
In java just like methods variables of a class can have another class as its
member.Writing a class with in another is allows in java . the class written
with in class is called nested class and the class that holds the inner class is
called the outer class.
Syntax-
Class OuterDemo
{
Class NestedDemo
{
}
}

Nested class are divided into two class.

1. Non static nested class:-


Static nested class-these are the non static member of a class

2. Static nested class:-


These are the static member of a class

Inner class (Non-static nested class)-


inner classes are a security mechanism in java .we know a class can not be
associated with the access modifier private but if we have the class as the
member of class, then the inner class can be made private and also this is use to
access the private of a class.
Inner classes are three type depending how and where you define them.
Inner class- creating an inner class is quit simple you just need to write a class
within class.
 Regular inner class can access each and every thing of the outer class.

Page | 31
 Regular inner class cannot contain the static declaration that is why we
never have the main method in the regular inner class.
 In the case of regular inner class inheritance is not allow, it means we
cannot directly inherit the inner class.

Local method inner class:-


In java we can write a class with in a method and this will be a
local type like local variable, the scope of the inner class is restricted with In
the method .

Anonymous class-
An inner class declared without a class name is known as anonymous inner
class.
In case of anonymous inner class we declared and instantiated then at same
time generally they are used whenever you need to override the method of a
class or an interface.

STATIC NESTED CLASS-


a static inner class which is a static member of the outer class it can be access
without instantiating the outer class using other static member just like static
member , a static nested class does not have access to the instance variable and
method of the outer class .
Syntax
class Outer{
Static class NestedDemo
{
//statement1//
}
//statement2//
}

Page | 32
14. Java Package
A java package is a group of similar types of classes, interfaces and sub-
packages.

Package in java can be categorized in two form, built-in package and user-
defined package.

There are many built-in packages such as java, lang, awt, javax, swing, net, io,
util, sql etc.

Here, we will have the detailed learning of creating and using user-defined
packages.

Advantage of Java Package

1) Java package is used to categorize the classes and interfaces so that they can
be easily maintained.

2) Java package provides access protection.

3) Java package removes naming collision.

Page | 33
How to access package from another package:-

There are three ways to access the package from outside the package.
1. import package.*;
2. import package.classname;
3. fully qualified name.
1) Using packagename:-

If you use package.* then all the classes and interfaces of this package will be
accessible but not subpackages.

The import keyword is used to make the classes and interface of another
package accessible to the current package.

Example of package that imports the package name:-


Package:-

//save by A.java
package pack;
public class A{
public void msg(){
System.out.println("Hello");
}
}

Programme:-

//save by B.java
package mypack;
import pack.*;

class B{
public static void main(String args[]){
A obj = new A();
obj.msg();
Page | 34
}
}
2) Using packagename.classname:-

If you import package.classname then only declared class of this package will be
accessible.

Example of package by import package.classname

Package:-
//save by A.java

package pack;
public class A
{
public void msg(){
System.out.println("Hello");
}
}

Programme:-
//save by B.java
package mypack;
import pack.A;
class B{
public static void main(String args[])
{
A obj = new A();
obj.msg();
}
}

Page | 35
3) Using fully qualified name

If you use fully qualified name then only declared class of this package will be
accessible. Now there is no need to import. But you need to use fully qualified
name every time when you are accessing the class or interface.

It is generally used when two packages have same class name e.g. java.util and
java.sql packages contain Date class.

Example of package by import fully qualified name

Package:-
//save by A.java
package pack;
public class A{
public void msg()
{
System.out.println("Hello");
}
}

Programme:-
//save by B.java
package mypack;
class B{
public static void main(String args[])
{
pack.A obj = new pack.A();//using fully qualified name
obj.msg();
}
}

Note: If you import a package, sub packages will not be imported.

Page | 36
If you import a package, all the classes and interface of that package will be
imported excluding the classes and interfaces of the subpackages. Hence, you
need to import the subpackage as well.
Note: Sequence of the program must be package then import then class.

Subpackage in java:-

Package inside the package is called the subpackage. It should be created to


categorize the package further.Let's take an example, Sun Microsystem has
definded a package named java that contains many classes like System, String,
Reader, Writer, Socket etc. These classes represent a particular group e.g.
Reader and Writer classes are for Input/Output operation, Socket and
ServerSocket classes are for networking etc and so on. So, Sun has
subcategorized the java package into subpackages such as lang, net, io etc. and
put the Input/Output related classes in io package, Server and ServerSocket
classes in net packages and so on.

The standard of defining package is domain.company.package e.g.


com.javatpoint.bean or org.sssit.dao.
Example of Subpackage:-
package com.javatpoint.core;
class Simple
{
public static void main(String args[])
{
System.out.println("Hello subpackage");
}
}

Page | 37
15. Wrapper classes
Wrapper class in java provides the mechanism to convert primitive into object
and object into primitive.

Since J2SE 5.0, autoboxing and unboxing feature converts primitive into object
and object into primitive automatically. The automatic conversion of primitive
into object is known as autoboxing and vice-versa unboxing.

The eight classes of java.lang package are known as wrapper classes in java.

All the wrapper classes (Integer, Long, Byte, Double, Float, Short) are
subclasses of the abstract class Number.

Primitive Type Wrapper class

Boolean Boolean

Char Character

Byte Byte

Short Short

Int Integer

Long Long

Float Float

Double Double

Page | 38
Boxing:-
The conversion of primitive data to object type is called boxing.
Example:-
int a = 200;
Integer b = new Integer(a);
// this is known as boxing.

Unboxing:-
Converting an object of a wrapper type to its corresponding primitive value is
called unboxing. For example conversion of Integer to int. The Java compiler
applies unboxing when an object of a wrapper class is:
 Passed as a parameter to a method that expects a value of the corresponding
primitive type.
 Assigned to a variable of the corresponding primitive type.

Example:-
C = b.intValue();
// unboxing

Autoboxing:-
Converting a primitive value into an object of the corresponding wrapper class is
called autoboxing. For example, converting int to Integer class. The Java
compiler applies autoboxing when a primitive value is:
 Passed as a parameter to a method that expects an object of the
corresponding wrapper class.
 Assigned to a variable of the corresponding wrapper class.

Example:-
int a = 200;
Integer b = a;
// this is known as Autoboxing.
Int c = b;

Page | 39
16. CONCLUSION
After complete my industrial training, I had been exposed to an IT
technical and programmer working life. Throughout my internship, I could
understand more about the definition of an IT responsible and innovative
technician and programmer in future. Along my training period, I realize that
observation is a main element to find out the root cause of a problem. Not only
for my project but daily activities too. During my project, I cooperate with my
colleagues and operators to determine the problems Moreover; the project
indirectly helps me to learn.
Independently, discipline myself ,be considerate/patient, self-trust, take
initiative and the ability to solve problems, besides my communication skills is
strengthen as well when communicating with others. During my training period,
I have received criticism and advice from engineers and technician guidance for
me to change myself and avoid myself making the same mistakes again. Apart
from that, I had also Learn my programming skills through various programs
that I had done. This also helps sharpen my skills in html , css since most of the
programs were done with In sum, the activities that I had learned during
industrial training really are useful for me in future to face challenges in a
working environment. Throughout the industrial training, I found that several
things are important.

 Critical and Analytical Thinking


To organize our tasks and assignment, we need to analyse our problems and
assignment, and to formulate a good solution to the problem. We would have to
set contingency plan for the solution, so that we are well prepared for the
unforeseeable situations.

 Time Management
As overall technician and programmer are always racing against tight timeline
and packed schedule, a proper time management will minimize facing overdue
deadlines. An effective time management allows us to do our assignment

Page | 40
efficiently and meet our schedules , Scheduling avoids time wastage and allows
us to plan ahead, and gaining more as a result.
 Opposing to a Herculean goal seemed to be reachable at first sight it is
better to sub-divide the goals to a few achievable tasks, so that we will be
gaining more confidence by accomplishing those tasks.

 Colleague Interactions
In working environment, teamwork is vital in contributing to a strong
organization. Teamwork is also essential in reaching the goals of the
organization as an entity. Thus, communicating and sharing is much needed in
the working environment. Therefore, together as a team, instead of working
alone. This is because working together as a team is easier in reaching our
targets rather than operating individually.
I would like to once again appreciate everyone who has made my industrial a
superb experience.

(.)

Page | 41

You might also like