You are on page 1of 23

Summer training report

On
Java se

Hasnain Mehadi
BCA 5th Semester
Roll No. 17035010047

Department Of Computer Science


Gyanodaya Degree College
University of Lucknow
1
Course

 Features of Java10
 Concept of OOPs with real life examples
 Packages & interfaces, functional interfaces
 REPL Tool
 Exception Handling
 Streaming
 Multithreading
 Collection Frameworks
 Generics
 Annotations
 JDBC
 GUI Designing (Applet,AWT,SWING,)
 Event handling
 Socket Programming
 Introduction to Design pattern
 Eclipse IDE
 Project

Core Java
Class:
It is a user defined data types .It is a blueprint, a skeleton .It is represent a wide
domain. It has attribute and method

Attribute and method:


It Define the characteristic of the class. Method define the functionality of the
class

e.g. There is a class car which has 1000 color price model etc. as its attribute
and start stop apply brake on the method

Object:
It is the real world entity on which river is object belong to a particular class
object have the same attribute and method of the class object are stored in a
heap memory.
If the car is a class i20 the object of the class

Instance:
The state of an object at a particular moment is called the instance.

2
Instance variable (MemberVariable / DataMumber):
It is always used with the instance/object of the class. E.g. is color , price model
is an instance variable of a car class and will always used by car class objects
itself. It cannot be accessed without instance

Instance method:
It is always used with the instance object of the class that is start stop methods
can be accessed with the object of the car class instance method use instance
variable to perform operations

Principle of oops:

 Encapsulation
 Abstraction
 Inheritance
 Polymorphism

Encapsulation:
Wrapping up of a data and member and method together in a single unit (class)
is known as encapsulation. Because all the property and method of the car is
Wrapped with it. If we remove engine tyre, staring we cannot use the car as
whole.

Abstraction:
Hiding the internal details of from end user is known an abstraction.
Complexity hiding hide the internal functionality or mechanism of the object .It
tells how you can use the object not how the object works. Data hiding hides the
private data from end user to maintain the data security and integrity.

Encapsulation is the feature that is built –in with every java program.

Inheritance:
It is the process of creating new class from existing class. We do inheritance
from reusability of the code. The newly created class is known as subclass and
old class is known as a superclass. Now subclass has all the property (except
private) and method of superclass in its own class. Java support single level,
multilevel, hierarchical inheritance is known as IS-A relationship. It is move
from generalization to specialization.

3
Single Level Inheritance:
When there is a superclass and subclass and one subclass it is known as a single
level inheritance example.

Plant <<Super class>> Plant <<Super class>>

Herbs <<sub class>> Herbs <<sub class>>

Multilevel Inheritance:
When superclass by subclass for the class it is known as a multilevel inheritance

Plant <<Super class>>

Herbs <<super class for mint and sub class for plant>>

Mint <<sub class>>

Hierarchical Inheritance:

When a subclass has more than one subclass and assign the subclass becomes
super is known as a hierarchical inheritance.

4
Plant <<Super class>>

Creeper Herbs Shrubs

Grapes Mint Rose


Java does not support multiple inheritance due to Diamond problem

Limitation:
Subclass has additional overhead of a superclass method if there is a no use of
the method in subclass

Polymorphism:
More than one form of things (method/constructor). We do polymorphism for
the convenience of end-user method constructor overloading method overriding
is an example of the polymorphism.

Constructors:
 The name of the constructor must be same as the class
 it is used to initialize the instance variable of the class
 there is a no return type of the constructor, not even void
 if you don’t create any explicit constructor in our class Java compiler
supplied by default
 we can create explicit default (without parameterized) and parameterized
constructor
 when we have more than one constructor those are different item the
basis of their data type or number of argument passing the constructor it
is known as a constructor overloading
 if we create a parameterized constructor in our class and we are trying to
create object of that class using default constructor Java compiler will
give compile time error
 we always create object of the class using matching constructed because
constructed get involved at a time of the object creation
 we can make a construct the private in this case we cannot create object
of this class outside from the class
 it get invoked at a time of the object creation.
5
Instance block:
It is used to initialize instance variable. It is called when we create object of the
class. If you already assign instance variable and we have instance block and
constructor then order of execution is-

 instance variable with Value


 instance block
 constructor
Use of final keyword

1. To make a variable as constant: final int MAX=30;


2. To make a classes class as final : if we make a class defined it cannot be
inherited by other class.
String, Stringbuild, Stringbuffer, Integer and all wrapper classes are final
class
3. To make a method as final: if we make a method as a final it cannot be
overridden by its child classes. Final void show();

Command line argument:


When we pass parameter at a time of running of the program application it is
known as a command line argument. The runtime system passes the
command line argument to the application main method by an array of
string. This allow the user to specify configuration information when the
application is launched

Variable argument in the method:


We can pass more than an argument in the same method at the time of the
calling when we make a method of this type it is known as a variable
argument in the method variableargs get internally converted into array of
particular data types it get introduced in JDK 1.5

Rules:
 There must be only one variable argument if it exists in a method
 It must be last argument the method
 It must be provided with …(three) dots.

Syntax:
int sum(int …num);

6
Static block variable and method

Static variable:
 It is independent of the object.
 It is shareable to all objects. Whenever we need to make to make a
variable that is common for all object we always prefer to make static
variable e.g. For all student whose belongs to particular School
schoolname will be common, so we can make a schoolname is a static
variable in a studentclass.
 There is only one copy of static variable present in memory
 Get loaded in memory before creation of object
 It is always access with class name using dot (.) “operator” it can be used
with object also but it is not recommended to access object as it is an
independent of object
 It is used to save memory
 It is always preceded with the keyword static
 It is context independent
 We cannot use “this” Keyword with static context because “this”always
belongs to object

Static Method:
 We make static method to use static variable.
 It is always access with class name using dots (.) operator. It can be with
object also but it is not recommended to access with object as it is
independent of object.
 For accessing static method we never needs object of the class, hence by
making static methods we can save memory.
 Generally we make static method to perform that task of the class which
is not object dependent
 Static method cannot access, non static variable .It cannot use “this ”
keyword.

7
Static Block:
 It is used to initializes static variable.
 We can create many blocks we needed.
 We never need to call static block, it get called automatically we can run
the application.
 If we have more than one block, they will be getting called in order they
have been created in the application.
 cannot use instance in this block.

Order of loading of Static Block, Variable, Method:


 Static variable with value
 Static block
 Static method

Method Overloading
When we have more than one with same name but they have are the different
number of parameter are their data types are different or their parameter order a
different. It is known as method overloading method overloading does not
depend on return type of the method. It is implementation of polymorphism. It
is known as compile time polymorphism.

Void add(int x,float y) // will work

Void add(float y,int x) // will work

Void add(int x,int y) // will work

Void add(int x,int y) // it will show error.

Inheritance:
Java.lang.object is a superclass of all classes in Java.

Object creation in inheritance:


Superclass object get created first then subclass, hence first constructor of
superclass will be involved than subclass constructor

Use of super keyword:


 If superclass and subclass both have the same variable/method name to
identify super class variable/method we user super keywords e.g.
super.variablename(), super.methodname()

8
 It is used to call superclass constructor explicitly .super is used to call
default constructor e.g. super(parameter1,parameter2) for parameterized
constructor.

Abstract class:
We cannot instantiate abstract class. It contain static instance and abstract
method but in general with make Abstract class to put only abstract method. It
is used to improve common platform. Abstract class contains constructor but it
cannot be used explicitly because we cannot create object of the abstract class
and constructor are getting invoked at time of object creation. When we are
create subclass object that is get that is getting inherited from super abstract
class then implicitly this constructor will be getting called if we have create
explicit in abstract class. We can create reference variable of the abstract class
We cannot make a class as a final and abstract together.

Abstract class Teacher{}

Abstract method:
Abstract method does not contain any body if a method is declared as abstract.
It must be kept inside abstract class. If a class extend as abstract class, the
subclass must define all the Abstract method given in the superclass or the sub
class must be declared as abstract itself.

Concrete class:
A class of which we can create object is known as a concrete class.

Method overriding:
If superclass and subclass both have the same method signature and subclass
change the definition of superclass method it is known as a method overriding.

Method signature means return type name of method parameter data type and
number of parameters.

Runtime polymorphism:
A superclass reference variable can refer the object of a subclass.

It is dependent on the object of the subclass that is being assigned at runtime to


superclass reference variables. It is also known as a late binding or dynamic
method dispatch. A superclass reference variable cannot access subclass method
before assigning the subclass object

9
Package:
It is a collection of Java classes ,interface and notation etc. We create a package
for reusability of the code it is used to organize classes (byte code) in
hierarchical manner. We can use the class by importing the package in which
they belong.

