Professional Documents
Culture Documents
On
Java se
Hasnain Mehadi
BCA 5th Semester
Roll No. 17035010047
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
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.
Multilevel Inheritance:
When superclass by subclass for the class it is known as a multilevel inheritance
Herbs <<super class for mint and sub class for plant>>
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>>
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-
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.
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.
Inheritance:
Java.lang.object is a superclass of all classes in Java.
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 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.
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();
By setting the classpath we can use these two classes in the current program.
D;\precursor\set classpath=.;d\;
D:\precursor>javac OSDemo.java
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
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.
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.
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
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)
{
}
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
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
DeSerializations:
Reading the state of an object from the file is known as deserialization.
java.io.ObjectInputStream:<<class>>method:
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:
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
Need of Synchronization:
When we have single resource and multiple thread want to access that
resource we need to do synchronization.
Class locking: Applying lock on private static variable and static 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.
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.
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:
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 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