Interfaces,Packages and Threads

Slides:



Advertisements
Similar presentations
1 Packages: Putting Classes Together. 2 Introduction The main feature of OOP is its ability to support the reuse of code: Extending the classes (via inheritance)
Advertisements

INHERITANCE BASICS Reusability is achieved by INHERITANCE
OBJECT-ORIENTED PROGRAMMING CONCEPTS (Review). What is an Object? What is an Object? Objects have states and behaviors. Example: A dog has states - color,
PACKAGES. PACKAGES IN JAVA A package is a collection of related classes and interfaces in Java Packages help in logical grouping of classes and interfaces.
Introduction to Java Programming, 4E
Chapter 3 Using Classes and Objects. 2 Creating Objects  A variable holds either a primitive type or a reference to an object  A class name can be used.
Threading in Java – a Tutorial QMUL IEEE SB. Why Threading When we need to run two tasks concurrently So multiple parts (>=2) of a program can run simultaneously.
Introduction to Java Programming with JBuilder 4
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
LESSON 2 CREATING A JAVA APPLICATION JAVA PROGRAMMING Compiled By: Edwin O. Okech [Tutor, Amoud University]
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Introduction to Java Programming. History F James Gosling and Sun Microsystems F Oak F Java, May 20, 1995, Sun World F HotJava –The first Java-enabled.
1 Module Objective & Outline Module Objective: After completing this Module, you will be able to, appreciate java as a programming language, write java.
CIS 644 Aug. 25, 1999 tour of Java. First … about the media lectures… we are experimenting with the media format please give feedback.
Introduction to Java Programming with Forte Y. Daniel Liang.
CS 3131 Introduction to Programming in Java Rich Maclin Computer Science Department.
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
Java programming Package. A package is a group of similar types of classes, interfaces and sub-packages. Package can be categorized in two form, built-
Methods in Java. Program Modules in Java  Java programs are written by combining new methods and classes with predefined methods in the Java Application.
Object Oriented Programming Examples: C++, Java Advantages: 1. reusibility of code 2. ability to adapt (extend) previously written code.
Introduction to Threads Session 01 Java Simplified / Session 14 / 2 of 28 Objectives Define a thread Define multithreading List benefits of multithreading.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. A class represents a single concept from the problem domain, or a.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
JAVA INTRODUCTION. What is Java? 1. Java is a Pure Object – Oriented language 2. Java is developing by existing languages like C and C++. How Java Differs.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
1 Chapter 6 Programming with Objects and Classes F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive.
Programming in java Packages Access Protection Importing packages Java program structure Interfaces Why interface Defining interface Accessing impln thru.
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
Packages. The main feature of OOP is its ability to support the reuse of code: –Extending the classes –Extending interfaces The features in basic form.
CSI 3125, Preliminaries, page 1 Packages. CSI 3125, Preliminaries, page 2 Packages Packages are containers for classes Collection of classes Packages.
April 20, 1998CS102-02Lecture 4-1 A Method to the Madness CS Lecture 4-1 Java's Work Horses.
SourceAnatomy1 Java Source Anatomy Barb Ericson Georgia Institute of Technology July 2008.
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
Object Oriented Programming Object and Classes Lecture 3 MBY.
Creating Java Applications (Software Development Life Cycle) 1. specify the problem requirements - clarify 2. analyze the problem - Input? Processes? Output.
Computer Science 209 Software Development Packages.
1 Creating Objects  A variable holds either a primitive type or a reference to an object  A class name can be used as a type to declare an object reference.
Introduction to Java Programming, 4E Y. Daniel Liang.
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
Objects and Classes. F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive data type and object type.
“Packages in Java”.
3 Introduction to Classes and Objects.
Java Multithreading.
Inheritance-Basics.
Packages When we name a program , we name it by class name…
Java Basics Packages.
Interfaces.
Interface.
Java Programming Language
An Introduction to Java – Part I, language basics
Packages and Interfaces
The Building Blocks Classes: Java class library, over 1,800 classes:
Interfaces.
Week 3 Object-based Programming: Classes and Objects
Sampath Kumar S Assistant Professor, SECE
Object Oriented Programming in java
Sampath Kumar S Assistant Professor, SECE
Inheritance Inheritance is a fundamental Object Oriented concept
Applying OO Concepts Using Java
Constructors, GUI’s(Using Swing) and ActionListner
PACKAGES.
Lecture Notes - Week 2 Lecture-1. Lecture Notes - Week 2 Lecture-1.
Multithreading in java.
MULTITHREADING PROGRAMMING
Chapter 6 Objects and Classes
Chap 1. Getting Started Objectives
Chap 4. Programming Fundamentals
មជ្ឈមណ្ឌលកូរ៉េ សហ្វវែរ អេច អ ឌី
Chapter 7 Objects and Classes
INTERFACES Explained By: Sarbjit Kaur. Lecturer, Department of Computer Application, PGG.C.G., Sector: 42, Chandigarh.
Presentation transcript:

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

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.

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.

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();

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.

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

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

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..,

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.

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

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

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.

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

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)

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

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

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

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