0% found this document useful (0 votes)
7 views17 pages

INTERFACES

An interface in programming specifies an API for functionalities without defining implementations, with all methods being implicitly abstract and public. Interfaces cannot have instance fields, cannot be instantiated, and can be implemented by classes that must provide implementations for all declared methods. They allow for multiple inheritance and can extend other interfaces, making them useful for defining common behaviors across unrelated classes.

Uploaded by

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

INTERFACES

An interface in programming specifies an API for functionalities without defining implementations, with all methods being implicitly abstract and public. Interfaces cannot have instance fields, cannot be instantiated, and can be implemented by classes that must provide implementations for all declared methods. They allow for multiple inheritance and can extend other interfaces, making them useful for defining common behaviors across unrelated classes.

Uploaded by

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

INTERFACES

An interface is a reference type that is


closely related to the class .
As it name implies , an interface specifies
an interface or API for certain
functionalities.
It does not define any implementation of
that API, however.
INTERFACES
There are a number of restrictions that
apply to the members of an interface---
– All methods of an interface are implicitly
abstract, even if the abstract modifier is
omitted.
– All methods of an interface are implicitly
public, even if the public modifier is omitted.It
is an error to define a protected or private
method in an interface.
INTERFACES
An interface cannot define instance fields,
the only fields allowed in an interface are
constants that are declared both static and
final
An interface cannot be instantiated,so it
does not define a constructor.
INTERFACES
Implementing an interface:--
– Just as a class uses extends to specify its super
class , it can use implements to name one or more
interfaces
– When a class implements an interface , it has to give
implementation for all the methods that are declared
in the interface , otherwise the class must be
declared abstract, otherwise the code will not be
compiled at all
INTERFACES
EXAMPLE:---
public interface Greeting {
public void greetUser( ) ;
}
public class SayHello implements Greeting {
public void greetUser( ) {
System.out.println(“Hello”);
}
}
public class SayBye implements Greeting {
public void greetUser( ) {
System.out.println(“Bye”);
}
}
INTERFACES

When to use interfaces:--


– When you are not going to give implementation
for most of the methods, go for an interface
– An interface is useful because any class can
implement it, even if that class extends some
entirely unrelated super class
INTERFACES

Implementing multiple interfaces:--


– When a class implements more than one interface, it
must provide implementation for all the methods in
all the interfaces
– Java supports multiple inheritance in case of
interfaces
INTERFACES

Extending Interfaces :----


– Interfaces can have sub-interfaces , just as classes
have sub-classes
– A sub- interface inherits all the methods and
constants of its super-interfaces
– An interface can have an extends clause that lists
more than one super-interfaces
INTERFACES
A class can extend another class as well as
implement some interface
public class MyApplet extends Applet implements
Runnable { … }
A class can implement multiple interfaces
public class MyApplet extends Applet implements
Runnable, MouseListener { … }
An interface can “extend” another interface
public interface xxx { … }
public interface yyy extends xxx { … }
INTERFACES
Interfaces are useful for :
– Declaring methods that one or more classes are
expected to implement
– Determining an object’s programming interface
without revealing the actual body of the class
– Capturing similarities between unrelated classes
without forcing a class relationship
INTERFACES
Interfaces
– As with abstract classes, you can use an interface
name as a type of reference variable
– References can be cast to and from interface
types, and you can use the “instanceof” operator to
determine if an object’s class implements an
interface
INTERFACES

Advantages:---
– In multi-level inheritance , when an application is
dealing with the lowest level class , then the run-
time JVM will create in an order the constructor of
all the above super class resulting in objects that
might not be used in the applications.
– Interfaces will avoid this
INTERFACES

– Since interface does not cost much in the memory


resources during runtime, you can have classes
implementing any no. of interfaces.
– Interfaces can extend other interfaces inorder to give
a meaningful information to the designer.
– Ex:-StainlessSteel extends Steel
INTERFACES

Dynamic method dispatch:--


– When there are interface variables references for
multiple class objects which have implemented the
interfaces, then the JVM during the runtime will
check which class object the variable is referring to
and execute that implementation only.
INTERFACES
MyIf mi = new MyIfImpl1 ( );
mi.xyz();
mi.abc(); X

mi = new MyIfImpl2 ( );
mi.xyz();
mi.pqr(); X
INTERFACES

A marker interface is an interface having no


methods for which you have to give
implementation
The Clonable interface is a marker interface
It defines no methods ,but identifies the class as
one that allows its internal state to be cloned by
the clone( ) method of the Object class

You might also like