4

504 views
446 views

Published on

Hariprasanna V (9843824677)

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
504
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
18
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • hi dfhrehfdd crf
  • 4

    1. 1. Day 6 <ul><li>Interfaces </li></ul><ul><li>Exception Handling </li></ul>
    2. 2. 4 Interfaces <ul><li>An interface is basically a kind of class. Like classes, interfaces contains methods and variables but with a major difference. The difference is that interfaces defines only abstract methods and final fields. This means that interfaces do not specify any code to implement these methods and data fields contains only constants. It is the responsibility of the class that implements an interface to define the code for implementation of these methods. </li></ul><ul><li>interface Interface Name </li></ul><ul><li>{ </li></ul><ul><li>variable declaration ; </li></ul><ul><li>methods declaration; </li></ul><ul><li>} </li></ul>
    3. 3. <ul><li>Here is an example of an interface definition that contains two variables and one method. </li></ul><ul><li>interface Item </li></ul><ul><li> { </li></ul><ul><li>static final int code = 1001; </li></ul><ul><li>static final String name = “fan”; </li></ul><ul><li> } </li></ul><ul><li>Extending Interfaces :Like classes interfaces can also be extended. An interface can be subinterfaced from other interfaces. The new subinterface will inherit all the methods of the superinterface in a manner similar to subclass. This can be achieved using the keyword extend. </li></ul><ul><li>interface name2 extends name1 </li></ul><ul><li>{ </li></ul><ul><li>body of name2 </li></ul><ul><li>} </li></ul>
    4. 4. <ul><li>For example we can put all the constants in one interface and all the method in the other interface. This will enable us to use the constants in classes where the </li></ul><ul><li>methods are not required. </li></ul><ul><li>interface ItemConstants </li></ul><ul><li>{ </li></ul><ul><li>int code = 1001; </li></ul><ul><li>string name = “Fan”; </li></ul><ul><li>} </li></ul><ul><li>interface Item extends ItemConstants </li></ul><ul><li>{ </li></ul><ul><li>void display(); </li></ul><ul><li>} </li></ul>
    5. 5. <ul><li>Implementing Interfaces : Interfaces are used as “superclasses” whose properties are inherited by classes. It is therefore necessary to create a class that inherits the given interface. </li></ul><ul><li>Here the classname “implements” the interface interfacename. The general form of the implementing interface is as follows: </li></ul><ul><ul><li>class classname extends superclass </li></ul></ul><ul><ul><li>implements unterface1, interface2, …….. </li></ul></ul><ul><ul><li>{ </li></ul></ul><ul><ul><li>body of classname </li></ul></ul><ul><ul><li>} </li></ul></ul>
    6. 6. 5 Exception Handling <ul><li>A Java exception is an object that describes an exceptional (error) condition that has occurred in a piece of code. When an exceptional condition arises, an object representing that exception is created and thrown in the method that caused the error. The exception is caught and processed. </li></ul><ul><li>Exception can be generated by Java’s run-time system, or they can be Manually generated by your code. </li></ul><ul><li>Java exception handling is managed via five keywords : try, catch, throw, throws and finally. </li></ul>
    7. 7. <ul><li>The general form pf exception handling is: </li></ul><ul><li>try{ </li></ul><ul><li> //block of code to monitor the errors </li></ul><ul><li>} </li></ul><ul><li>catch(Exception Type1 exOb){ </li></ul><ul><li> // exception handler for Exception Type1 </li></ul><ul><li>} </li></ul><ul><li>catch(Exception Type2 exOb){ </li></ul><ul><li> // exception handler for Exception Type2 </li></ul><ul><li>} </li></ul><ul><li>//………. </li></ul><ul><li>catch(Exception TypeN exOb){ </li></ul><ul><li> // exception handler for Exception TypeN </li></ul><ul><li>} </li></ul>
    8. 8. <ul><li>Exceptions Types: All exception types are built in class Throwable. Thus, Throwable are two subclasses that partition exception into two distinct branches. One is handled by Exception and the other is handles by Error. </li></ul><ul><li>Uncaught Exception </li></ul><ul><li>Using try and Catch </li></ul><ul><li>Multiple Catch clause </li></ul><ul><li>Nested try statements </li></ul><ul><li>throw </li></ul><ul><li>throws </li></ul><ul><li>finally </li></ul>
    9. 9. Sample Code Exception: class Exc2 { public static void main(String args[]) { int d, a; try { // monitor a block of code. d = 0; a = 42 / d; System.out.println(&quot;This will not be printed.&quot;); } catch (ArithmeticException e) { // catch divide-by- zero error System.out.println(&quot;Division by zero.&quot;); } System.out.println(&quot;After catch statement.&quot;); } }
    10. 10. Interface: interface Callback { void callback(int param); } class Client implements Callback { // Implement Callback's interface public void callback(int p) { System.out.println(&quot;callback called with &quot; + p); } void nonIfaceMeth() { System.out.println(&quot;Classes that implement interfaces &quot; +&quot;may also define other members, too.&quot;); } }
    11. 11. class AnotherClient implements Callback { // Implement Callback's interface public void callback(int p) { System.out.println(&quot;Another version of callback&quot;); System.out.println(&quot;p squared is &quot; + (p*p)); } }

    ×