Object Oriented
Programming
Topics To Be Covered Today
Interface
◦ Interface vs. Class
Interface inheritance
◦ Multiple Inheritance with Interface
◦ Java 8 Default Method in Interface
◦ Default Methods and Multiple Inheritance
◦ Java 8 Static Method in Interface
Abstract Class vs. Interface
Interface
A blueprint of a class which has static constants
and abstract methods.
A mechanism to achieve abstraction
◦ There can be only abstract methods in interface
A class implements an interface, thereby inheriting
the abstract methods of the interface.
◦ A class that implements interface must implement all the
methods declared in the interface.
◦ Java Interface also represents IS-A relationship.
It cannot be instantiated just like abstract class.
Interface VS Class
Writing an interface is similar to writing a class.
But:
◦ A class describes the attributes and behaviors of an
object.
◦ While an interface contains behaviors that a class
implements.
a class extends another class, an interface
extends another interface but a class
implements an interface.
Interface is similar to class
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.
Interface is different from a class
An interface is different from a class in several
ways:
◦ An interface cannot be instantiated.
◦ 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.
Why use Java interface?
There are mainly three reasons to use interface
◦ It is used to achieve abstraction.
◦ By interface, we can support the functionality of multiple
inheritance.
◦ It can be used to achieve loose coupling.
Loose coupling just means that components are able to work
together without assuming anything about the internal
workings of each other.
https://
[Link]/questions/19483626/how-is-loose-couplin
g-achieved-using-interfaces-in-java-when-an-implementation
-c
How to declare interface?
Interface is declared by using interface keyword
/* File name : [Link] */
public interface NameOfInterface {
// Any number of final, static fields
// Any number of abstract method declarations
}
Interfaces have the following properties:
◦ An interface is implicitly abstract.
You do not need to use the abstract keyword while
declaring an interface.
◦ Each method in an interface is also implicitly abstract, so
the abstract keyword is not needed.
◦ Methods in an interface are implicitly public and abstract
while all fields are public, static and final by default.
Internal addition by compiler
Interface fields are public, static and final by
default, and methods are public and abstract.
The java compiler adds internally:
◦ public and abstract keywords before the interface
method.
◦ public, static and final keywords before data members.
Example
Printable interface having only one method
Implementation is provided in the InterfaceExp
class.
//What if I remove public??
Implementing Interfaces
When a class implements an interface, there are
several rules:
◦ Sign a contract, agreeing to perform the specific
behaviors of the interface.
If a class does not perform all the behaviors of the interface,
the class must declare itself as abstract
◦ An interface can extend another interface, in a similar
way as a class can extend another class.
◦ A class can extend only one class, but can implement
more than one interface at a time.
Interface Example: Drawable
Explanation
In this example:
◦ Drawable interface has only one method.
◦ Its implementation is provided by Rectangle and Circle
classes.
◦ In real scenario, interface is defined by someone but
implementation is provided by different implementation
providers.
◦ And, it is used by someone else.
The implementation part is hidden by the user
which uses the interface.
Multiple inheritance by interface
If a class implements multiple interfaces, or an
interface extends multiple interfaces i.e. known as
multiple inheritance
Example
Question
Q)Multiple inheritance is not supported through
class in java but it is possible by interface, why?
Answer:
multiple inheritance is not supported in case of
class because of ambiguity
But it is supported in case of interface because
there is no ambiguity
◦ As implementation is provided by the implementation
class not the implemented interface
Example
Example
Interface inheritance: Extend Interface
Extending Multiple Interfaces
//Multiple Inheritance
Java 8 Default Method in Interface
Since Java 8, we can have method body in
interface. But we need to make it default method
Default Methods and Multiple
Inheritance
Suppose, both the implemented interfaces contain
default methods with same method signature,
then which method will be called ???
The implementing class should explicitly specify
which default method is to be used or it should
override the default method.
Contd.
Java 8 Static Method in Interface
Since Java 8, we can have static method in
interface
Question
Q) Can an Interface be final?
◦ No, because its implementation is provided by another
class.
Q)Can we define private and protected modifiers for
variables in interfaces?
◦ No, they are implicitly public.
Q) What is similarity between abstract class and
interface?
◦ both are used to achieve abstraction where we can
declare the abstract methods.
◦ both can't be instantiated.
Abstract Class VS Interface
Abstract class can have Interface can have only
abstract and non- abstract methods.
abstract methods. ◦ Since Java 8, it can
Abstract class doesn't have default and static
support multiple inheritance. methods also.
Abstract class can have final,
Interface supports multiple
non-final, static and non- inheritance.
static variables.
Interface has only static and
final variables.
Abstract class can provide Interface can't provide the
the implementation of
implementation of abstract
interface.
class.
The abstract keyword is used The interface keyword is
to declare abstract class. used to declare interface.
Abstract class Interface
Abstract Class VS Interface(Contd.)
An abstract class can An interface can extend
extend another class and another interface only.
implement multiple An interface class can be
interfaces. implemented using keyword
An abstract class can be “implements”
extended using keyword ◦ Interface can also be extended
using extend keyword
“extends”. Members of a Java
An abstract class can have
interface are public by
class members like private,
default.
protected, etc.
public abstract class Shape{ public interface Drawable {
public abstract void draw(); void draw();
} }
Abstract Class Interface
Example of abstract class and
interface in Java
Contd..
Questions?