Presentation is loading. Please wait.

Presentation is loading. Please wait.

Interfaces,Packages and Threads

Similar presentations


Presentation on theme: "Interfaces,Packages and Threads"— Presentation transcript:

1 Interfaces,Packages and Threads
UNIT-III Interfaces,Packages and Threads

2 INTERFACE An interface is a reference type in Java.
It is similar to class. It is a collection of abstract methods. A class implements an interface, thereby inheriting the abstract methods of the interface. An interface is similar to a class in the following ways − An interface can contain any number of methods. An interface is written in a file with a .java extension, with the name of the interface matching the name of the file. The byte code of an interface appears in a .class file. Interfaces appear in packages, and their corresponding byte code file must be in a directory structure that matches the package name.

3 An interface is different from a class in several ways, including −
You cannot instantiate an interface. An interface does not contain any constructors. All of the methods in an interface are abstract. An interface cannot contain instance fields. The only fields that can appear in an interface must be declared both static and final. An interface is not extended by a class; it is implemented by a class. An interface can extend multiple interfaces.

4 The interface keyword is used to declare an interface.
Declaring Interfaces The interface keyword is used to declare an interface. Following is an example of an interface − Syntax of interface:- import java.lang.*; public interface NameOfInterface { //Any number of final, static fields // Any number of abstract method declarations } Example /* File name : Animal.java */ public interface Animal public void eat(); public void travel();

5 Interfaces have the following properties −
An interface is implicitly abstract. You do not need to use the abstract keyword while declaring an interface. Each method in an interface is also implicitly abstract, so the abstract keyword is not needed. Methods in an interface are implicitly public.

6 Output Mammal eats Mammal travels Example
/* File name : Mammal.java */ public class Mammal implements Animal { public void eat() System.out.println("Mammal eats"); } public void travel() System.out.println("Mammal travels"); public static void main(String args[]) Mammal m = new Mammal(); m.eat(); m.travel(); Output Mammal eats Mammal travels

7 PACKAGES -It is a group of classes and interfaces.
It can be classified into 2 types System Packages User defined packages 1.Java.lang –language packages This packages contains language String, Math, Thread, Object

8 2.Java.util –utilities packages It supports utility classes(random,date,vector,datastrucure etc., 3.Java.awt.package –GUI packages This package contains classes which are used for implementing graphical interface in java.(Font,Layoutmanager,Graphical classes such as Button,Textfield and Textarea,checkbox etc., 4.Java.io –Input output packages This packages contains classes which supports Input ,output of data. It includes classes like InputStream,OutputStream etc., 5.Java.net –Networking packages This packages contains classes which are used for networking.It includes classes like URL,Internet Address, Socket etc..,

9 6.Java.applet –Applet package
This package contains classes for creating & implementing applets. It includes packaging applet. 7.Java.sql –Database package This package contains classes for implementing database connectivity.

10 T h e s e p a c k a g e s c a n b e d e f i n e d b y u s e r
C r e a t i n g p a c k a g e : - - w e c a n a s s i g n t h e c l a s s e s a n d i n t e r f a c e s i n a s o u r c e f i l e t o a p a r t i c u l a r p a c k a g e b y u s i n g p a c k a g e s t a t e m e n t . S y n t a x : - P a c k a g e p a c k a g e n a m e ; H e r e p a c k a g e i s a k e y w o r d P a c k a g e n a m e i s n a m e o f t h e p a c k a g e USER DEFINED PACKAGES

11 USER DEFINED PACKAGE These packages are defined by the user. Creating Package:- We can assign the classes and interfaces in source file to a particular package by using package statement. Syntax:- Package packagename; -Here package is a keyword -Packagename is the name of the class

12 Creating our own package:
1)Declare the package at the top of the java sourcefile. Package packagename; 2)Declare the class as public Package class classname; 3)Create as directory and Name the same name as the package. 4)The file should be named with the same name as their public class followed by the extension as.Java 5)Compile the source file which creates the.classfile in the directory. 6)Source file and classfile should be in a directory as specified in the class path environment variable.

13 Example: package x; Import java. io
Example: package x; Import java.io.*; public class hello { public void display() System.out.println(“welcome”); } import x.*; class pack Public static void main(stringargs[]) hello h1=new hello(); h1.display(); output: welcome

14 THREADS It is a sequence of instructions that is executed to define a unique flow of control. it is the smallest unit of code MULTITASKING It is same as multithreading. Multitasking in java is a process of executing multiple tasks simultaneously. Multithreading and Multiprocessing both are used to achieve multitasking. In multitasking we have two methods. Process based Multitasking(Multiprogramming) Thread based Multitasking(multithreading)

15 P r o c e s s b a s e d M u l t i t a s k i n g ( M u l t i p r o g r a m m i n g )
E a c h p r o c e s s h a s a n a d d r e s s i n m e m o r y . A p r o c e s s i s a h e a v y w e i g h t . C o s t o f c o m m u n i c a t i o n b e t w e e n t h e p r o c e s s i s h i g h . T h r e a d b a s e d M u l t i t a s k i n g ( m u l t i t h r e a d i n g ) T h r e a d s s h a r e t h e s a m e a d d r e s s s p a c e A t h r e a d i s l i g h t w e i g h t . C o s t o f c o m m u n i c a t i o n b e t w e e n t h e t h r e a d i s l o w

16 SINGLE THREADED PROGRAM
S i n g l e t h r e a d u s e d t o p e r f o r m o n l y o n e t a s k . A t h r e a d i s a l i g h t w e i g h t s u b - p r o c e s s , t h e s m a l l e s t u n i t o f p r o c e s s i n g . c l a s s A B C { … . public void run(..) .. } SINGLE THREADED PROGRAM

17 MULTITHREADING PROGRAMMING
M u l t i t h r e a d i n g i n j a v a i s a p r o c e s s o f e x e c u t i n g m u l t i p l e t h r e a d s s i m u l t a n e o u s l y . I t s a l s o c a l l e d a s m u l t i t a s k i n g . MULTITHREADING PROGRAMMING

18 A MULTITHREADED PROGRAM
Main Thread start start start Thread A Thread B Thread C Threads may switch or exchange data/results *


Download ppt "Interfaces,Packages and Threads"

Similar presentations


Ads by Google