Building-In Package:
Java.lang,java.util,java.awt etc built in package are there to make application
using java.

Note java.lang is the default package means the class/interface which belong
to this package , for using them we never need to import java.lang as this
package by default import inn every java file.

Static Import:
It get added in a JDK 1.5 for calling only static method variable of the class we
do not need to use class name if you are using static import for example import
Static Java.lang.Math.*. It will import all the static method and variable of
math class

UserDefined package:
We can create our own package and can put class interface there. All the class
and method must be defined as a public so that they can get accessible outside
from the package itself. There must be only one public class in each java file
that belong to package and .java file name and class name must be same.

Package OS;
Public class Linux
{
Public void showFeatures()
{
System.out.println(“multi user OS”);
}
}

10
Package OS;
Public class Dos
{
Public void showFeatures()
{
System.out.println(“single user OS”);
}
}

We need to save these two class with two different name Linux.java and
Dos.java. Now we compile it by saving it in OS folder. In the next class we will
import it to use it and then will run the program as follow follows

Import OS.*;
OS OSDemo;
{
Public static void main(String args[])
{
Linux 1 =new Linux();
1.shoefeature();

Dos 2=new Dos();


2.showFeature();
}
}

By setting the classpath we can use these two classes in the current program.

D;\precursor\set classpath=.;d\;

D:\precursor>javac OSDemo.java

D:\precursor>java OSDemo and we will get the desired output.

Classpath:
Classpath is a path in a Java Virtual Machine(JVM) or Java compiler that
specify the location of a user defined classes and package it can be set using
command prompt or using environment variable.

Access Specifiers/Modifiers:
They are used to tell the scope of a variable method a particular context.

11
Java has a four type of Access Specifires
1 private 2 public
3 protected 4 default

Variable/Method Private Public Protected Default


Same class Yes Yes Yes Yes
Without package
Same package No Yes Yes Yes
Different class
Different class No Yes No NO
Different package
Different package No Yes Yes No
sub class
Same package No Yes Yes Yes
Subclass
Different class No Yes Yes Yes
Without package

Interface:
 It is used to provide common platform.
 If a class implement an interface it just like signing a contract with that
class.
 It contain only abstract method till JDK 1.7.
 In JDK 1.8 we can create a static/default method in the interface also.
 In JDK 1.9 on word we can also make a private method in interface
private method maybe Static also.
 By default all the variable declared the interface are public static and
final
 If a class implement an interface the class must define all the abstract
method given in the in that interface. If it does not to do so, it (class) must
be declared as abstract itself.
 When we define an abstract method in the implemented class ,the
implemented method must precede with public modifiers.
 One interface can extend other interface
 We can create interface inside as well outside from class.
 It is saved with Java extension when will make it is a separate file.
 A class can implement more than one interface at a time.

12
Default method:
The concept of default method is getting added into JDK 1.8 with interface if
we define a default method in an interface it has a body implemented classes are
not bound to redefine this method if there is a no need of that method in the
class generally we define those method as a default in an interface used as
optional in many cases.

Private method:
We can make private method in an interface also this is getting it added in
JDK1.9. This method may be static in nature also.

Functional interface:
An interface is known as functional interface if it contains only one abstract
method.
e.g.Runnable, ctionListener, itemListener are functional interface

Marker interface:
An interface is known as marker interface if it does not contain any type of
method or variable e.g. serializable, cloneable are marker interface it is used to
provide a special meaning to the class.

Runtime polymorphism with interface:


An interface Reference variable can refer the object of the class in which that
interface is implemented

Exception handling
Exception are run time error. When exception error program get terminated
abnormally this should not happen. Exception handling is mechanism in which
by handing the exceptions we do normal termination of the a program. So the
robustness of the application remains.

All exception are store in stack.

13
object

Throwable

Exception Type:
It is of two type checked and unchecked exception.

Checked Exception:
It is checked at compile time. It mean if a method/constructor is throwing a
checked exception it should be handled using try-catch block or it should
declare using throws clause, other wise compiler give error. IOException,
FileNotFoundException, InterrupedException etc. are checked exception.

Unchecked Exception:
Unchecked exception are not checked at compile time .It checked at run time. It
mean if your program is throwing unchecked exception and even if you don’t
handle/declare that exception, the program won’t give a compilation error. Most
of the time these exception occurred due to the bad data provided by user during
the user programmed interaction. It is up to program to judge the condition in
advance that can cause such exception and handle them appropriately all
unchecked exception are directly subclass of a Runtime exception

