unit-ii chapter-ii interfaces · herbert schildt, “java the complete reference,” oracle press,...
TRANSCRIPT
UNIT-II
Chapter-II
Interfaces:
Definition
Implementing interfaces
Nested interfaces
Applying interfaces
Variables in interface and
Extending interfaces.
Herbert Schildt, “Java the Complete Reference,” Oracle Press, Ninth Edition, 2014.1
2
Interfaces:
• The interface in java is a mechanism to achieve abstraction.
• An interface in java is a blueprint of a class. It has static
constants and abstract methods.
• There can be only abstract methods in the java interface not
method body. It is used to achieve abstraction and multiple
inheritance in Java.
• Java Interface also represents IS-A relationship.
• It cannot be instantiated just like abstract class.
• By interface, we can support the functionality of multiple
inheritance and it can be used to achieve loose coupling.
3
Cont..
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;
}
Defining an Interface
4
Cont..
Note:• The java compiler adds public and abstract keywords before the
interface method.• More, it adds public, static and final keywords before data
members.
5
Cont..
Understanding relationship between classes and interfaces
UNIT-II
Chapter-II
Interfaces:
Definition
Implementing interfaces
Nested interfaces
Applying interfaces
Variables in interface and
Extending interfaces.
Herbert Schildt, “Java the Complete Reference,” Oracle Press, Ninth Edition, 2014.6
7
Implementing interfaces
class classname [extends superclass] [implements interface [, interface…]]
{
//class-body
}
interface printable{
void print(); }
Interface Example
class A implements printable{
public void print(){ System.out.println("Hello");}
public static void main(String args[]){
A obj = new A();
obj.print();
} }
8
Scenario: 1
Shape is the Interface, its implementation is provided by the
Rectangle and Circle classes. Mostly, we don't know about the
implementation class (i.e. hidden to the end user) and object
of the implementation class is provided by the factory
method. draw()
Scenario: 2
Bank is the Interface, its implementation is provided by the
ICICI and SBI classes. Return of interest.
Cont..
Accessing Implementations Through Interface References
9
Cont..interface Bank{
float rateOfInterest();
}
class SBI implements Bank{
public float rateOfInterest(){return 9.15f;}
}
class PNB implements Bank{
public float rateOfInterest(){return 9.7f;}
}
class TestInterface2{
public static void main(String[] args){
Bank b=new SBI();
System.out.println("ROI: "+b.rateOfInterest());
}}
UNIT-II
Chapter-II
Interfaces:
Definition
Implementing interfaces
Nested interfaces
Applying interfaces
Variables in interface and
Extending interfaces.
Herbert Schildt, “Java the Complete Reference,” Oracle Press, Ninth Edition, 2014.10
11
Nested Interfaces
An interface can be declared a member of a class or another
interface. Such an interface is called a member interface
or a nested interface.
A nested interface can be declared as public, private, or
protected.
This differs from a top-level interface, which must either be
declared as public or use the default access level.
When a nested interface is used outside of its enclosing
scope, it must be qualified by the name of the class or
interface of which it is a member. Thus, outside of the class or
interface in which a nested interface is declared, its name
must be fully qualified.
12
Cont..
Example:interface Showable{
void show();interface Message{void msg();}
}class TestNestedInterface1 implements Showable.Message{public void msg(){System.out.println("Hello nested interface");}
public static void main(String args[]){Showable.Message message=new TestNestedInterface1();
//upcasting heremessage.msg();
}}
Output: hello nested interface
13
Cont..
Program:
Create a class called FixedStack that implements a fixed-
length version of an integer stack (interface that defines an
integer stack). This interface will be used by both stack
implementations.
Page 201 and 202 (TB).
UNIT-II
Chapter-II
Interfaces:
Definition
Implementing interfaces
Nested interfaces
Applying interfaces
Variables in interface and
Extending interfaces.
Herbert Schildt, “Java the Complete Reference,” Oracle Press, Ninth Edition, 2014.14
15
Cont..interface Bank{
float rateOfInterest();
}
class SBI implements Bank{
public float rateOfInterest(){return 9.15f;}
}
class PNB implements Bank{
public float rateOfInterest(){return 9.7f;}
}
class TestInterface2{
public static void main(String[] args){
Bank b=new SBI();
System.out.println("ROI: "+b.rateOfInterest());
}}
UNIT-II
Chapter-II
Interfaces:
Definition
Implementing interfaces
Nested interfaces
Applying interfaces
Variables in interface and
Extending interfaces.
Herbert Schildt, “Java the Complete Reference,” Oracle Press, Ninth Edition, 2014.16
17
Cont..import java.util.Random;
interface SharedConstants
{
int NO = 0;
int YES = 1;
int MAYBE = 2;
int LATER = 3;
int SOON = 4;
int NEVER = 5;
}
18
Cont..class Question implements SharedConstants {
Random rand = new Random();
int ask() {
int prob = (int) (100 * rand.nextDouble());
if (prob < 30)
return NO; // 30%
else if (prob < 60)
return YES; // 30%
else if (prob < 75)
return LATER; // 15%
else if (prob < 98)
return SOON; // 13%
else return NEVER; // 2% } }
19
Cont..
class AskMe implements SharedConstants {
static void answer(int result) {
switch(result) {
case NO: System.out.println("No"); break;
case YES: System.out.println("Yes"); break;
case MAYBE: System.out.println("Maybe"); break;
case LATER: System.out.println("Later"); break;
case SOON: System.out.println("Soon"); break;
case NEVER: System.out.println("Never"); break;
} }
20
Cont..
public static void main(String args[]) {
Question q = new Question();
answer(q.ask());
answer(q.ask());
answer(q.ask());
answer(q.ask());
} }
Output:
Later
Soon
No
Yes
UNIT-II
Chapter-II
Interfaces:
Definition
Implementing interfaces
Nested interfaces
Applying interfaces
Variables in interface and
Extending interfaces.
Herbert Schildt, “Java the Complete Reference,” Oracle Press, Ninth Edition, 2014.21
22
interface Printable{
void print();
}
interface Showable extends Printable{
void show();
}
class TestInterface4 implements Showable{
public void print(){ System.out.println("Hello");}
public void show(){ System.out.println("Welcome");}
public static void main(String args[]){
TestInterface4 obj = new TestInterface4();
obj.print();
obj.show();
} }
Cont..
Output:
Hello
Welcome
A class implements interface but one interface extends another
interface .