Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 16

Department of Computer

Science and Engineering

Module II

More about Interfaces


Presented by: Dr. Ram Paul Hathwal

Dept of CSE, ASET, AUUP


Learning Objectives Department of Computer
Science and Engineering

To be able to declare and use Interface types.


To appreciate how Interfaces can be used to decouple classes
 Differences between Classes and Interfaces
 Important Points about Interfaces.
 Interface Hierarchy
 Types of Interfaces
 How to implement multiple inheritance in Java.
 Converting from Class to Interface.
Department of Computer
Interfaces Science and Engineering

Another way to achieve abstraction in Java.

An Interface is a completely “abstract class” which is used to group


related methods.

Interface types are used to express common operations.

Interfaces make it possible to make a service available to a wide set.


What is an Interface? Department of Computer
Science and Engineering

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

A Java interface is a collection of constants and abstract methods:


 abstract method: a method header without a method body; we declare an abstract
method using the modifier abstract.
 since all methods in an interface are abstract, the abstract modifier is usually left off.
Methods declared in an interface are always public and abstract, therefore Java compiler
will not complain if you omit both keywords
Static methods cannot be declared in the interfaces – these methods are never abstract
and do not express behavior of objects
Cannot be instantiated. Only classes that implements interfaces can be instantiated.
However, final public static variables can be defined to interface types.
Why not just use abstract classes? Java does not permit multiple inheritance from classes,
but permits implementation of multiple interfaces.
Differences between Department of Computer
Classes and Interfaces 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

Like abstract classes, Interface methods do not have


interfaces cannot be a body - the body is provided
used to create objects by the "implement" class

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 class Car implements Vehicle {


interface Vehicle { public void vehicleHorn() {
// interface method(does not have a body) // The body of vehicleHorn() is provided here
public void vehicleHorn(); System.out.println(“The Car has lowd horn”);
} }
Interface Hierarchies Department of Computer
Science and Engineering

 Inheritance can be applied to interfaces as well as classes.


 One interface can be used as the parent of another.
 The child interface inherits all abstract methods of the parent.
 A class implementing the child interface must define all methods from both the
parent and child interfaces.
 Note that class hierarchies and interface hierarchies are distinct (they do not overlap).
 The Java standard class library contains many helpful interfaces.
 Develop interfaces when you have code that processes objects of different classes in
a common way.
Extending Interfaces Department of Computer
Science and Engineering

interface Const
Syntax: {

interface name2 extends name1 int age=35;


String name=“CSE”)
{
}
body of name2 interface Methods extends Const
} {
void display();
}
interface Combine extends Const, Methods
{
………………………..
}
Different forms of Interfaces Department of Computer
Science and Engineering
Running Example: Department of Computer
Implementing Interfaces Science and Engineering

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

public float compute(float x, float y) }

{ }

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

Different forms of interfaces

Diamond problem in multiple inheritance

Solution to Diamond problem


Department of Computer
Science and Engineering
End

Thank you…

You might also like