2. What is an Interface?
• It is a contract that declares a set of abilities (WHAT)
without defining how it is implemented.
• Any concrete class that implements this interface is
signing up to implement all the methods specified in
the contract (interface).
Example:
public interface Automobile {
public abstract start();
public abstract accelerate();
public abstract stop();
}
3. Features of an Interface
• By default, all methods are public and abstract
(no body).
• No instance variables.
• Any variable defined in an interface is
public, static and final. (implies class level
constants).
• A concrete class implements all methods of an
interface.
Example:
public class Triangle implements Shape {
// All methods of Shape have to be implemented.
}
4. Abstract Class implement Interface
• An abstract class can implement some
methods of an interface. It needs to be left
with at least 1 unimplemented method.
Example:
public abstract class TwoDimShape
implements Shape {
// Some methods of Shape have to be implemented.
}
5. Interfaces solve Multiple Inheritance
problem in Java
• A class cannot extend more than 1 class.
• A class can implement multiple interfaces at
the same time.
• An interface can also extend multiple
interfaces.
• A java class can extend exactly 1 class and
implement multiple interfaces at the same
time.
6. Polymorphism via Interfaces
• A class implementing an interface has an
“is-a” relationship with it’s interface.
• If Triangle class implements the Shape
class, then every triangle is a shape.
• Hence, we can say: Shape s = new Triangle();
• Also, if a method parameter expects an
interface, you can invoke that method by
passing a class object that implements that
interface.
7. Program to Interfaces
• Instead of writing your classes in a way that says:
I depend on this specific class to do this stuff
• you write it in a way that says
I depend on any interface that does this stuff
Examples:
Drivable[] myDrivables = new Drivable[2];
myDrivables[0] = new Car();
myDrivables[1] = new Bike();
for (int i = 0; i < 2; ++i)
{
myDrivables[i].accelerate();
}
8. Good to Know: Liskov Substitution
Principle
• In a computer program, if S is a subtype of
T, then objects of type S may be
substituted for objects of type T without
altering the correctness of the program.
9. Difference between Abstract Classes
and Interfaces
Feature
Abstract Class
Interface
Abstract Methods
At least ONE
All are abstract
Inheritance
Can extend only ONE
Interface CAN extend many
Class CAN implement many
Instance Variables
CAN have it
CANNOT have any
Access modifiers
public, private or protected ONLY public
Constructors
CAN have it
CANNOT have any
10. Abstract Classes or Interfaces ?
• If design can potentially change in the future, use
interfaces.
• If inheritance hierarchy is stable, use abstract
classes, especially for all non-leaf classes in the
hierarchy.
• If various implementations only share method
signatures, use interfaces.
• If various implementations share both method
signatures & state, use abstract classes.
• If only some implementations need to provide a
certain behavior, use interfaces.