UNIT-II Chapter-II Interfaces · Herbert Schildt, “Java the Complete Reference,” Oracle Press,...

22
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

Transcript of UNIT-II Chapter-II Interfaces · Herbert Schildt, “Java the Complete Reference,” Oracle Press,...

Page 1: UNIT-II Chapter-II Interfaces · Herbert Schildt, “Java the Complete Reference,” Oracle Press, Ninth Edition, 2014. 1. 2 Interfaces: • The interface in java is a mechanism to

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

Page 2: UNIT-II Chapter-II Interfaces · Herbert Schildt, “Java the Complete Reference,” Oracle Press, Ninth Edition, 2014. 1. 2 Interfaces: • The interface in java is a mechanism to

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.

Page 3: UNIT-II Chapter-II Interfaces · Herbert Schildt, “Java the Complete Reference,” Oracle Press, Ninth Edition, 2014. 1. 2 Interfaces: • The interface in java is a mechanism to

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

Page 4: UNIT-II Chapter-II Interfaces · Herbert Schildt, “Java the Complete Reference,” Oracle Press, Ninth Edition, 2014. 1. 2 Interfaces: • The interface in java is a mechanism to

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.

Page 5: UNIT-II Chapter-II Interfaces · Herbert Schildt, “Java the Complete Reference,” Oracle Press, Ninth Edition, 2014. 1. 2 Interfaces: • The interface in java is a mechanism to

5

Cont..

Understanding relationship between classes and interfaces

Page 6: UNIT-II Chapter-II Interfaces · Herbert Schildt, “Java the Complete Reference,” Oracle Press, Ninth Edition, 2014. 1. 2 Interfaces: • The interface in java is a mechanism to

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

Page 7: UNIT-II Chapter-II Interfaces · Herbert Schildt, “Java the Complete Reference,” Oracle Press, Ninth Edition, 2014. 1. 2 Interfaces: • The interface in java is a mechanism to

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();

} }

Page 8: UNIT-II Chapter-II Interfaces · Herbert Schildt, “Java the Complete Reference,” Oracle Press, Ninth Edition, 2014. 1. 2 Interfaces: • The interface in java is a mechanism to

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

Page 9: UNIT-II Chapter-II Interfaces · Herbert Schildt, “Java the Complete Reference,” Oracle Press, Ninth Edition, 2014. 1. 2 Interfaces: • The interface in java is a mechanism to

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());

}}

Page 10: UNIT-II Chapter-II Interfaces · Herbert Schildt, “Java the Complete Reference,” Oracle Press, Ninth Edition, 2014. 1. 2 Interfaces: • The interface in java is a mechanism to

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

Page 11: UNIT-II Chapter-II Interfaces · Herbert Schildt, “Java the Complete Reference,” Oracle Press, Ninth Edition, 2014. 1. 2 Interfaces: • The interface in java is a mechanism to

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.

Page 12: UNIT-II Chapter-II Interfaces · Herbert Schildt, “Java the Complete Reference,” Oracle Press, Ninth Edition, 2014. 1. 2 Interfaces: • The interface in java is a mechanism to

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

Page 13: UNIT-II Chapter-II Interfaces · Herbert Schildt, “Java the Complete Reference,” Oracle Press, Ninth Edition, 2014. 1. 2 Interfaces: • The interface in java is a mechanism to

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).

Page 14: UNIT-II Chapter-II Interfaces · Herbert Schildt, “Java the Complete Reference,” Oracle Press, Ninth Edition, 2014. 1. 2 Interfaces: • The interface in java is a mechanism to

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

Page 15: UNIT-II Chapter-II Interfaces · Herbert Schildt, “Java the Complete Reference,” Oracle Press, Ninth Edition, 2014. 1. 2 Interfaces: • The interface in java is a mechanism to

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());

}}

Page 16: UNIT-II Chapter-II Interfaces · Herbert Schildt, “Java the Complete Reference,” Oracle Press, Ninth Edition, 2014. 1. 2 Interfaces: • The interface in java is a mechanism to

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

Page 17: UNIT-II Chapter-II Interfaces · Herbert Schildt, “Java the Complete Reference,” Oracle Press, Ninth Edition, 2014. 1. 2 Interfaces: • The interface in java is a mechanism to

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;

}

Page 18: UNIT-II Chapter-II Interfaces · Herbert Schildt, “Java the Complete Reference,” Oracle Press, Ninth Edition, 2014. 1. 2 Interfaces: • The interface in java is a mechanism to

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% } }

Page 19: UNIT-II Chapter-II Interfaces · Herbert Schildt, “Java the Complete Reference,” Oracle Press, Ninth Edition, 2014. 1. 2 Interfaces: • The interface in java is a mechanism to

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;

} }

Page 20: UNIT-II Chapter-II Interfaces · Herbert Schildt, “Java the Complete Reference,” Oracle Press, Ninth Edition, 2014. 1. 2 Interfaces: • The interface in java is a mechanism to

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

Page 21: UNIT-II Chapter-II Interfaces · Herbert Schildt, “Java the Complete Reference,” Oracle Press, Ninth Edition, 2014. 1. 2 Interfaces: • The interface in java is a mechanism to

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

Page 22: UNIT-II Chapter-II Interfaces · Herbert Schildt, “Java the Complete Reference,” Oracle Press, Ninth Edition, 2014. 1. 2 Interfaces: • The interface in java is a mechanism to

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 .