Top Banner

of 30

Java Third 30

Apr 03, 2018

Download

Documents

Sahil Ahuja
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
  • 7/28/2019 Java Third 30

    1/30

    Interfaces: Multiple Inheritance

    class A extends B extends C

    {

    ..

    }

    Such type of definitions are not permitted injava

  • 7/28/2019 Java Third 30

    2/30

  • 7/28/2019 Java Third 30

    3/30

    Interfaces

    It is the responsibility of the class thatimplements an interface to define the code

    for implementation of these methods intefaceInterfaceName

    {

    variables declaration;methods declaration;

    }

  • 7/28/2019 Java Third 30

    4/30

    Extending Interfaces

    interface ItemConstants

    {

    int code=1001;

    String name=Fan;

    }

    Interface ItemMethods

    {

    void display( );

    }

    Interface Item extends ItemConstants,ItemMethods{

    ..

    }

  • 7/28/2019 Java Third 30

    5/30

    Interfaces

    Subinterfaces are not allowed to define the

    methods declared in the superinterfaces.

    Why?

  • 7/28/2019 Java Third 30

    6/30

    Class Implementing Interfacesinterface Area

    {

    final static float pi=3.14;float compute ( float x, float y);

    }

    Class Rectangle implements Area

    {

    public float compute(float x, float y)

    {

    return (x* y);}

    }

    Class InterfaceTest

    {

    public static void main(String args[]){

    Rectangle rect=new Rectangle( );

    Area area;

    area=rect;

    System.out.println(Area of rectangle = + area.compute(10,20));

    }

    }

  • 7/28/2019 Java Third 30

    7/30

    Implementing Multiple Inheritanceclass Student

    {

    int rollNumber;

    void getNumber( int n)

    {

    rollNumber=n;

    }

    void putNumber( ){

    System.out.println("Roll No :" + rollNumber);

    }

    }

    interface Sports

    {

    float sportWt=6.0F;

    void putWt( );

    }

    class Results extends Student implements Sports

    {

    float total;

    public void putWt( )

    {

    System.out.println("sport wt = "+ sportWt);

    }

    }

    class Hybrid

    {

    public static void main(String args[])

    {

    Results student1=new Results();

    student1.getNumber(1234);

    student1.putWt();

    }

    }

  • 7/28/2019 Java Third 30

    8/30

    Packages

    Packages are Javas way of grouping a

    variety of classes and/or interfaces together.

    The grouping is usually done according tofunctionality.

  • 7/28/2019 Java Third 30

    9/30

    Benefits of Packages

    1) The classes contained in the packages ofother programs can be easily reused.

    2) Classes can be unique3) Way to hide classes thus preventing other

    programs or packages from accessing

    classes that are meant for internal use only4) Provide a way for separating Design fromCoding

  • 7/28/2019 Java Third 30

    10/30

    Package Classification

    Java API packages

    User defined packages

    Java

    lang util io awt net applet

  • 7/28/2019 Java Third 30

    11/30

    Using packages

    Using import statements

    import java.util.Vector;

  • 7/28/2019 Java Third 30

    12/30

    Creating Packages

    package com.jiet;

    class Hello{

    public static void main(String args[])

    {

    System.out.println("Hello World");}

    }

  • 7/28/2019 Java Third 30

    13/30

    Compiling & Executing

    Javacd . Hello.java

    Java com.jiet.Hello

  • 7/28/2019 Java Third 30

    14/30

    Comparison (Java & C++)

    Java has both kinds of comments like C++ does

    All method definitions are defined in the body of the class.Thus, as in C++ it would look like all the functions are

    inlined, but theyre not Class definitions are roughly the same form in Java as in

    C++, but theres no closing semicolon

    Theres no scope resolution operator :: in Java. Java uses

    the dot for everything, but can get away with it since youcan define elements only within a class. Even the methoddefinitions must always occur within a class, so there is noneed for scope resolution there either.

  • 7/28/2019 Java Third 30

    15/30

    Comparison (Java & C++)

    All objects of non-primitive types can be created only via new

    Java does not support default arguments

    You cannot specify public, private, orprotected inheritance in Java,as you can in C++

    overridden methods in a derived class cannot reduce the access of themethod in the base class. For example, if a method is public in the

    base class and you override it, your overridden method must also bepublic (the compiler checks for this)

    Java use dynamic binding for non-static methods and static binding forstatic methods automatically. No Virtual exists in Java

  • 7/28/2019 Java Third 30

    16/30

    Threads

    In java, Multitasking is achieved using threads

    Multithreading is programming paradigm where a

    program(process) is divided into two or moresubprograms(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

  • 7/28/2019 Java Third 30

    17/30

    Threads

    The ability of a language to supportmultithreads is referred to as concurrency

    Threads in java are subprograms of a mainprogram (main method), are known aslightweight threads or lightweight processes

    Any application that requires two or morethings to be done at the same time, isprobably a best one for use of threads

  • 7/28/2019 Java Third 30

    18/30

    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????

  • 7/28/2019 Java Third 30

    19/30

    Two ways to create a thread

    By creating a thread class: Define a class

    that extends Thread class and override the

    run( ) method with the code required by thethread

    By converting a class to a thread: Define a

    class that implements Runnable interface.The Runnable interface has only one

    method, run( )

  • 7/28/2019 Java Third 30

    20/30

    Extending the Thread class

    Declare the class as extending the Thread

    class

    Implement the run( ) method that isresponsible for executing the sequence of

    code that the thread will execute

    Create a thread object and call the start( )method to initiate the thread execution

    P

  • 7/28/2019 Java Third 30

    21/30

    Programclass A extends Thread

    {

    public void run( )

    {

    for(int i=1;i

  • 7/28/2019 Java Third 30

    22/30

    Stopping and Blocking a thread

    aThread.stop( ); : causes thread to moveinto a dead state

    sleep() // blocked for a specified time

    suspend( ) // blocked until further orders

    wait( ) // blocked until certain condition

    occurs Resume( ) is used in case of suspend( )

    Notify( ) is used in case of wait( )

  • 7/28/2019 Java Third 30

    23/30

    Life Cycle of a Thread

    During the life time of a thread, it enters

    these states

    Newborn state

    Runnable state

    Running state

    Blocked state

    Dead satate

  • 7/28/2019 Java Third 30

    24/30

    Note

    Sleep( ) method throws exception so it is

    mandatory to catch it while using sleep( )

    try{

    sleep (1000);

    }catch(Exception e)

    { }

  • 7/28/2019 Java Third 30

    25/30

    Thread Priority

    Each thread is assigned a priority, which affects the orderin which it is scheduled for running. The threads by defaultare of the same priority and that is 5(NORM_PRIORITY);

    Java permits us to set the priority of a thread using thesetPriority( ) method

    Threadname.setPriority(intNumber);

    The intNumber is an integer value to which the threadspriority is set. It can be between 1 to 10

    However there are some constants available MIN_PRIORITY=1

    NORM_PRIORITY=5

    MAX_PRIORITY=10

  • 7/28/2019 Java Third 30

    26/30

    Setting Priority- Example

    Class ThreadPriority

    {

    public static void main(String args[])

    {A threadA=new A( );

    B threadB=new B( );

    C threadC=new C( );

    threadC.setPriority(Thread.MAX_PRIORITY);threadB.setPriority(threadA.getPriorty( )+1);

    }

    }

  • 7/28/2019 Java Third 30

    27/30

    Synchronization

    When two or more threads compete for the same resources, it leads to seriousproblems

    e.g. one thread may try to read a record from a file while another is stillwriting to the same file.

    Java enables us to overcome this problem using a technique known assynchronization

    The method that will read information from a file and the method that willupdate 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 holdsthe monitor, no other thread can enter the synchronized section of code.

    Deadlock

  • 7/28/2019 Java Third 30

    28/30

    DeadlockThread A

    synchronized method2( )

    {

    synchronized method1 ( ){

    ..

    }

    }

    Thread B

    synchronized method1( )

    {

    synchronized method2( )

    {

    }

    }

  • 7/28/2019 Java Third 30

    29/30

    Implementing the Runnable

    Interface The runnable interface declares the run( ) method

    that is required for implementing threads.

    Declare the class as implementing the Runnableinterafce

    Implement the run( ) method

    Create a thread by defining an object that is

    instantiated from this runnable class as the targetof the thread

    Call the threads start( ) method to run the thread

    P

  • 7/28/2019 Java Third 30

    30/30

    ProgramClass 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); }

    }