Module II - More About Interfaces
Module II - More About Interfaces
Module II
An interface defines a protocol of behavior that can be implemented by any class
anywhere in the class hierarchy.
An interface defines a set of methods but does not implement them. A class that
implements the interface agrees to implement all the methods defined in the interface,
thereby agreeing to certain behavior.
An interface is a named collection of method definitions (without implementations).
Interface reserve behaviors for classes that implement them.
Variables can be declared in the interfaces. They can only be declared as static and
final. – Both keyword are assumed by default and can be safely omitted.
Sometimes interfaces declare only constants – be used to effectively import sets of
related constants.
4
More about Interface Department of Computer
Science and Engineering
An interface type does not have instance variables but a class can.
All methods in an interface type are abstract (or in Java 8, static or default)
They have a name, parameters, and a return type, but no implementation.
All methods in an interface type are automatically public.
An interface type has no constructor.
You cannot construct objects of an interface type.
Use interface types to make code more reusable.
A class can only extend (inherit from) a single superclass.
An interface specifies the behavior that an implementing class should supply
(in Java 8, an interface can now supply a default implementation).
A class can implement more than one interface.
Important Points Department of Computer
Science and Engineering
On implementation of an
Interface methods are by
interface, you must override
default abstract and public
all of its methods
Interface attributes
are by default public, An interface cannot
static and final contain a constructor
Example of Interface Department of Computer
Science and Engineering
interface InterfaceName
{ //variables declaration
variables declaration; static final type variablename=value;
methods declaration; //methods declaration
} return-type methodname1 (parameter_list);
interface Const
Syntax: {
interface Area {
class ICT{
static final float pi=3.14f; public static void main(String args[]) {
float compute (float x, float y); } Rectangle rect=new Rectangle();
class Rectangle implements Area { Circle c=new Circle();
public float compute(float x,float y) Area area;
area=rect;
{
System.out.println("Area of rectangle ="+area.compute(10,20));
return(x*y);
area=c;
}}
class Circle implements Area{
System.out.println("Area of Circle ="+area.compute(10,0));
{ }
return(pi*x*x);
} }
Converting from Department of Computer
Classes to Interfaces Science and Engineering
You can convert from a class type to an interface type, provided the class implements it.
A Measurable variable can refer to an object of the BankAccount class because
BankAccount implements the Measurable interface:
BankAccount account = new BankAccount(1000);
Measurable meas = account; // OK
A Measurable variable can refer to an object of the Country class because that class also
implements the Measurable interface:
Country uruguay = new Country("Uruguay", 176220);
Measurable meas = uruguay; // Also OK
A Measurable variable cannot refer to an object of the Rectangle class because Rectangle
doesn't implement Measurable:
Measurable meas = new Rectangle(5, 10, 20, 30); // ERROR
Why and When Department of Computer
to Use Interfaces? Science and Engineering
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).
It can be achieved with interfaces, because the class can implement multiple interfaces.
An interface can extends other interfaces, just as a class can extends another classes.
An interface can extend any number of interfaces.
The list of superinterfaces is a comma-separated list of all the interfaces extended by the
new interface.
Interfaces are designed to support dynamic method resolution at run time.
Methods in an interface have public visibility by default.
They disconnect the definition of a method or set of methods from the inheritance
hierarchy.
Summary
Department of Computer
Science and Engineering
Importance of Interfaces
Thank you…