Title: Basic concept of java (1)
1BASIC CONCEPT OF JAVA
BY
PSK TECHNOLOGIES PVT.LTD
IT COMPANY NAGPUR
WEBSITEhttps//www.pskitservices.com/ ADDRESSPLO
T NO.780 NEAR DURGA TEMPLE,
KATOL ROAD CHAONI,NAGPUR-13 CONTACT 9975288300
2Q Can we override static method? A No, static
method cannot be overridden. It can be proved by
runtime polymorphism, so we will learn it
later. Q Why we cannot override static
method? A because static method is bound with
class whereas instance method is bound with
object. Static belongs to class area and instance
belongs to heap area. Q Can we override java
main method? A No, because main is a static
method.
WEBSITEhttps//www.pskitservices.com/CONTACT9975
288300
3- Difference between method overloading and method
overriding in java - Method Overloading
- Method overloading is used to increase the
readability of the program. - Method overloading is performed within class.
- In case of method overloading, parameter must be
different - Method overloading is the example of compile time
polymorphism. - 5)In java, method overloading can't be performed
by changing return type of the
method only.
Return type can be same or different in method
overloading. But you must have to change the
parameter.
WEBSITEhttps//www.pskitservices.com/CONTACT9975
288300
4- Method Overriding
- Method overriding is used to provide the specific
implementation of the method that is already
provided by its super class. - 2) Method overriding occurs in two classes
(inheritance). - 3) In case of method overriding, parameter must
be same. - 4) Method overriding is the example of run time
polymorphism. - 5) Return type must be same or covariant in
method overriding.
WEBSITEhttps//www.pskitservices.com/CONTCAT9975
288300
5/this keyword in java/ To refer the current
class instance variable To invoke the current
class constructor To invoke the current class
method To pass as an argument in the method
call To pass as an argument in the constructor
call To return the current class
instance Proving this keyword There can be a lot
of usage of java this keyword. In java, this is a
reference variable that refers to the current
object.
WEBSITEhttps//www.pskitservices.com/CONTACT9975
288300
6- /Usage of java this keyword/
- Here is given the 6 usage of java this keyword.
- This() keyword can be used to refer current class
instance variable. - 2. This() can be used to invoke current class
constructor. - 3 .This() keyword can be used to invoke current
class method (implicitly) - 4. This() can be passed as an argument in the
method call. - 5. This() can be passed as argument in the
constructor call. - 6 .This() keyword can also be used to return the
current class instance.
WEBSITEhttps//www.pskitservices.com/CONTACT9975
288300
7Multithreading in java with examples A thread is
a light-weight smallest part of a process that
can run concurrently With the other parts(other
threads) of the same process. Threads are
independent because they all have separate path
of execution thats the reason if an exception
occurs in one thread, it doesnt affect the
execution of other threads. All threads of a
process share the common memory. The process of
executing multiple threads simultaneously is
known as multithreading. The main purpose of
multithreading is to provide simultaneous
execution of two or more parts of a program to
maximum utilize the CPU time. A multithreaded
program contains two or more parts that can run
concurrently. Each such part of a program called
thread.
WEBSITEhttps//www.pskitservices.com/CONTACT9975
288300
82. Threads are lightweight sub-processes, they
share the common memory space. In Multithreaded
environment, programs that are benefited from
multithreading, utilize the maximum CPU time so
that the idle time can be kept to minimum. 3. A
thread can be in one of the following states A
thread can be in only one state at a given point
in time. Multitasking vs Multithreading vs
Multiprocessing vs parallel processing
WEBSITEhttps//www.pskitservices.com/CONTACT9975
288300
9- If you are new to java you may get confused among
these terms as they are used quite frequently
when we discuss multithreading. Lets talk about
them in brief. - Multitasking Ability to execute more than one
task at the same time is known as multitasking. - Multithreading We already discussed about it. It
is a process of executing multiple threads
simultaneously. Multithreading is also known as
Thread-based Multitasking. - Multiprocessing It is same as multitasking,
however in multiprocessing more than one CPUs are
involved. On the other hand one CPU is involved
in multitasking. - Parallel Processing It refers to the utilization
of multiple CPUs in a single computer system. - Creating a thread in Java
- There are two ways to create a thread in Java
- By extending Thread class.
- 2) By implementing Runnable interface.
WEBSITEhttps//www.pskitservices.com/CONTACT9975
288300
10Before we begin with the programs (code) of
creating threads, lets have a look at these
methods of Thread class. We have used few of
these methods in the example below. getName()
It is used for Obtaining a threads
name getPriority() Obtain a threads
priority isAlive() Determine if a thread is
still running join() Wait for a thread to
terminate run() Entry point for the
thread sleep() suspend a thread for a period
of time start() start a thread by calling its
run() method
WEBSITEhttps//www.pskitservices.com/CONTACT9975
288300
11- Methods isAlive() and join()
- In all the practical situations main thread
should finish last else other threads which have
spawned from the main thread will also finish. - To know whether the thread has finished we can
call is Alive() on the thread which returns true
if the thread is not finished. - Another way to achieve this by using join()
method, this method when called from the parent
thread makes parent thread wait till child thread
terminates. - These methods are defined in the Thread class.
- We have used is Alive() method in the above
examples too.
WEBSITEhttps//www.pskitservices.com/CONTACT9975
288300
12- Synchronization
- Multithreading introduces asynchronous behavior
to the programs. If a thread is writing some data
another thread may be reading the same data at
that time. This may bring inconsistency. - When two or more threads need access to a shared
resource there should be some way that the
resource will be used only by one resource at a
time. The process to achieve this is called
synchronization. - To implement the synchronous behavior java has
synchronous method. Once a thread is inside a
synchronized method, no other thread can call any
other synchronized method on the same object. All
the other threads then wait until the first
thread come out of the synchronized block.
WEBSITEhttps//www.pskitservices.com/CONTACT9975
28300
13When we want to synchronize access to objects of
a class which was not designed for the
multithreaded access and the code of the method
which needs to be accessed synchronously is not
available with us, in this case we cannot add the
synchronized to the appropriate methods. In java
we have the solution for this, put the calls to
the methods (which needs to be synchronized)
defined by this class inside a synchronized block
in following manner. Synchronized (object)
// statement to be synchronized
WEBSITEhttps//www.pskitservices.com/CONTACT9975
288300
14To make a method synchronized, simply add the
synchronized keyword to its declaration. Then no
two invocations of synchronized methods on the
same object can interleave with each
other. 1.Synchronized statements must specify
the object that provides the intrinsic
synchronized keyword is used for exclusive
accessing. 2.Lock. When synchronized (this) is
used, you have to avoid to synchronizing
invocations of other objects' methods. 3.Wait()
tells the calling thread to give up the monitor
and go to sleep until some other thread enters
the same monitor and calls notify( ). 4.Notify()
wakes up the first thread that called wait() on
the same object.
WEBSITEhttps//www.pskitservices.com/CONTACT9975
288300
15Synchronized ,Wait, notify and notifyall When a
thread running in a synchronized method of an
object is calling the wait() method of the same
object, that thread releases the lock of the
object and is added to that object's waiting
queue. As long as it's there, it sits idle. Note
also that wait() forces the thread to release its
lock. This means that it must own the lock of an
object before calling the wait() method of that
(same) object. Hence the thread must be in one of
the object's synchronized methods or synchronized
block before calling wait().
WEBSITEhttps//www.pskitservices.com/CONTACT9975
288300
16When a thread invokes an object's notify() or
notifyAll() method, one (an arbitrary thread) or
all of the threads in its waiting queue are
removed from the waiting queue to the entry
queue. They then actively contend for the
object's lock, and the one that gets the lock
goes on to execute. If no threads are waiting in
the waiting queue, then notify() and notifyAll()
have no effect. Before calling the notify() or
notifyAll() method of an object, a thread must
own the lock of the object. Hence it must be in
one of the object's synchronized methods or
synchronized block.
WEBSITEhttps//www.pskitservices.com/CONTACT9975
288300
17 OUR SOFTWARE COURSES
WEBSITEhttps//www.pskitservices.com/CONTACT9975
288300
18 OUR HARDWARE COURSES
WEBSITEhttps//www.pskitservices.com/CONTACT99752
88300
19 WEBSITE DESIGNING
WEBSITEhttps//www.pskitservices.com/CONTACT9975
288300
20DIGITAL MARKETING
WEBSITEhttps//www.pskitservices.com/CONTACT9975
288300
21IT TRAINING
WEBSITEhttps//www.pskitservices.com/CONTACT9975
288300
22SALES AND SERVICES
WEBSITEhttps//www.pskitservices.com/CONTACT9975
288300
23THANK YOU
FOLLOW US ON
WEBSITEhttps//www.pskitservices.com/CONTACT9975
288300