1 / 18

Interfaces,Packages and Threads

Interfaces,Packages and Threads. UNIT-III. 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.

egarrick
Download Presentation

Interfaces,Packages and Threads

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Interfaces,Packages and Threads UNIT-III

  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. 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. 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 System 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. USER DEFINED PACKAGES These packages can be defined by user • Creating package:- - we can assign the classes and interfaces in a source file to a particular package by using package statement. Syntax:- Package packagename; Here package is a keyword Packagename is name of the package

  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.*; 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. Process based Multitasking(Multiprogramming) • Each process has an address in memory . • A process is a heavyweight. • Cost of communication between the process is high. Thread based Multitasking(multithreading) • Threads share the same address space • A thread is lightweight. • Cost of communication between the thread is low

  16. SINGLE THREADED PROGRAM Single thread used to perform only one task. A thread is a lightweight sub-process, the smallest unit of processing. class ABC { …. public void run(..) { … .. } }

  17. MULTITHREADING PROGRAMMING Multithreading in java is a process of executing multiple threads simultaneously. Its also called as multitasking.

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

More Related