14
e.g. ArithmeticException, NullPointerException,
ArrayIndexOutOfBoundException.

Exception Keyword:
try
{
We put all those statement in try block on which we have doubt that they
may arise an exception
}
catch(exception type)
{
It is used to catch the appropriate exception thrown by JRE to terminate a
program normally
}

finally
{
It get executed always with the exception occur or not we use finally
block to close all the resources used in a program
}

throws:
It is used to declare the exception when it is written along with the method body
it is used to tell the caller program which is using the method/constructor that
the method/constructor may arise an exception if it is written with method
signature or constructed signature

Public static void main(String args[])throws IOException //declaration of exception


{
//method body
}
Public String readLine()throws IOException
//inform the caller program that I may write this exception

Throw:
It is used to throw an exception explicitly.
Throw<< throwable instance >>

15
Multiple exceptionHandling with single catch block:
We can handle more than one Exception in a single catch block this this concept
is getting added in JDK 1.7
Try
{
}
Catch(SQLException | IOException si)
{
}

Try with resources:


We must close all the resource after using this but if we don’t want to do it
explicitly we can use try with resources it is a local for try block it get added in
JDK 1.7

try(BufferedReader br=new BufferedReader(new InputStreamReader(System.in))

in this case we don’t need to close close br explicitly it close automatically as


soon as it goes out of scope.

From JDK 1.9 onword try with resources are non local they can be used
the global also.

Note: If you are not using multiple exception with a single catch and we have
multiple catches, catch block catching exception must be the last catch because
it can handle all type of exception so it be written in between multiple catches
we will get compile time error
e.g.
try
{
Some code with multiple exception
}
catch(SQLException se)
{
}
catch(Exception e) //error here
{
}
catch(IOException ie)
{
}

16
Note: superclass exception is able to handle all type of subclass exception. If we
have a single try block and catch is not compulsory we can use finally block
instead of catch block, termination will be abnormal

Methods of throwable <<class >>:


public void printstackTrace():
it is used to print a stack of the exception for throwable object

public String getMessage():


returns the details String message of the throwable instance.

Custom Exception:
We can create our own exception class by extending exception class. All
custom exception are checked exception

Assertion:
Assassin is a statement in Java. it can be used to test your assumptions about the
program. While executing assertion it is believed to be true if it fail JVM will
throw an error named assassin. It is mainly used for testing purpose.

Streaming:
It is a technique in which either we can produce information of concern
information. Streaming means flow of data me neither is read data from some
resource or write data to some target. Java has a given various classes/interfaces
to do this task is streaming are divided into two categories on the basis of how
much byte they read and write data at time.

Byte Stream:
the class/interface those belongs to byte stream we will be read or write on bite
information at a time it represent a kind of low level IO. Processing becomes
slow If we read write data into a text file. All other Stream type are built on byte
stream

Character Stream:
The classes interface those belongs to characteristic will read or write two byte
information at a time, hence as the processing will become fast.

17
Character Stream class Hierarchy:
Here Reader and Writer are abstract class.

Serialization:
Write the state of an object into a file is known as serialization. We must
implement the class with serializable<<interface>> of which object we want to
write into a file

Transient Variable: Transient Variable never gets store into a file.


E.g. transient String behavior.

DeSerializations:
Reading the state of an object from the file is known as deserialization.

java.io.ObjectInputStream:<<class>>method:

public final Object readObject() throws IOException, FileNotFoundException:

It is used to read object from a file.

public final void writeObject(Object obj) throws IOException:


It is used to write the object into the file.

Multithreading:
It is a technique in which we can performed more than one task simultaneously.
It means in single application we can run multiple threads to perform different
task parallel.

What is Thread:

 Thread is the smallest unit of the process.


 Each thread has its own path of execution
 Each thread has its own stack
 Each thread share the same address space of the process
 It is known as light weight entity
 In java there are System defined thread and user defined thread
 We can create daemon thread also
 Daemon thread always run in background
 Daemon thread are generally used to make background service
 Very low priority

18
 Only execute when no other thread of the same program is running
 JVM ends the program finishing these thread, when daemon thread are
the only thread running in a program
 Garbage collection is an example of daemon thread
 JVM stops the thread when all the user threads (in contrast to the daemon
threads) are terminated. JVM does not care about daemon thread to
complete when in running state, not even finally block also let execute.
JVM do give performance to non-daemon thread that is created by us

Thread Life Cycle:

Common Method of java.lang Thread<<class>>

public void synchronized void start():


It is used to make a thread in runnable state.

public void run():


It get called when CPU is assigned to thread for its execution. Basically run()
is not the original of the thread class it is the methods of runnable
<<interface>> and since runnable is implemented by thread class so the so
methods become part of thread class.

public void native Boolean isAlive():


It will check whether your thread is dead or alive.

public final void setPriority(int):


It is used to set the priority of the thread but we cannot rely on that as each
operating system have a different scheduling mechanism and behavior. The
value of level must be within the range MIN_PRIORTY and
MAX_PRIORITY. Currently, these value are 1 and 10, respectively. To
return a thread to default priority, specify NORM_PRRIORITY, which is
currently 5.

public final int getPriority:


It is used to fetch the priority of thread.

public final synchronized void setName(String name):


It is used to set the name of the thread explicitly.
19
public final java.lang.String getName(String name):
It is used the fetch the name of the thread explicitly so that we can assign
different task to the thread on the basis of their name

public void synchronized void join(long millisecond )Throws


interruptedException: When a thread use join(--) remaining thread has to
wait for certain period of time that is mentioned in the method parameter.

public void join() throws InterruptedException:


This is the same as the pervious method, but the difference is that here all the
thread has to wait till the time, the thread which use join() has finished its
task.

public final void setDaemon(Boolean):


It is used to make a thread as daemon.

public final void isDaemon():


it will check the thread is daemon or not

public final void stop():


it is used to stop the thread explicitly but in JDK 1.7 this method get
depreciated

public static void sleep(long millisecond) throws InterruptedException:


it is used pause the execution of the thread for specified time interval

public static thread currentThread():


it is give the current running thread in the memory .

Need of Synchronization:
When we have single resource and multiple thread want to access that
resource we need to do synchronization.

Synchronization: Mean applying on class or object.

Class locking: Applying lock on private static variable and static method.

Object locking: Applying lock on private instance variable and instance


method.

20
Synchronization Limitation:
Once to do synchronization the performance of the application will become
little bit slow but now it will be thread safe.

public synchronization void show() to apply object locking.

ThreadSafe<<class>>:
It contain synchronized method and blocks. Vector, hashtable , Stack are
thread safe class.

Collection
Collection : it is a group of object. It is store the object of the class. We can
also say that collection are built-in data stature.

Wrapper class: It is used to wrap primitive data type into its equivalent
object. All classes belong to java.lang package.

Boxing:
Conversion of primitive data type into its equivalat object explicitly is
known as boxing.

int i=10; // primitive data type.

Integer ob=new integer(i); // I get converted into object

21
Auto Boxing:
Convert of primitive data type to its equivalat object implicitly is known as
auto boxing. It gets introduced I JDK 1.5 AutoBoxing is done at compile
time by java compiler.

UnBoxing:
Conversion of object into its primitive data type is known as unboxing.

int j=ob.intValue()

Collection Hierarchy:
All collection classes/ interfaces belong to java.util package

Introduction of collection:

 Reduce programing effort: By providing useful data stature and


algorithm, the collection framework frees you to concentrate on the
important part of your program.

 Increase program speed and quality: This collection frame work


provide high-performance, high-quality implementation of useful data
structure and algorithm.

 Temporary storage of data without using database: Very useful in making


dynamic website where data must be checked temporary memory first,
before communicating it with the original data source.

TypeSafe collection: JDK 1.5 onward with the help of generic collection
become type safe. Mean in single collection basket we can put only similar type
object. So searching and retrieval is not an issue.

UnSafe Collection: Up to JDK 1.4 collection where not type safe mean in
your collection you can put any class object. So it was very difficult to search or
retrieve items from those collection.

22
Legacy classes and interfaces: Before JDK1.2 there were only few
collection classes and interfaces were available.

 Vector
 Hashtable
 Dictionary
 Properties
 Enumeration

List<<interface>>: The class those implement list interface can store duplicate
elements

public abstract int size(): It will return total number of elements present in the
collection.

public abstract Boolean isEmpty(): return true if collection is empty.

public abstract Boolean contains (Object): Return true if the collection will
contain that object.

public abstract Boolean add(E): It is used to add a particular class object into
a collection and if the object get added successfully it will return true.

23

You might also like