Interfaces: Multiple Inheritance
Interfaces: Multiple Inheritance
class A extends B extends C { .. } Such type of definitions are not permitted in java
Interfaces
An interface is basically a kind of class with the difference that interfaces define only abstract methods and final fields. Interfaces do not specify any code to implement these methods and data fields contain only constants
Interfaces
It is the responsibility of the class that implements an interface to define the code for implementation of these methods inteface InterfaceName { variables declaration; methods declaration; }
Extending Interfaces
interface ItemConstants { int code=1001; String name=Fan; } Interface ItemMethods { void display( ); } Interface Item extends ItemConstants,ItemMethods { .. }
Interfaces
Subinterfaces are not allowed to define the methods declared in the superinterfaces. Why?
class Hybrid { public static void main(String args[]) { Results student1=new Results(); student1.getNumber(1234); student1.putWt(); } }
Packages
Packages are Javas way of grouping a variety of classes and/or interfaces together. The grouping is usually done according to functionality.
Benefits of Packages
1) The classes contained in the packages of other programs can be easily reused. 2) Classes can be unique 3) Way to hide classes thus preventing other programs or packages from accessing classes that are meant for internal use only 4) Provide a way for separating Design from Coding
Package Classification
Java API packages User defined packages
Java
lang
util
io
awt
net
applet
Using packages
Using import statements import java.util.Vector;
Creating Packages
package com.jiet; class Hello{ public static void main(String args[]) { System.out.println("Hello World"); } }
Threads
In java, Multitasking is achieved using threads Multithreading is programming paradigm where a program(process) is divided into two or more subprograms(processes) Java enables us to use multiple flows of control in developing programs. Each flows of control is a separate tiny program known as a thread that runs in parallel to others
Threads
The ability of a language to support multithreads is referred to as concurrency Threads in java are subprograms of a main program (main method), are known as lightweight threads or lightweight processes Any application that requires two or more things to be done at the same time, is probably a best one for use of threads
Creating Threads
Threads are implemented in the form of objects that contain a method called run( ). run( ) makes up the entire body of a thread
run( ) can be invoked by an object of the concerned thread How to create objects????
Program
class A extends Thread { public void run( ) { for(int i=1;i<=5;i++) SOP(\tFrom Thread A: i=+i); SOP(Exit from A); } } class B extends Thread { public void run( ) { for(int j=1;j<=5;j++) SOP(\tFrom Thread B: j=+j; SOP(Exit from B); } } class ThreadTest { public static void main (String args[]) { new A( ).start( ); new B( ).start( ); } }
Note
Sleep( ) method throws exception so it is mandatory to catch it while using sleep( ) try{
sleep (1000); } catch(Exception e) { }
Thread Priority
Each thread is assigned a priority, which affects the order in which it is scheduled for running. The threads by default are of the same priority and that is 5(NORM_PRIORITY); Java permits us to set the priority of a thread using the setPriority( ) method Threadname.setPriority(intNumber); The intNumber is an integer value to which the threads priority is set. It can be between 1 to 10 However there are some constants available MIN_PRIORITY=1 NORM_PRIORITY=5 MAX_PRIORITY=10
Synchronization
When two or more threads compete for the same resources, it leads to serious problems e.g. one thread may try to read a record from a file while another is still writing to the same file. Java enables us to overcome this problem using a technique known as synchronization The method that will read information from a file and the method that will update the same file may be declared as synchronized. Example: synchronized void update( ) { // code here is synchronized } When we declare a method synchronized, Java creates a monitor and hands it over to the thread that calls the method first time. As long as the thread holds the monitor, no other thread can enter the synchronized section of code.
Deadlock
Thread A synchronized method2( ) { synchronized method1 ( ) { .. } }
Thread B synchronized method1( ) { synchronized method2( ) { } }
Program
Class X implements Runnable //Step1 { public void run( ) //Step2 { .. } } Class RunnableTest { public static void main(String argas[]) { X runable=new X( ); Thread threadx=new Thread(runable); //Step3 threadX.start( ); //Step4 SOP(End of Thread); } }