Java Programming
Abstraction: Interface
ABSTRACTION IN JAVA
▪ Abstraction is a process of hiding the implementation details and showing
only functionality to the user.
• For example sending SMS, you just type the text and send the message. You
don't know the internal processing about the message delivery. Complex
details such as what happens as soon as you send the message is hidden from
the user.
▪ There are two ways to achieve abstraction in java
1. Abstract class
2. Interface
WHAT IS AN INTERFACE?
• Interface is a mechanism to achieve abstraction in java.
• Interface looks like class but it is not a class.
• An interface can have methods and variables just like the class but the methods
declared in interface are by default abstract (only method signature, no body).
• The variables declared in an interface are public, static & final by default.
• Interfaces are the blueprint of the class, it specify what a class must do and not how.
Uses of interface:
• Used to achieve abstraction
• It supports multiple inheritance
• Used to achieve loose coupling
MULTIPLE INHERITANCE IN JAVA
BY INTERFACE
• If a class implements multiple interfaces, or an interface extends multiple
interfaces i.e. known as multiple inheritance.
UNDERSTANDING RELATIONSHIP BETWEEN
CLASSES AND INTERFACES
• As shown in the figure given below, a class extends another class, an interface
extends another interface but a class implements an interface.
KEY POINTS OF INTERFACE
• We can’t instantiate an interface in java.
• Interface provides complete abstraction as none of its methods can have body.
• ”implements” keyword is used by classes to implement an interface.
• While providing implementation in class of any method of an interface, it needs to be mentioned as public.
• Class implementing any interface must implement all the methods, otherwise the class should be declared
as “abstract”.
• Interface cannot be declared as private, protected or transient.
• All the interface methods are by default abstract and public.
• Variables declared in interface are public, static and final by default.
• Class implements interface and interface extends interface.
• A class can implement any number of interfaces.
• If there are two or more same methods in two interfaces and a class implements both interfaces,
implementation of the method once is enough.
DIFFERENCE BETWEEN ABSTRACT
CLASS AND INTERFACE
Abstract Classes Interfaces
1 Abstract class can have both abstract and Interface can have only abstract methods
concrete methods
2 A class can extend only one abstract class A class can implement any number of
interfaces
In abstract class keyword ‘abstract’ is In an interface keyword ‘abstract’ is
3 mandatory to declare a method as an optional to declare a method as an abstract
abstract
4 Abstract class can have protected, public Interface can have only public abstract
and public abstract methods methods i.e. by default
Abstract class can have static, final or
5 static final variable with any access Interface can have only static final
specifier (constant) variable i.e. by default