Presentation is loading. Please wait.

Presentation is loading. Please wait.

Packages, Interfaces & Exception Handling

Similar presentations


Presentation on theme: "Packages, Interfaces & Exception Handling"— Presentation transcript:

1 Packages, Interfaces & Exception Handling
Unit II-Chapter No. : 4-Interfaces Packages, Interfaces & Exception Handling

2 School of Computer Science & Engineering
Content Explain packages, Interfaces and Exception handling mechanism of java Write java program to create code library using packages Write a java program to manage run time errors using java exception handling mechanism. Write a java program to implement interfaces for a given problem specification. School of Computer Science & Engineering

3 School of Computer Science & Engineering
Content Packages: Define, create, access protection and importing packages Interfaces: Define, create interface and implement Method overriding Exceptions: Define, Exception handling fundamentals, classification: Exception Types. Using throw, throws, finally keywords to manage exceptions. User Defined Exceptions School of Computer Science & Engineering

4 School of Computer Science & Engineering
Interfaces: Basics Background: abstract classes The “interface” in java supports to fully abstract the class from its implementation (where as abstract class consists of concrete methods) Using interface we can specify what a class must do , but not how it does it. Example: Suppose : BankInterface { void deposit(double amt); // what, not how} class BankAccount implements BankInterface{ double balance; void deposit(double amt){balance+=amt;} // how to deposit School of Computer Science & Engineering

5 Interfaces: Definition
Define: Interfaces are syntactically similar to classes but they lack instance variables and their methods are declared without any body. School of Computer Science & Engineering

6 School of Computer Science & Engineering
Interfaces: Basics One interface can be implemented by any number of classes. One class can implement any number of interfaces. School of Computer Science & Engineering

7 School of Computer Science & Engineering
Interfaces: Basics Few conditions on classes which implements interface To implement an interface , a class must create the complete set of methods defined by the interface Each class is free to determine the details of its own implementation. Interfaces are designed to support dynamic method resolution at run time. School of Computer Science & Engineering

8 Interfaces: Define/ General syntax
access interface name{ return-type method-name1(parameter-list); return-type method-name2(parameter-list); type final-varname1 = value; type final-varname2 = value; //…. return-type method-nameN(parameter-list); type final-varnameN = value; } Note: all methods are abstract in interface All interface variables are final and static School of Computer Science & Engineering

9 School of Computer Science & Engineering
Interface: Example Example create interface interface CallBack{ void callBack(int param); } Implementing interface General syntax class ClassName [extends superclass][implements interface [,interface]] { // class-body } School of Computer Science & Engineering

10 Interface: create and implement
Example School of Computer Science & Engineering

11 Interface: create and implement
It is both permissible and common for classes that implement interface to define additional members of their own School of Computer Science & Engineering

12 Interfaces: Polymorphic Power
School of Computer Science & Engineering

13 Interfaces: partial implementation
If a class includes an interface but does not fully implement the methods defined by that interface, that class must be declared as abstract. School of Computer Science & Engineering

14 School of Computer Science & Engineering
Interfaces: Nested An interface can be declared a member of a class or another interface, such interface is called a member interface or a nested interface. A nested interface can be declared as public, private, or protected. When a nested interface is used outside of its enclosing , it must be qualified by the class or interface of which it is a member. School of Computer Science & Engineering

15 Nested Interfaces: Example
School of Computer Science & Engineering

16 School of Computer Science & Engineering
Interface: Variables Apart from having methods declared in interfaces, it can also have “variables”. The variables declared in interface are by default “final and static” and they can be shared by multiple classes Example School of Computer Science & Engineering

17 School of Computer Science & Engineering
Interface:Variables Example : School of Computer Science & Engineering

18 School of Computer Science & Engineering
Extending Interface Example : School of Computer Science & Engineering

19 Interface: Multiple Inheritance
An interface can extend more than interface School of Computer Science & Engineering

20 School of Computer Science & Engineering
Interfaces Caution: Because dynamic lookup of a method at run time incurs a significant overhead when compared with normal method invocation in java, we should be careful not to use interfaces casually in performance-critical code. School of Computer Science & Engineering

21 School of Computer Science & Engineering
Exercises Consider customer can have multiple accounts like SavingsAccount and ProvidentFundAccount, the customer can withdraw and deposit amount to savings account but not with provident fund account (PF account), however he can transfer amount from saving account to PF account. Write a test program to simulate the above scenario. School of Computer Science & Engineering


Download ppt "Packages, Interfaces & Exception Handling"

Similar presentations


Ads by Google