Java PPT - 3 by Adi
Java PPT - 3 by Adi
Unit-3
Dr. K ADISESHA
Java OOPs Tutorial 2
Introduction
Inheritance
Interfaces
Package
Multithreading
Prof. K. Adisesha
3
Introduction
Introduction:
Object-Oriented Programming is a methodology or paradigm to design a program using
classes and objects.
➢ It simplifies software development and maintenance by providing some concepts:
❖ Object
❖ Class
❖ Inheritance
❖ Polymorphism
❖ Abstraction
❖ Encapsulation
❖ Coupling & Cohesion
❖ Association
❖ Aggregation
❖ Composition
Prof. K. Adisesha
4
Introduction
Inheritance in Java:
Types of inheritance in java
➢ On the basis of class, there can be three types of inheritance in java: Single, Multilevel
and Hierarchical.
➢ In java programming, multiple and hybrid inheritance is supported through interface
only.
Prof. K. Adisesha
7
Java OOPs
Inheritance in Java:
Multiple and Hybrid inheritance in java
➢ Java does not support Multiple inheritance, where a class inherits from multiple classes.
This was done to avoid the diamond problem, where two super-classes of a class have a
common subclass and cause ambiguity in the subclass's implementation.
➢ Java does support Hybrid inheritance, where a class implements multiple interfaces. This
is done to provide a common interface for multiple classes to implement and share
behavior.
➢ Instead of multiple inheritance, Java provides alternatives such as aggregation and
interface implementation to achieve similar functionality.
Prof. K. Adisesha
8
Java OOPs
Java Abstraction:
Data abstraction is the process of hiding certain details and showing only essential
information to the user. Abstraction can be achieved with either abstract classes or
interfaces.
➢ To access the abstract class, it must be inherited from another class.
➢ The abstract keyword is a non-access modifier, used for classes and methods:
❖ Abstract class: is a restricted class that cannot be used to create objects (to access
it, it must be inherited from another class). abstract class ClassA {…}
❖ Abstract method: can only be used in an abstract class, and it does not have a
body. The body is provided by the subclass (inherited from).
// Abstract method (does not have a body)
Prof. K. Adisesha public abstract void Method1();
9
Java OOPs
Java Abstraction:
A class which is declared as abstract is known as an abstract class. It can have abstract
and non-abstract methods.
➢ It needs to be extended and its method implemented. It cannot be instantiated.
➢ An abstract class must be declared with an abstract keyword.
➢ It can have abstract and non-abstract methods.
➢ It cannot be instantiated.
➢ It can have constructors and static methods also.
➢ It can have final methods which will force the subclass not to change the body of the method.
➢ Example of abstract class ➢ Example of abstract method
abstract class A{} abstract void printStatus();//no method body and abstract
Prof. K. Adisesha
10
Java OOPs
Prof. K. Adisesha
16
Interfaces
Java - Interfaces:
Implementing Interfaces− When a class implements an interface, you can think of the class as
signing a contract, agreeing to perform the specific behaviors of the interface. If not the class must
declare itself as abstract.
➢ A class uses the implements keyword to implement an interface. The implements keyword
appears in the class declaration following the extends portion of the declaration..
/* File name : BCA.java */ public static void main(String args[])
public class BCA implements Student { {
public void Personal () BCA obj = new BCA();
{ System.out.println(“K. ADISESHA"); obj. Personal();
} obj. Course();
public void Course () } Output:
{ System.out.println(“Java Programming"); } K. ADISESHA
} } Java Programming
Prof. K. Adisesha
17
Interfaces
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.
➢ Advantage of Java Package
❖ Java package is used to categorize the classes and interfaces so that they can be
easily maintained.
❖ Java package provides access protection.
❖ Java package removes naming collision.
Prof. K. Adisesha
19
Java Package
Built-in Packages:
The Java API is a library of prewritten classes, that are free to use, included in the Java
Development Environment(JDE).
➢ The library is divided into packages and classes. Meaning you can either import a single
class (along with its methods and attributes), or a whole package that contain all the classes
that belong to the specified package.
➢ To use a class or a package from the library, you need to use the import keyword:
➢ Syntax
❖ import package.name.Class; // Import a single class import java.util.Scanner;
❖ import package.name.*; // Import the whole package import java.util.*;
Prof. K. Adisesha
20
Java Package
Creating a Package:
While creating a package, you should choose a name for the package and include a
package statement along with that name at the top of every source file that contains the
classes, interfaces, enumerations, etc., that you want to include in the package.
➢ The package statement should be the first line in the source file. There can be only one
package statement in each source file, and it applies to all types in the file.
❖ package <file_name>;
➢ To compile the Java programs with package statements, you have to use -d option as shown
below. package mypack;
class MyPackageClass {
❖ javac -d Destination_folder <file_name.java.> public static void main(String[] args) {
To compile the package: C:\Users>javac -d . MyPackageClass.java System.out.println("This is my package!");
To run the package : C:\Users>java mypack.MyPackageClass }}
Prof. K. Adisesha
21
Java Package
Threads In Java:
Threads allows a program to operate more efficiently by doing multiple things at the
same time.
➢ Threads can be used to perform complicated tasks in the background without interrupting the
main program.
➢ There are two ways to create a thread:
❖ It can be created by extending the Thread class and overriding its run() method:
❖ Another way to create a thread is to implement the Runnable interface:
Extend Syntax: Implement Syntax
public class Main extends Thread { public class Main implements Runnable {
public void run() { public void run() {
System.out.println("This code is running in a thread"); System.out.println("This code is running in a thread");
} } } }
Prof. K. Adisesha
23
Threads In Java
Running Threads:
The major difference is that when a class extends the Thread class, you cannot extend
any other class, but by implementing the Runnable interface, it is possible to extend from
another class as well.
➢ If the class extends the Thread class, the thread can be run by creating an instance of the class
and call its start() method:
➢ Extend Example public class Main extends Thread
{ public static void main(String[] args)
{ Main thread = new Main();
thread.start();
System.out.println("This code is outside of the thread"); }
public void run()
{ System.out.println("This code is running in a thread"); }
Prof. K. Adisesha
}
24
Threads In Java
Running Threads:
Thread class, by implementing the Runnable interface, it is possible to extend from
another class as well.
➢ If the class implements the Runnable interface, the thread can be run by passing an instance of
the class to a Thread object's constructor and then calling the thread's start() method:
➢ Implement Example: public class Main implements Runnable
{ public static void main(String[] args)
{ Main obj = new Main();
Thread thread = new Thread(obj);
thread.start();
System.out.println("This code is outside of the thread"); }
public void run()
{ System.out.println("This code is running in a thread"); } }
Prof. K. Adisesha
25
Threads In Java
Prof. K. Adisesha
26
Threads In Java
Prof. K. Adisesha
27
Threads In Java
Thread Lifecycle in Java:
Following are the stages of the life cycle:
➢ New − A new thread begins its life cycle in the new state. It remains in this state until the
program starts the thread. It is also referred to as a born thread.
➢ Runnable − After a newly born thread is started, the thread becomes runnable. A thread in this
state is considered to be executing its task.
➢ Waiting − Sometimes, a thread transitions to the waiting state while the thread waits for another
thread to perform a task.
➢ Timed Waiting − A runnable thread can enter the timed waiting state for a specified interval of
time. A thread in this state transitions back to the runnable state when that time interval expires
or when the event it is waiting for occurs.
➢ Terminated (Dead) − A runnable thread enters the terminated state when it completes its task or
otherwise terminates.
Prof. K. Adisesha
28
Multithreading in Java
Multitasking in Java:
Multitasking is the process that lets users perform multiples tasks at the same time. There
are two ways to enable multitasking in Java:.
➢ Process-based multitasking: The processes in this type of multitasking are heavy and a
lot of time is consumed. This is because the program takes a long time to switch between
different processes.
➢ Thread-based multi tasking: Threads are light-weight compared to process-based multi
tasking, and the time taken to switch between them is shorter.
➢ There are two ways to create a thread:
❖ By extending Thread class (extend syntax)
❖ By implementing Runnable interface.
Prof. K. Adisesha
29
Multithreading in Java
Multithreading in Java:
Multithreading in Java is a process of executing multiple threads simultaneously.
➢ A thread is a lightweight sub-process, the smallest unit of processing. Multiprocessing and
multithreading, both are used to achieve multitasking.
➢ Threads are independent. If there occurs exception in one thread, it doesn't affect other threads.
It uses a shared memory area.
➢ Java Multithreading is mostly used in games, animation, etc.
➢ Advantages of Java Multithreading
❖ It doesn't block the user because threads are independent and you can perform multiple
operations at the same time.
❖ You can perform many operations together, so it saves time.
❖ Threads are independent, so it doesn't affect other threads if an exception occurs in a single
thread.
Prof. K. Adisesha
30
Multithreading in Java
Thread Methods:
Thread methods in Java are very important while you're working with a multi-threaded
application. The thread class has some important methods.
➢ public void start(): you use this method to start the thread in a separate path of execution. Then
it invokes the run() method on the thread object.
➢ public void run(): this method is the starting point of the thread. The execution of the thread
begins from this process.
➢ public final void setName(): this method changes the name of the thread object.
➢ public final void setPriority(): you use this method to set the values of the thread object.
➢ public void sleep(): you use this method to suspend the thread for a particular amount of time.
➢ public void interrupt(): you use this method to interrupt a particular thread. It also causes it to
continue execution if it was blocked for any reason.
➢Prof.public
K. Adisesha
final boolean isAlive(): this method returns true if the thread is alive..
32
Multithreading in Java
Thread Priorities:
Every Java thread has a priority that helps the operating system determine the order in
which threads are scheduled.
➢ Java thread priorities are in the range between a constant of 1 -10:
❖ MIN_PRIORITY (a constant of 1).
❖ MAX_PRIORITY (a constant of 10).
❖ By default, every thread is given priority NORM_PRIORITY (a constant of 5).
➢ Threads with higher priority are more important to a program and should be allocated
processor time before lower-priority threads.
➢ However, thread priorities cannot guarantee the order in which threads execute and are
very much platform dependent.
Prof. K. Adisesha
33
Multithreading in Java
Queries ?
Prof. K. Adisesha
9449081542