Saltar al contenido principal
JAVA INTERFACES
SHIV KUMAR
BCA 5TH SEM.
1822562
WHAT IS AN INTERFACE?
• An interface is a reference type in Java. It is similar to class.
• It is a collection of abstract methods.
• A class implements an interface, thereby inheriting the abstract methods of
the interface.
• Along with abstract methods, an interface may also contain constants, default
methods, static methods, and nested types. Method bodies exist only for
default methods and static methods.
• Writing an interface is similar to writing a class. But a class describes the
attributes and behaviours of an object. And an interface contains behaviours
that a class implements.
WHY AND WHEN TO USE INTERFACES?
• To achieve security - Hide certain details and only show the important details
of an object (interface).
• Java does not support "multiple inheritance" (a class can only inherit
from one superclass). However, it can be achieved with interfaces, because the
class can implement multiple interfaces.
• When a class implements an interface, we can think of the class as signing
a contract, agreeing to perform the specific behaviours of the interface. If a class
does not perform all the behaviours of the interface, the class must declare itself
as abstract.
AN INTERFACE IS SIMILAR TO A CLASS IN THE
FOLLOWING WAYS −
• An interface can contain any number of methods.
• An interface is written in a file with a .java extension, with the name of the
interface matching the name of the file.
• The byte code of an interface appears in a .class file.
• Interfaces appear in packages, and their corresponding bytecode file must be
in a directory structure that matches the package name.
HOWEVER, AN INTERFACE IS DIFFERENT FROM A CLASS
IN SEVERAL WAYS, INCLUDING −
• We cannot instantiate an interface.
• An interface does not contain any constructors.
• All of the methods in an interface are abstract.
• An interface cannot contain instance fields. The only fields that can appear in
an interface must be declared both static and final.
• An interface is not extended by a class; it is implemented by a class.
• An interface can extend multiple interfaces.
INTERFACES HAVE THE FOLLOWING PROPERTIES −
• An interface is implicitly abstract. You do not need to use
the abstract keyword while declaring an interface.
• Methods in an interface are implicitly public.
DECLARING INTERFACES
• The interface keyword is used to declare an interface.
• Following is EXAMPLE of an interface declaration −
IMPLEMENTING INTERFACES
• A class uses the implements keyword to
implement an interface.
• OUTPUT:
The pig says: wee wee
Zzz
EXTENDING INTERFACES
• An interface can extend another interface
in the same way that a class can extend
another class.
• The extends keyword is used to extend an
interface, and the child interface inherits
the methods of the parent interface.
• The following Sports interface is extended
by Hockey and Football interfaces.
EXTENDING MULTIPLE INTERFACES
• A Java class can only extend one parent class. Multiple inheritance is not
allowed.
• Interfaces are not classes, however, and an interface can extend more than
one parent interface.
• The extends keyword is used once, and the parent interfaces are declared
in a comma-separated list.
• For example, if the Hockey interface extended both Sports and Event, it
would be declared as −
THANK YOU!