0% found this document useful (0 votes)
16 views21 pages

4 Interface

OOP Interface

Uploaded by

hasibshahriar04
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views21 pages

4 Interface

OOP Interface

Uploaded by

hasibshahriar04
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

INTERFACES

Type text here


Interface’s Basics
 In English, an interface is a device or system
that unrelated entities use to interact.
 A remote control is an interface between you
and a television set. The English language is
an interface between two people.
 Similarly, a Java interface is a device that
unrelated objects use to interact with one
another.
 A Java interface defines a set of methods but
does not implement them.
 Using interface, you can specify what a class
must do, but not how it does it.
 Interfaces are similar to class but
◼ Lack of instance variables, can contain only
constant which implicitly final and static.
◼ Their methods are declared without any body.
 After define, any number of classes can
implement it.
 One class can implement any number of
interfaces.
 To implement an interface, a class must create a
complete set of methods defined by the interface.
(That is, must override in other words)
 “interface” keyword is used.
 Using interface, java implements “one interface,
multiple methods” aspect of polymorphism.
 General form of interface:
Access interface interface_name{
return_type method_name1(parameter_list);
return_type method_name2(parameter_list);
type final_variable1 = value;
type final_variable2 = value;
//....
return_type method_nameN(parameter_list);
type final_variableN = value;
}
 access is either public or not used.
 When no access specifier is included, then the
interface is only available to other member of
the package. That is Package Private
 All methods and variables are implicitly
public if the interface is declared as public.
 Example:

interface Callback{
void meth(int param);
}
Implementing Interface
 One or more classes can implement the interface.
 The general form of a class that includes the
“implements”:
access class classname [extends superclass]
[implements interface_name [,interface_name....]]
{
//class body
}
 access is either public or not used.
 If a class implements two interface the interfaces is
separated by comma.
 The methods that implement an interface must be
declared public.
 The type signature of the implementing method
must exactly the the type signature specified in the
interface definition.
 Example:
class Client implements Callback {
// Implement Callback's interface
public void meth(int p) {
System.out.println("callback called with " + p);
}
}
 The class that implements interface have also
some own methods.
 Example:
class Client implements Callback {
// Implement Callback's interface
public void meth(int p) {
System.out.println("callback called with " + p);
}

void nonIfaceMeth() {
System.out.println("Classes that implement interfaces " +
"may also define other members, too.");
}
}
Accessing through interface
Reference
class AnotherClient implements Callback {
// Implement Callback's interface callback called with 42
public void meth(int p) { Another version of callback
System.out.println("Another version of
p squared is 1764
callback");
System.out.println("p squared is " + (p*p));
}
}
Class Box {…..}
c=c1;
class TestIface2 {
c.meth(42);
public static void main(String args[]) {
c. nonIfaceMeth();
Callback c; Can't write, because reference variable is of Client class (interface),
so it cannot access a method of Client class
c = ob; // c now refers to
Client c1=new Client(); AnotherClient object
AnotherClient ob = new AnotherClient(); c.meth(42);
}
}
Interface and Dynamic method
dispatch
Differences between abstract class
and interfaces
Feature Interface Abstract class
Multiple A class may implement A class may extend only one
inheritance several interfaces. abstract class.

Default An interface cannot provide An abstract class can


implementa any code at all, much less provide complete code,
tion default code. default code, abstract
methods that have to be
overridden.

Constants Static final constants only. Both instance and static


constants are possible.

Relationship Interfaces are often used to An abstract class defines the


describe the peripheral core identity of its
abilities of a class which descendants.
could apply to totally
unrelated objects.
In easier words, connects unrelated objects In easier words, connects related objects
What Can I Use Interfaces For?
 Interfaces are useful for the following:
◼ Capturing similarities between unrelated classes
without artificially forcing a class relationship.
◼ Declaring methods that one or more classes are
expected to implement.
Interface Example

interface IntMinMax{
int _min();
int _max();
}
Save: IntMinMax.java
IntClass1.java
class ArrayClass implements IntMinMax{
int a[]=new int[10];
ArrayClass(int b[]) public int _max()
{ {
a=b; int c,i;
} c=a[0];
public int _min()
{ for(i=1;i<a.length;i++)
int c,i; {
c=a[0];
for(i=1;i<a.length;i++) if(a[i]>c)
{
c=a[i];
if(a[i]<c)
}
c=a[i];
return c;
}
}
return c;
}
}
class IntClass1{
public static void main(String args[])
{
int arr[]={50,8,9,1,2,7,10,6};
ArrayClass ob1=new ArrayClass(arr);
System.out.println(ob1._min());
System.out.println(ob1._max());

1
} 50

}
IntClass2.java
class IntNum implements IntMinMax{ public int _max()
int a,b,c; {
IntNum(int i,int j,int k) if(a>b)
{ {
a=i; if(a>c) return a;
b=j; else return c;
c=k; }
} else
public int _min() {
{ if(b>c) return b;
else return c;
if(a<b)
}
{
}
if(a<c) return a;
}
else return c;
}
else
{
if(b<c) return b;
else return c;
}
}
class Intclass2{
public static void main(String args[])
{
IntNum ob1= new IntNum(3,10,5);
System.out.println(ob1._min());
System.out.println(ob1._max());
}
3
} 10

 See example
◼ Page 243 (applying interfaces)
⚫ Stack Example
Variables in Interfaces
interface SharedConstants {
int Even = 0; Just like #define or const of
int Odd = 1; C/C++.
}

class Intvariable implements SharedConstants{


public static void main(String args[])
{
int a;
a=Integer.parseInt(“200”);
if(a%2==Even)
System.out.println("Even Number");
else if(a%2==Odd)
System.out.println("Odd Number");

}
}
Interfaces Can Be Extended
// One interface an extend another.
interface A { class IFExtend {
void meth1(); public static void main(String arg[])
void meth2(); {
} MyClass ob = new MyClass();
// B now includes meth1() and meth2() -- it adds meth3().
interface B extends A { ob.meth1();
void meth3(); ob.meth2();
} ob.meth3();
}
// This class must implement all of A and B
}
class MyClass implements B {
public void meth1() {
System.out.println("Implement meth1().");
}
public void meth2() {
System.out.println("Implement meth2().");
}
public void meth3() {
System.out.println("Implement meth3().");
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy